/*
 * @Author: XianKaiQun
 * @LastEditors  : WuWei
 * @Date: 2020-11-04 10:43:03
 * @LastEditTime : 2023-05-25 15:55:58
 */

import 'package:flutter/material.dart';
import 'package:wisdom_cli/src/assets.gen.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WImageSelecter extends StatefulWidget {
  const WImageSelecter({
    Key? key,
    this.size,
    required this.onSelected,
    required this.currentCount,
    this.maxCount = 9,
    this.source = WImageSource.cameraAndGallery,
    this.sourceCallBack,
  }) : super(key: key);

  ///Widget尺寸大小
  final double? size;

  ///选中图片后的回调，注意可能会返回`null`
  final void Function(List<FileEntity> values) onSelected;

  final int currentCount;

  /// [maxCount]最大选择数目，
  final int maxCount;

  /// [source]  图片来源
  final WImageSource source;

  /// [sourceCallBack] 回调图片来源（相机或相册）
  final void Function(WImageSource source)? sourceCallBack;
  @override
  _WImageSelecterState createState() => _WImageSelecterState();
}

class _WImageSelecterState extends State<WImageSelecter> {
  final FocusNode focusNode = FocusNode();
  @override
  void dispose() {
    focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final maxCount = widget.maxCount;
    final currentCount = widget.currentCount;
    if (currentCount >= maxCount) return SizedBox(width: 0, height: 0);
    return Container(
      width: widget.size ?? 65.pt,
      height: widget.size ?? 65.pt,
      child: Wisdom.column(
        inkWell: WisBoxInkWell(
          onTap: () async {
            focusNode.requestFocus();
            final res = await WAssetPickerUtil.image(
              maxCount: maxCount - currentCount,
              source: widget.source,
              sourceCallBack: widget.sourceCallBack,
            );
            widget.onSelected(res);
          },
          focusNode: focusNode,
        ),
        margin: EdgeInsets.only(right: 5.pt, top: 5.pt),
        borderRadius: BorderRadius.circular(4.pt),
        mainAxisAlignment: MainAxisAlignment.center,
        color: colorScheme.background,
        children: [
          if ((widget.maxCount) > 1) SizedBox(height: 8.pt),
          Image(
            image: AssetList.$image_png_image,
            width: 20.pt,
          ),
          if ((widget.maxCount) > 1)
            Text(
              '${widget.currentCount}/${widget.maxCount}',
              style: TextStyle(
                color: colorScheme.minor,
                height: 1.6,
                fontSize: 10.pt,
              ),
            ),
        ],
      ),
    );
  }
}
