import 'dart:io';

import 'package:clean/base/base_controller.dart';
import 'package:clean/module/image_picker/image_picker_util.dart';
import 'package:clean/module/locations_photo/locations_single_photo_view.dart';
import 'package:clean/module/people_photo/photo_group.dart';
import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class LocationsPhotoController extends BaseController {
   final RxList<PhotoGroup> photoGroups = <PhotoGroup>[].obs;


  @override
  onInit() {
    super.onInit();
    loadLocationPhoto();
  }

  void loadLocationPhoto() {
    photoGroups.clear();

    if (ImagePickerUtil.locationPhotos.isEmpty) {
      print('locationPhotos.isEmpty');
      return;
    }
    if (ImagePickerUtil.locationPhotos.values.isNotEmpty) {
      print('locationPhotos.isNotEmpty ${ImagePickerUtil.locationPhotos.values}');
      for (var entry in ImagePickerUtil.locationPhotos.entries) {
        photoGroups.add(PhotoGroup(
          isSelected: false,
          images: entry.value,
          location: entry.key,
        ));
      }
    }


  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    restoreSelections();
  }
  void clickPhotoGroup(PhotoGroup photoGroup) {
    print('clickPhotoGroup ${photoGroup.location}');
    LocationsSinglePhotoPage.start(photoGroup: photoGroup);
  }

  PhotoGroup getGroupByLocation(String? location) {
    return photoGroups.firstWhere((group) => group.location == location);
  }

  void restoreSelections() {
    for (var group in photoGroups) {
      for (int i = 0; i < group.images.length; i++) {
        group.selectedImages[i] = ImagePickerUtil.selectedLocationPhotosIds.contains(group.images[i].id);
      }
    }
  }

}
