import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:keyboard/data/bean/image_viewer_item.dart';
import 'package:keyboard/module/intimacy_analyse/intimacy_analyse_upload/widget/upload_add_widget.dart';
import 'package:keyboard/module/intimacy_analyse/intimacy_analyse_upload/widget/upload_item_widget.dart';
import 'package:keyboard/utils/prefix_util.dart';
import 'package:keyboard/utils/upload/file_data_source_util.dart';
import '../../../../data/bean/upload_info.dart';
import '../../../../data/consts/constants.dart';
import '../../../../utils/step_card_util.dart';
import '../../../../utils/upload/upload_state.dart';
import '../../image_viewer/image_viewer_page.dart';

/// 模式
enum Mode {
  /// 编辑模式
  edit,

  /// 预览模式
  preview,
}

/// 上传九宫格
class UploadNineGrid extends StatelessWidget {
  /// 模式
  final Mode mode;

  /// 图片资源列表
  final List<UploadInfo> imageUploadInfoList;

  /// 最大显示数量
  final int maxCount;

  /// 图片间距
  final double spacing;

  /// 点击上传按钮的回调
  final OnClickAddCallback? onClickAddCallback;

  /// 点击删除时回调
  final OnClickDeleteCallback? onClickDeleteCallback;

  const UploadNineGrid({
    super.key,
    required this.mode,
    required this.imageUploadInfoList,
    this.maxCount = Constants.uploadNineGridMaxCount,
    this.spacing = 2.0,
    this.onClickAddCallback,
    this.onClickDeleteCallback,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(
        // 最小高度
        // 最少1行
        minHeight: 90.h,
        // 最少2行
        // minHeight: 180.h,
      ),
      child: GridView.builder(
        // 去掉默认的Padding，默认会有一个默认的顶部padding大小
        padding: EdgeInsets.zero,
        // 包裹内容
        shrinkWrap: true,
        // 禁止滚动
        physics: const NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          // 宫格的列数
          crossAxisCount: 3,
          // 垂直方向的间距
          crossAxisSpacing: spacing,
          // 水平方向的间距
          mainAxisSpacing: spacing,
        ),
        itemCount: StepCardUtil.getRealItemCount(
          mode,
          imageUploadInfoList.length,
          maxCount,
        ),
        itemBuilder: (context, index) {
          // 添加图片的条目
          if (index >= imageUploadInfoList.length) {
            return UploadAddWidget(
              // 剩余多少张图片，可以上传
              residueCount: maxCount - imageUploadInfoList.length,
              // 点击上传图片
              onClickAddCallback: onClickAddCallback,
            );
          } else {
            // 上传项
            UploadInfo info = imageUploadInfoList[index];
            return _buildUploadItem(info, index);
          }
        },
      ),
    );
  }

  /// 上传项
  Widget _buildUploadItem(UploadInfo info, int index) {
    return UploadItemWidget(
      imageUploadInfo: info,
      // 编辑状态时，才有删除按钮
      hasDeleteBtn: mode == Mode.edit,
      // 点击删除按钮时回调，删除图片
      onClickDeleteCallback: (UploadInfo uploadInfo) {
        if (onClickDeleteCallback != null) {
          onClickDeleteCallback!(uploadInfo);
        }
      },
      onClickItemCallback: (UploadInfo uploadInfo) {
        // 预览图片
        ImageViewerPage.start(
          imageUploadInfoList.map((info) {
            String src = FileDataSourceUtil.getFileDataSourceSrc(info);
            // 远程图片
            if (PrefixUtil.isRemoteImage(src)) {
              return ImageViewerItem.network(src);
            }
            // 本地图片
            return ImageViewerItem.file(src);
          }).toList(),
          index: index,
        );
      },
    );
  }
}
