import 'package:get/get.dart';
import 'package:wgshare/common/mixins/request_tool_mixin.dart';
import 'package:wgshare/common/store/app_storage_key.dart';
import 'package:wgshare/utils/storage.dart';

import '../models/app_updata_info_entity.dart';

class BusinessStore extends GetxController with RequestToolMixin {
  static BusinessStore get to => Get.find();

  /// app版本更新信息
  Rx<AppUpdataInfoEntity?> appUpdataInfoEntity = Rx(null);

  /// 是否拒绝会议列表页请求文件访问权限
  bool? isRefuseHomeCheckPermission = false;

  /// 跳过此版本更新的版本号
  int? skipUpVersion = 0;

  BusinessStore init() {
    isRefuseHomeCheckPermission = StorageService.to.read(AppStorageKey.isRefuseHomeCheckPermission.value);
    skipUpVersion = StorageService.to.read(AppStorageKey.skipUpVersion.value);
    try {
      var appUpdataInfo = StorageService.to.read(AppStorageKey.appUpDataInfo.value);
      if (appUpdataInfo != null) {
        appUpdataInfoEntity.value = AppUpdataInfoEntity.fromJson(appUpdataInfo);
      }
    } catch (err) {
      StorageService.to.remove(AppStorageKey.userInfo.value);
      appUpdataInfoEntity.value = null;
    }
    return this;
  }

  /// 缓存app版本更新信息
  void setAppUpdataInfo(AppUpdataInfoEntity info) {
    appUpdataInfoEntity.value = info;
    StorageService.to.write(AppStorageKey.appUpDataInfo.value, info);
  }

  /// 缓存是否拒绝会议列表页请求文件访问权限状态
  void setIsRefuseHomeCheckPermission(bool type) {
    isRefuseHomeCheckPermission = type;
    StorageService.to.write(AppStorageKey.isRefuseHomeCheckPermission.value, type);
  }

  /// 跳过此版本更新的版本号
  void setSkipUpVersion(int code) {
    skipUpVersion = code;
    StorageService.to.write(AppStorageKey.skipUpVersion.value, code);
  }

  /// 清空缓存app版本更新信息
  void erase() {
    appUpdataInfoEntity.value = null;
    StorageService.to.write(AppStorageKey.appUpDataInfo.value, null);
  }
}
