import 'dart:io';

import 'package:classify_photo/classify_photo.dart';
import 'package:clean/base/base_controller.dart';
import 'package:clean/module/image_picker/image_picker_util.dart';
import 'package:clean/module/locations_photo/locations_photo_view.dart';
import 'package:clean/module/people_photo/people_photo_view.dart';
import 'package:clean/module/similar_photo/similar_photo_view.dart';
import 'package:clean/utils/toast_util.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:photo_classify/photo_classify.dart';
import 'dart:typed_data';

import 'package:wechat_assets_picker/wechat_assets_picker.dart';
class HomeController extends BaseController {

  final  Rx<double> totalSpace = 500.0.obs;
  final  Rx<double> usedSpace = 300.0.obs;
  final  Rx<double> photoSpace = 100.0.obs;
  final Rx<double> freeSpace = 200.0.obs;

  // 计算已用存储百分比
  double get usedSpacePercentage => (usedSpace.value / totalSpace.value) * 100;

  // 计算照片占用存储百分比
  double get photoSpacePercentage => (photoSpace.value / totalSpace.value) * 100;

  // 计算可用存储百分比
  double get freeSpacePercentage => (freeSpace.value / totalSpace.value) * 100;

  RxList<String> similarImages = List.generate(4, (index) => 'iconHomeNoPhoto').obs;
  RxInt imageCount = 0.obs;

  // 相似图片
  RxList<AssetEntity> similarPhotos = <AssetEntity>[].obs;
  // 人物图片
  RxList<File> peoplePhotos = <File>[].obs;
  // 地点图片
  Rx<File?> locationPhoto = Rx<File?>(null);
  // 截图照片
  Rx<File?> screenshotPhoto = Rx<File?>(null);

  similarCleanClick() {
    print('similarCleanClick');

    SimilarPhotoPage.start();
  }
  peopleCleanClick() {
    PeoplePhotoPage.start();
    print('peopleCleanClick');
  }
  locationCleanClick() {

    LocationsPhotoPage.start();
    print('locationCleanClick');
  }
  screenshotCleanClick() {
    print('screenshotCleanClick');


  }
  blurryCleanClick() {
    print('blurCleanClick');
  }

  @override
  Future<void> onInit() async {
    // TODO: implement onInit
    super.onInit();

    handlePhotos();
  }

  void handlePhotos() async {
    final photoClassify = ClassifyPhoto();
    try {
      print('开始获取照片');
      final photos = await photoClassify.getPhoto();
      print('获取照片完成: ${photos?.length ?? 0} 组照片');

      if (photos != null) {
        await ImagePickerUtil.updatePhotos(photos);

        similarPhotos.clear();
        if (ImagePickerUtil.similarPhotos.isNotEmpty) {
          for (var group in ImagePickerUtil.similarPhotos) {
            for (var asset in group) {
              similarPhotos.add(asset);
              if (similarPhotos.length == 4) {
                break;
              }
            }
          }
        }

        // 处理地点照片
        locationPhoto.value = null;
        if (ImagePickerUtil.locationPhotos.isNotEmpty) {
          // 获取第一个地点的第一张照片
          final firstLocationPhotos = ImagePickerUtil.locationPhotos.values.first;
          if (firstLocationPhotos.isNotEmpty) {
            var file = await firstLocationPhotos.first.file;
            if (file != null) {
              locationPhoto.value = file;
            }
          }
        }

        // 处理人物照片
        peoplePhotos.clear();
        if (ImagePickerUtil.peoplePhotos.isNotEmpty) {
        for (var personPhotos in ImagePickerUtil.peoplePhotos) {
            var file = await personPhotos.file;
            if (file != null) {
              peoplePhotos.add(file);
              if (peoplePhotos.length == 2) {
                break;
              }
            }
          }
        }

        if (ImagePickerUtil.screenshotPhotos.isNotEmpty) {
          var file = await ImagePickerUtil.screenshotPhotos.first.file;
          if (file != null) {
            screenshotPhoto.value = file;
          }
        }
      }
    } catch (e, stackTrace) {
      print('获取照片失败: $e');
      print('Stack trace: $stackTrace');
    }
  }
}