import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:wgshare/common/mixins/request_tool_mixin.dart';
import 'package:wgshare/utils/device_info.dart';
import 'package:wgshare/utils/toast_utils.dart';

import '../../common/config/app_config.dart';
import '../../common/models/common/base_structure_result.dart';
import '../../common/models/user_info_entity.dart';
import '../../common/store/user_store.dart';
import '../../routes/app_routes.dart';
import '../../utils/permission/PermissionService.dart';
import '../../view/public_dialog.dart';
import '../../view/upgrade/loadJson/hide_load_network_json.dart';
import '../../view/upgrade/loadJson/load_network_json.dart';
import 'login_state.dart';

class LoginLogic extends GetxController with RequestToolMixin {
  late LoginState state;

  @override
  void onInit() {
    print("初始化登录init................");
    state = LoginState();
    HideCheckVersion hideCheckVersion = HideCheckVersion();
    hideCheckVersion.doHttpHideCheckVersion();
    super.onInit();
  }

  @override
  void onClose() {
    print("销毁onClose................");
   
    super.onClose();
  }

  /// 改变页面状态
  void changePageState(int pageState){
    state.pageState.value = pageState;
  }

  /// 改变勾选协议状态
  void changeAgreementState(bool checkAgreementBool) {
    state.checkAgreementBool.value = checkAgreementBool;
  }

  /// 登录
  Future<void> doHttpLogin() async {
    if(state.userNameController!.text.isEmpty){
      ToastUtils.showError("请输入账号");
    }else if(state.passwordController!.text.isEmpty){
      ToastUtils.showError("请输入密码");
    }else if(state.checkAgreementBool.value != true){
      ToastUtils.showError("请阅读并勾选相关协议");
    }else{
      BaseStructureResult<UserInfoEntity> res = await getClient().login(state.userNameController!.text, md5.convert(utf8.encode(state.passwordController!.text)).toString());
      if (null != res.data) {
        UserStore.to.setToken(res.data!.token);
        UserStore.to.setRefreshToken(res.data!.refreshToken);
        UserStore.to.setUserDetailInfo(res.data!);
        UserStore.to.setLoginType(AppConfig.NORMAL_LOGIN);
        Get.offAllNamed(Routes.startPage);
        state.userNameController!.text = "";
        state.passwordController!.text = "";
        state.meetingCodeController!.text = "";
        state.nickNameCodeController!.text = "";
        state.checkAgreementBool.value = false;
      }
    }
  }

  /// 直接进入会议（匿名登录）
  Future<void> doHttpAnonymousLogin() async {
    if(state.meetingCodeController!.text.isEmpty){
      ToastUtils.showError("请输入会议号");
    }else if(state.meetingCodeController!.text.length != 8){
      ToastUtils.showError("请输入正确的会议号");
    }else if(state.nickNameCodeController!.text.isEmpty){
      ToastUtils.showError("请输入昵称");
    }else if(state.checkAgreementBool != true){
      ToastUtils.showError("请阅读并勾选相关协议");
    }else{
      BaseStructureResult<UserInfoEntity> res = await getClient().anonLogin(await DeviceInfo.getDeviceId(),state.nickNameCodeController!.text, state.meetingCodeController!.text);
      if (null != res.data) {
        UserStore.to.setToken(res.data!.token);
        UserStore.to.setRefreshToken(res.data!.refreshToken);
        UserStore.to.setUserDetailInfo(res.data!);
        UserStore.to.setLoginType(AppConfig.ANONYMOUS_LOGIN);
        Get.toNamed(Routes.meetingMainPage, arguments: {"roomNumber": state.meetingCodeController!.text});
        state.userNameController!.text = "";
        state.passwordController!.text = "";
        state.meetingCodeController!.text = "";
        state.nickNameCodeController!.text = "";
        state.checkAgreementBool.value = false;
      }
    }
  }

  /// 检验房间
  Future<void> doHttpCheckMeetingRoom() async {
    if(state.meetingCodeController!.text.isEmpty){
      ToastUtils.showError("请输入会议号");
    }else if(state.meetingCodeController!.text.length != 8){
      ToastUtils.showError("请输入正确的会议号");
    }else if(state.nickNameCodeController!.text.isEmpty){
      ToastUtils.showError("请输入昵称");
    }else if(state.checkAgreementBool != true){
      ToastUtils.showError("请阅读并勾选相关协议");
    }else{
      BaseStructureResult<bool> res = await getClient().checkout(state.meetingCodeController!.text);
      if (null != res.data) {
        if(res.data == true){
          doHttpAnonymousLogin();
        }else{
          ToastUtils.showError("房间不存在");
        }
      }
    }
  }
}
