import 'dart:io';
import 'dart:isolate';

import 'package:clean/utils/file_size_calculator_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

import '../image_picker/image_picker_util.dart';

class PhotoGroup {
  // 照片组的选择状态
  final RxBool isSelected;

  // 照片组中的图片列表
  final RxList<AssetEntity> images;

  // 每张图片的选择状态
  final RxList<bool> selectedImages;

  // 已选中图片的唯一标识集合
  final RxSet<String> selectedPhotosIds = <String>{}.obs;

  // 照片组的位置
  final String? location;

  // 选中文件的总大小
  RxDouble selectedTotalSize = 0.0.obs;

  // 照片组的月份
  final String? month;

  // 整个 images 列表的总大小
  RxDouble totalSize = 0.0.obs;

  // 获取已选中的图片数量
  int get selectedCount => selectedPhotosIds.length;

  // 构造函数
  PhotoGroup({
    required bool isSelected,
    required List<AssetEntity> images,
    this.location,
    this.month,
  })  : isSelected = isSelected.obs,
        images = images.obs,
        selectedImages = RxList<bool>.filled(images.length, isSelected) {
    // 初始化已选中图片的唯一标识集合
    if (isSelected) {
      selectedPhotosIds.addAll(images.map((e) => e.id).toList());
    }
  }

  Future<void> initTotalSize() async {
    await FileSizeCalculatorUtil.calculateTotalSizeByIOS(
         assetIds: images.map((e) => e.id).toSet(), updateValue: (double totalSize) {

      this.totalSize.value = totalSize; // 监听并更新 UI
    });
  }


  // 切换选择所有图片的状态
  void toggleSelectAll(bool value) {
    isSelected.value = value;
    selectedImages.assignAll(List.filled(images.length, value));
    if (value) {
      selectedPhotosIds.addAll(images.map((e) => e.id).toList());
    } else {
      selectedPhotosIds.clear();
    }
  }

  /// 判断某张图片是否被选中
  bool isImageSelected(AssetEntity image) {
    return selectedPhotosIds.contains(image.id);
  }
}
