/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-10-10 09:42:27
 * @LastEditTime : 2023-05-15 15:47:37
 * @Description  : Do not edit
 */
import 'package:app_package/app_package.dart';
import 'package:get/get.dart';
import 'package:statistics_package/src/models/company_community.dart';
import 'package:statistics_package/src/service/impl/statistics_impl.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class StatisticFilterStore extends GetxController {
  final appGlobalStore = GetIt.I.get<AppGlobalStore>();
  var communityIds = [].obs;
  var communities = [].obs;
  int communityLength = 0;
  var communityTree = <CompanyCommunityModel>[].obs;
  var isAllCheckedCommunitys = [].obs;
  var allCheckedCommunityCount = [].obs;

  @override
  void onClose() {
    communityIds = [].obs;
    communities = [].obs;
    communityLength = 0;
    communityTree = <CompanyCommunityModel>[].obs;
    isAllCheckedCommunitys = [].obs;
    allCheckedCommunityCount = [].obs;
    super.onClose();
  }

  /// 获取公司小区树
  Future<void> getCompanyTree() async {
    var res = await StatisticsServiceImpl.instance!.getCompanyCommunityList();
    res.data?.forEach((company) {
          allCheckedCommunityCount.add(0);
          communityLength += company.communities!.length;
        });
    communityTree.value = res.data ?? [];
    communityTree.refresh();
  }

  /// 选择小区
  void checkCommunitys(item, index) async {
    if (communityIds.contains(item.communityId)) {
      allCheckedCommunityCount[index] = allCheckedCommunityCount[index] - 1;
      communityIds.removeWhere((data) => data == item.communityId);
      communities
          .removeWhere((element) => element.communityId == item.communityId);
    } else {
      allCheckedCommunityCount[index] = allCheckedCommunityCount[index] + 1;
      communities.add(item);
      communityIds.add(item.communityId);
    }
    allCheckedCommunityCount.refresh();
    communityIds.refresh();
    communities.refresh();
    update();
  }

  RxBool isAllChecked = false.obs;

  /// 全选
  void allCheckCommunitys() async {
    if (communityIds.length == communityLength && communityIds.length != 0) {
      isAllChecked.value = false;
      communityIds.value = [];
      communities.value = [];
      for (int i = 0; i < allCheckedCommunityCount.length; i++) {
        allCheckedCommunityCount[i] = 0;
      }
    } else {
      isAllChecked.value = true;
      communityIds.value = [];
      communities.value = [];
      for (int i = 0; i < communityTree.length; i++) {
        var company = communityTree[i];
        allCheckedCommunityCount[i] += company.communities!.length;

        company.communities!.forEach((commnity) {
              communityIds.add(commnity.communityId);
              communities.add(commnity);
            });
      }
    }
    communities.refresh();
    communityIds.refresh();
    allCheckedCommunityCount.refresh();
  }

  /// 公司全选
  void allCheckCompanyCommunitys(index) async {
    if (isAllCheckedCommunitys.contains(index)) {
      allCheckedCommunityCount[index] = 0;
      isAllCheckedCommunitys.removeWhere((element) => element == index);
      communityTree[index].communities!.forEach((element) {
        communityIds.removeWhere((data) => data == element.communityId);
        communities
            .removeWhere((data) => data.communityId == element.communityId);
      });
    } else {
      allCheckedCommunityCount[index] =
          communityTree[index].communities!.length;
      isAllCheckedCommunitys.add(index);
      communityTree[index].communities!.forEach((element) {
        if (!communityIds.contains(element.communityId)) {
          communityIds.add(element.communityId);
          communities.add(element);
        }
      });
    }
    isAllCheckedCommunitys.refresh();
    communityIds.refresh();
    communities.refresh();
  }

  toStatisticsPage() {
    if (communityIds.length == 0) {
      WToastUtil.show('请先选择小区~');
      return;
    }
    Get.toNamed('statistics/index',
        arguments: {'communitys': communityIds, 'communities': communities});
  }
}
