import 'dart:convert';

import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/enum_subject.dart';
import 'package:making_school_asignment_app/common/job/user_info.dart';
import 'package:making_school_asignment_app/common/job/user_info_detail.dart';
import 'package:making_school_asignment_app/common/mixins/request_tool_mixin.dart';
import 'package:making_school_asignment_app/common/utils/storage.dart';
import 'package:making_school_asignment_app/common/store/app_storage_key.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

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

  /// 是否登录
  String? token;
  String? xToken;

  /// 用户Token及过期时间信息
  Rx<UserInfo?> userInfo = Rx(null);

  /// 用户详细信息
  Rx<UserInfoDetail?> userDetailInfo = Rx(null);

  //科目
  RxList<EnumSubject> subjectList = RxList();

  //年级
  RxList<EnumSubject> gradeList = RxList();

  UserStore init() {
    token = StorageService.to.read(AppStorageKey.token.value);
    xToken = StorageService.to.read(AppStorageKey.xToken.value);
    try {
      try {
        var _userInfo = StorageService.to.read(AppStorageKey.userInfo.value);
        if (_userInfo != null) {
          print(_userInfo);
          userInfo.value = UserInfo.fromJson(_userInfo);
        }
      } catch (e) {
        print('这里报错了');
      }
      var _userDetail = StorageService.to.read(AppStorageKey.userDetailInfo.value);
      if (_userDetail != null) {
        // print(_userDetail);
        userDetailInfo.value = UserInfoDetail.fromJson(_userDetail);
      }
    } catch (err) {
      // print('LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL${err}');
      StorageService.to.remove(AppStorageKey.userInfo.value);
      StorageService.to.remove(AppStorageKey.userDetailInfo.value);
    }
    if ((token?.isNotEmpty ?? false) && userInfo.value != null) {
    } else {
      Get.toNamed(Routes.login);
    }
    getEnum();
    return this;
  }

  void getEnum() async {
    await getGradeList();
  }

  /// 保存 token
  void setToken(String token) {
    this.token = token;
    StorageService.to.write(AppStorageKey.token.value, token);
  }

  /// 更新Xtoken的匙
  void setXToken(String xtoken) {
    xToken = xtoken;
    StorageService.to.write(AppStorageKey.xToken.value, xtoken);
  }

  /// 保存 用户信息
  void setUserInfo(UserInfo info) {
    userInfo.value = info;
    StorageService.to.write(AppStorageKey.userInfo.value, info);
  }

  /// 保存 用户信息
  void setUserDetailInfo(UserInfoDetail info) {
    userDetailInfo.value = info;
    StorageService.to.write(AppStorageKey.userDetailInfo.value, info);
  }

  void erase() {
    userInfo.value = null;
    userDetailInfo.value = null;
    token = null;
    xToken = null;
  }

  //获取科目
  getSubjectList() async {
    // var res = await getClient().getEnumSubjectList();
  }

  //获取年级
  getGradeList() async {
    var res = await getClient().getEnumSubjectList(['EnumGrade', 'EnumSubject']);
    gradeList.value = res['EnumGrade']!;
    subjectList.value = res['EnumSubject']!;
  }

  // 用户信息更新
  updateUserInfo() async {
    UserInfo? userInfo = UserStore.to.userInfo.value;
    UserInfoDetail? userDetail = await getClient().getUser(userInfo!.nameidentifier);
    if (userDetail != null) setUserDetailInfo(userDetail);
  }
}
