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

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

class WFImagePicker extends StatelessWidget {
  const WFImagePicker({
    Key? key,
    this.title,
    this.isRequired = false,
    required this.onSelected,
    required this.onDelete,
    required this.images,
    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 List? images;
  final void Function(List<FileEntity> files)? onSelected;
  final void Function(int index)? onDelete;
  @override
  Widget build(BuildContext context) {
    // final style = WTheme.of(context);
    // final colorScheme = style.colorScheme;
    final _images = images ?? [];
    final _source = source;
    final _size = size ?? 65.pt;
    return WisFormLayoutComponent(
      title: title,
      isRequired: 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 (isBefore) ...[
            if (maxCount != null && (maxCount ?? 0) > (currentCount ?? 0))
              WImageSelecter(
                size: _size,
                onSelected: onSelected!,
                maxCount: maxCount ?? 0,
                currentCount: currentCount ?? 0,
              ),
          ],
          ...WImagePreview.buildList(
            size: _size,
            images: _images
                .map((e) =>
                    CachedNetworkImageProvider(AppParseUtil.image(e['id'])))
                .toList(),
            onDelete: onDelete,
          ),
          if (!isBefore) ...[
            if (maxCount != null && (maxCount ?? 0) > (currentCount ?? 0))
              WImageSelecter(
                source: _source,
                size: _size,
                onSelected: onSelected!,
                maxCount: maxCount ?? 0,
                currentCount: currentCount ?? 0,
              ),
          ],
        ],
      ),
    );
  }
}
