import 'package:cpt_auth/modules/select_country/select_country_page.dart';
import 'package:domain/entity/response/hotel_info_entity.dart';
import 'package:domain/repository/auth_repository.dart';
import 'package:get/get.dart';
import 'package:plugin_basic/constants/app_constant.dart';
import 'package:plugin_basic/service/app_config_service.dart';
import 'package:plugin_basic/service/user_service.dart';
import 'package:plugin_platform/engine/sp/sp_util.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
import 'package:router/componentRouter/component_router_service.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

import '../sign_up/sign_up_page.dart';
import 'login_state.dart';

class LoginController extends GetxController with DioCancelableMixin {
  final AuthRepository authRepository = Get.find();
  final LoginState state = LoginState();

  // 切换隐藏密码框
  void switchPwdVisibility() {
    state.pwdVisibility = !state.pwdVisibility;
    update();
  }

  // 重置电话的错误文本
  void resetCodeErrorText() {
    state.codeErrorText = null;
    update();
  }

  /// 执行账号密码的普通登录
  void doInputLogin() {
    state.codeErrorText = null;
    state.passwordErrorText = null;
    update();

    var codeController = state.formData['code']!['controller'];
    var passwordController = state.formData['password']!['controller'];

    state.code = codeController.text;
    state.password = passwordController.text;

    Log.d('当前待提交的 email:${state.code} password:${state.password}');

    if (Utils.isEmpty(state.code)) {
      state.codeErrorText = "The email cannot be empty!".tr;
      update();
    } else if (Utils.isEmpty(state.password)) {
      state.passwordErrorText = "The password cannot be empty!".tr;
      update();
    } else {
      //执行密码登录
      _request2LoginPassword();
    }
  }

  /// 请求接口执行登录
  void _request2LoginPassword() async {
    var result = await authRepository.userLogin(
      state.code,
      state.password,
      country: ConfigService.to.selectCountry.value,
      cancelToken: cancelToken,
    );

    //处理数据
    if (result.isSuccess) {
      //保存Token，去首页
      _handleLoginSuccess(result.data!);
    } else {
      ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
    }
  }

  /// 登录成功的统一处理 - 去首页
  void _handleLoginSuccess(HotelInfoEntity result) {
    //保存Token
    final token = result.token;
    UserService.to.setToken(token);

    //保存是否是管理员登录
    if (ConfigService.to.selectCountry.value != 0) {
      //如果是新加坡、英国国户，强制是管理员登录
      SPUtil.putInt(AppConstant.storageIsAdmin, 1);
    } else {
      SPUtil.putInt(AppConstant.storageIsAdmin, state.loginOptionPosition);
    }

    if (ConfigService.to.selectCountry.value == 1) {
      //去新加坡首页
      ComponentRouterServices.sgService.startSGMainPage();
    } else if (ConfigService.to.selectCountry.value == 2) {
      //去英国首页
      ComponentRouterServices.ukService.startUKMainPage();
    } else if (ConfigService.to.selectCountry.value == 3) {
      //去马来首页
      ComponentRouterServices.msService.startMSMainPage();
    } else if (ConfigService.to.selectCountry.value == 4) {
      //去荷兰首页
      ComponentRouterServices.nlService.startNLMainPage();
    } else if (ConfigService.to.selectCountry.value == 5) {
      //去泰国首页
      ComponentRouterServices.thService.startTHMainPage();
    } else {
      //去越南首页
      ComponentRouterServices.vnService.startVNMainPage();
    }
  }

  @override
  void onReady() async {
    super.onReady();
  }

  /// 跳转登录页面
  void gotoSignUpPage() {
    SignUpPage.startInstance();
  }

  // 去切换国家的页面
  void gotoSelectCountryPage() {
    SelectCountryPage.startInstance();
  }
}
