/*
 * @Author       : WuWei
 * @LastEditors: wuwei 
 * @Date         : 2022-01-19 16:44:35
 * @LastEditTime: 2025-03-12 14:39:42
 * @Description  : Do not edit
 */
part of 'index.dart';

class _Store with ChangeNotifier {
  final BuildContext context;
//这是获取路由传参的例子
  late Map args;
  _Store(this.context) {
    args =
        ModalRoute.of(context)!.settings.arguments as Map<dynamic, dynamic>? ??
            {};
  }
  // _Store(this.context);
  final authGlobalStore = GetIt.I.get<AuthGlobalStore>();
  final scorllViewKey = GlobalKey<WisScrollViewState>();

  late Map params = {
    'pageNum': 1,
    'pageSize': 20,
  };

  bool showUpdate = false;

  bool isGetList = false;

  List<MessageIndexListModel> list = [];

  num get billCount => messageStatistics.length > 0
      ? messageStatistics
              .where((element) => element.type == 'BILL')
              .toList()[0]
              .count ??
          0
      : 0;
  num get repairCount => messageStatistics.length > 0
      ? messageStatistics
              .where((element) => element.type == 'REPORT_AND_REPAIR')
              .toList()[0]
              .count ??
          0
      : 0;
  num get faceCount => messageStatistics.length > 0
      ? messageStatistics
              .where((element) => element.type == 'FACE_PASS')
              .toList()[0]
              .count ??
          0
      : 0;
  num get taskCount => messageStatistics.length > 0
      ? messageStatistics
              .where((element) => element.type == 'TASK')
              .toList()[0]
              .count ??
          0
      : 0;

  /// 初始化、刷新当前页
  Future initPage() async {
    checkUpdate();
    getMessageStatistics();
    getList(isLoad: false);
  }

  late AppUpdateModel? data;
  checkUpdate() async {
    final userId = authGlobalStore.info?.id;
    final PackageInfo packageInfo = await AppPackageInfo.instance.getPackageInfo();
    var res = await UpdateApi.getUpdateInfo({
      'version': packageInfo.version,
      'platformType': Platform.isAndroid ? 'android' : 'ios',
      'applicationType': 'hgj',
      'userId': userId,
      'phoneMemory': '',
      'updateStatus': true,
    });

    if (res.data == null) {
      showUpdate = false;
      return;
    }

    data = res.data;

    final update = await _checkUpdate(
      version: data!.version!,
      buildVersion: data!.buildVersion!,
    );

    if (update) {
      showUpdate = true;
    } else {
      showUpdate = false;
    }
    notifyListeners();
  }

 

  _checkUpdate({required String version, required String buildVersion}) async {
    final PackageInfo packageInfo = await AppPackageInfo.instance.getPackageInfo();
    final versionArr = version.split('.').toList();
    final curVersionArr = packageInfo.version.split('.').toList();
    var update = false;
    for (var i = 0; i < versionArr.length; i++) {
      final $0 = int.tryParse(versionArr[i]) ?? 0;
      final $1 = int.tryParse(curVersionArr[i]) ?? 0;

      if ($0 > $1) {
        update = true;
        break;
      }

      ///如果库中版本某一项已经小于当前版本  就没必要继续往下一项判断了
      else if ($0 < $1) {
        break;
      }
    }
    if (!update) {
      if ((int.tryParse(buildVersion) ?? 0) >
          (int.tryParse(packageInfo.buildNumber) ?? 0)) {
        update = true;
      }
    }

    return update;
  }

  List<MessageCountModel> messageStatistics = [];

  /// 获取首页消息统计
  Future<void> getMessageStatistics() async {
    final res = await MessageApi.getMsgGroupCount();
    messageStatistics = res.data ?? [];
    notifyListeners();
  }

  /// 获取首页消息列表
  Future getList({bool isLoad = true}) async {
    var res = await MessageApi.getMessageIndexList();
    final _data = res.data;
    final _list = _data ?? [];
    isGetList = true;
    list = _list;
    notifyListeners();
  }

  /// 跳转具体分类消息列表（type: BILL收费、TASK任务、FACE_PASS人脸、REPORT_AND_REPAIR报事报修, name: 对应名称）
  Future<void> toDetailList(type, name) async {
    Navigator.pushNamed(
      context,
      'notice_list',
      arguments: {
        'appBarTitle': name,
        'type': type,
      },
    );
    await initPage();
  }

  // final pushGlobalStore = GetIt.I.get<PushGlobalStore>();

  /// 报事报修对话框跳转（item: 消息详情）
  toDetailDialog(item) async {
    await MessageApi.putGroupRead({
      'msgId': item.msgId,
    });
    // pushGlobalStore.jumpByType(
    //   item.subType,
    //   {
    //     'id': item.id,
    //     'communityId': item.communityId,
    //     'index': 1,
    //   },
    //   false,
    // );
    await initPage();
  }
}
