import 'package:clean/module/people_photo/photo_group.dart';
import 'package:intl/intl.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

import '../../base/base_controller.dart';
import 'package:get/get.dart';

import '../../utils/file_utils.dart';
import '../../utils/image_util.dart';
import '../image_picker/image_picker_util.dart';
import 'calendar_month_view.dart';

class CalendarController extends BaseController {
  RxList<PhotoGroup> monthlyAlbums = <PhotoGroup>[].obs;
  RxList<AssetEntity> imageList = <AssetEntity>[].obs;

  @override
  void onInit() {
    super.onInit();
    loadAssets();
  }

  // 加载并分组图片
  Future<void> loadAssets() async {
    final List<AssetEntity> result = await ImagePickerUtil.loadAssetsAndroid();
    result.sort((a, b) => b.createDateTime.compareTo(a.createDateTime));

    imageList.value = result;
    updateMonthlyAssets();
  }

  // 更新按月份分组的照片
  void updateMonthlyAssets() {
    Map<String, List<AssetEntity>> groupedAssets = {};

    for (var asset in imageList) {
      final monthKey = DateFormat('yyyy-MM').format(asset.createDateTime);
      groupedAssets.putIfAbsent(monthKey, () => []).add(asset);
    }


    monthlyAlbums.clear();
    groupedAssets.forEach((month, assets) {
      assets
          .sort((a, b) => b.createDateTime.compareTo(a.createDateTime)); // 时间排序
      monthlyAlbums.add(PhotoGroup(month: month, images: assets, isSelected: false));
    });

    // 打印结果
    for (var album in monthlyAlbums) {
      print('${album.month}: ${album.images.length} 张照片');
    }
  }

  void clickMonthCard(PhotoGroup photoGroup) {
    print("clickMonthCard");
    CalendarMonthPage.start(photoGroup: photoGroup);
  }

  void clickImage(){
    print("clickImage");
  }



}


