/*
 * @Author: XianKaiQun
 * @LastEditors  : WuWei
 * @Date: 2020-10-26 11:07:39
 * @LastEditTime : 2023-07-03 16:57:52
 */

// import 'dart:io';

// import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

import '../../../new/widgets/image_preview_file.dart';

class WFImageLocalPicker extends StatefulWidget {
  WFImageLocalPicker({
    Key? key,
    this.title,
    this.isRequired = false,
    required this.onSelected,
    required this.onDelete,
    this.maxCount = 3,
    this.currentCount,
    this.size,
    this.isBefore = false,
    this.source = WImageSource.cameraAndGallery,
  }) : super(key: key);

  final Widget? title;
  final double? size;
  final bool isBefore;
  final source;

  ///是否必须
  final bool? isRequired;
  final int? maxCount;
  final int? currentCount;
  final void Function(List<FileEntity> files)? onSelected;
  final void Function(int index)? onDelete;

  @override
  State<WFImageLocalPicker> createState() => _WFImageLocalPickerState();
}

class _WFImageLocalPickerState extends State<WFImageLocalPicker> {
  List<FileEntity> _images = [];
  setImages(values) {
    _images = _images.length > 0 ? [..._images, ...values] : values ;
    setState(() {});
     widget.onSelected!(_images);
  }

  onDeleteImg(index) {
    _images.removeAt(index);
    print(_images);
    setState(() {});
     widget.onSelected!(_images);
  }

  @override
  Widget build(BuildContext context) {
    final _source = widget.source;
    final _size = widget.size ?? 65.pt;
    return WisFormLayoutComponent(
      title: widget.title,
      isRequired: widget.isRequired,
      axis: Axis.vertical,
      child: Wisdom.wrap(
        runSpacing: 10.pt,
        spacing: 10.pt,
        runAlignment: WrapAlignment.start,
        crossAxisAlignment: WrapCrossAlignment.start,
        wrapAlignment: WrapAlignment.start,
        padding: EdgeInsets.only(bottom: 15.pt),
        children: [
          if (widget.isBefore) ...[
            if (widget.maxCount != null &&
                (widget.maxCount ?? 0) > (widget.currentCount ?? 0))
              WImageSelecter(
                size: _size,
                onSelected: (values) {
                  setImages(values);
                },
                maxCount: widget.maxCount ?? 0,
                currentCount: widget.currentCount ?? 0,
              ),
          ],
          ...WImagePreviewFile.buildList(
            size: _size,
            images: _images.map((e) => e.file!).toList(),
            onDelete: (index) {
              onDeleteImg(index);
            },
          ),
          if (!widget.isBefore) ...[
            if (widget.maxCount != null &&
                (widget.maxCount ?? 0) > (widget.currentCount ?? 0))
              WImageSelecter(
                source: _source,
                size: _size,
                onSelected: (values) {
                  setImages(values);
                },
                maxCount: widget.maxCount ?? 0,
                currentCount: widget.currentCount ?? 0,
              ),
          ],
        ],
      ),
    );
  }
}
