/*
 * @Author: XianKaiQun
 * @Date: 2020-04-21 08:47:59
 * @LastEditors: wuwei oyoung3231@gmail.com
 * @LastEditTime: 2025-05-23 11:42:55
 * @Descripttion: 
 */
part of 'index.dart';

class _Store with ChangeNotifier {
  final BuildContext context;

  _Store(this.context) {
    Map localInfo = WisStorageUtil.get(key: 'storage-loginInfo') ?? {};
    entCode = localInfo['entCode'] ?? '';
    account = localInfo['account'] ?? '';
    password = localInfo['password'] ?? '';
  }

  // bool isDialogShowed = WisStorageUtil.get(key: 'isDialogShowed') ?? false;

  // GlobalKey<FormState> globalKey() => GlobalKey<FormState>();

  final formKey = GlobalKey<FormState>();
  final authGlobalStore = GetIt.I.get<AuthGlobalStore>();
  String entCode = ''; // 企业号
  String account = ''; //账号
  String password = ''; //密码
  String version = ''; //密码
  bool showPassWord = false; //显示密码？

  setEntCode(v) => entCode = v;

  setAccount(v) => account = v;

  setPassword(v) => password = v;

  setshowPassword() {
    showPassWord = !showPassWord;
    notifyListeners();
  }

  onInit() async {
    version = await getLocalAppVersion();
    notifyListeners();
  }

  Future<String> getLocalAppVersion() async {
    PackageInfo packageInfo = await AppPackageInfo.instance.getPackageInfo();
    return packageInfo.version; // 获取应用的版本号
  }

  /// 默认不勾选隐私协议
  bool isCheck = false;
  // 不同意
  onNotAgree() async {
    isCheck = false;
    await WisStorageUtil.set(key: 'isInstallApp', data: false);
    // await WisStorageUtil.set(key: 'isDialogShowed', data: false);
    notifyListeners();
  }

  onSetAgree(flag) {
    isCheck = flag;
    notifyListeners();
  }

  // 同意
  onAgree() {
    isCheck = true;
    notifyListeners();
    Navigator.of(context).pop();
  }

  /// 勾选框
  onCheck() async {
    isCheck = !isCheck;
    print(isCheck);
    // await WisStorageUtil.set(key: 'isDialogShowed', data: isCheck);
    notifyListeners();
  }

  ///登录
  login() async {
    if (entCode == '') {
      WToastUtil.show(
        '请输入企业号哦~',
        location: WToastLoction.center,
      );
      return;
    }
    if (account == '') {
      WToastUtil.show(
        '请输入账号哦~',
        location: WToastLoction.center,
      );
      return;
    }
    if (password == '') {
      WToastUtil.show(
        '请输入密码哦~',
        location: WToastLoction.center,
      );
      return;
    }
    if (isCheck != true) {
      WToastUtil.show(
        '请先阅读并勾选绘服务个人信息保护政策',
        location: WToastLoction.center,
      );
      return;
    }

    ///通知权限申请
    if (Permission.notification.isGranted != true) {
      final has = await Permission.notification.request();
      print(has);
      final hasPer = await WPermissionUtil.request(
        service: Permission.notification,
        deniedText: '您的通知权限尚未打开，为了及时接收重要消息，请您前往设置开启通知权限',
        guidance: true,
        title: '通知',
        cancelText: '直接登录',
      );
      print(hasPer);
    }
    final publicKey =
        "04e60d11e34bc2050e1ff22cfa4dbf3de25586b4a96a24a82d5cdd871086575bd0b32b569c6092aca803cb079a31a5c905095847f6dfd9d9bef0717a43c3437ce4";

    var cipher = SM2Crypto.encrypt(password, publicKey, c1c3c2Mode: true);
    print('cipher加密: $cipher');

    final success = await authGlobalStore.login(
      entCode: entCode,
      account: account,
      password: cipher, // 传递加密后的密文
    );

    if (success) {
      ///储存账号密码到本地
      await WisStorageUtil.set(key: 'storage-loginInfo', data: {
        'entCode': entCode,
        'account': account,
        'password': password,
      });

      // await WisStorageUtil.set(key: 'isDialogShowed', data: true);
      await WisStorageUtil.set(key: 'isInstallApp', data: true);
      await authGlobalStore.getInfo();

      await WNavUtil.instance!.pushNamedAndRemoveUntil(
        'main',
        (route) => false,
      );
    }
  }
}
