import 'package:cpt_auth/modules/login/login_page.dart';
import 'package:cpt_auth/modules/reset_psd/reset_psd_page.dart';
import 'package:cpt_auth/modules/setting/dialog/switch_project_dialog.dart';
import 'package:domain/entity/response/index_option_entity.dart';
import 'package:domain/repository/auth_repository.dart';
import 'package:domain/repository/ms_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/dialog/dialog_engine.dart';
import 'package:plugin_platform/engine/notify/notify_engine.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
import 'package:shared/utils/event_bus.dart';
import 'package:widgets/dialog/app_default_dialog.dart';

import 'setting_state.dart';

class SettingController extends GetxController with DioCancelableMixin {
  final AuthRepository _authRepository = Get.find();
  final MSRepository _msRepository = Get.find();
  final SettingState state = SettingState();

  //切换账号
  void switchProjects() async {
    final result = await _msRepository.fetchSwitchProjectList(country: ConfigService.to.selectCountry.value, cancelToken: cancelToken);

    if (result.isSuccess) {
      final projects = result.data?.cutUser;

      if (projects != null && projects.isNotEmpty) {
        _showSwitchProjectDialog(projects);
      }
    } else {
      final errorMessage = result.errorMsg;
      ToastEngine.show(errorMessage ?? "Network Load Error".tr);
    }
  }

  void _showSwitchProjectDialog(List<IndexOptionEntity> projects) {
    DialogEngine.show(
        widget: SwitchProjectDialog(
      options: projects,
      confirmAction: (entity) {
        _requestSwitchProject(entity);
      },
    ));
  }

  void _requestSwitchProject(IndexOptionEntity entity) async {
    final result = await _msRepository.switchProjectSubmit(entity.value, country: ConfigService.to.selectCountry.value, cancelToken: cancelToken);

    if (result.isSuccess) {
      NotifyEngine.showSuccess('successful'.tr);

      final token = result.data?.token;
      UserService.to.setToken(token);

      //发送通知刷新首页
      bus.emit(AppConstant.eventMainRefresh, true);
    } else {
      final errorMessage = result.errorMsg;
      ToastEngine.show(errorMessage ?? "Network Load Error".tr);
    }
  }

  //去重置密码页面
  void gotoResetPasswordPage() {
    ResetPasswordPage.startInstance();
  }

  //确定删除账号
  void doAccountDelete() {
    DialogEngine.show(
        widget: AppDefaultDialog(
      title: "Confirmation".tr,
      message: "Are you sure you want to deactivate your account? You will not be able to login into the app once you proceed with the request.".tr,
      confirmAction: () {
        _requestDeactivate();
      },
    ));
  }

  void _requestDeactivate() async {
    var result = await _authRepository.hotelDeactivate(cancelToken: cancelToken);

    //处理数据
    if (result.isSuccess) {
      //清除数据，去首页
      UserService.to.handleLogoutParams();
      LoginPage.startWithPopAll();
    } else {
      ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
    }
  }

  //确定退出登录
  void doLogout() {
    DialogEngine.show(
        widget: AppDefaultDialog(
      title: "Confirmation".tr,
      message: "Are you sure you need to exit the system?".tr,
      confirmAction: () {
        _requestLogout();
      },
    ));
  }

  /// 请求接口退出账号
  void _requestLogout() async {
    if (ConfigService.to.selectCountry.value == 5) {
      //直接退出
      //清除数据，去首页
      UserService.to.handleLogoutParams();
      LoginPage.startWithPopAll();
    } else {
      //请求接口
      var result = await _authRepository.userLogout(country: ConfigService.to.selectCountry.value, cancelToken: cancelToken);

      //处理数据
      if (result.isSuccess) {
        //清除数据，去首页
        UserService.to.handleLogoutParams();
        LoginPage.startWithPopAll();
      } else {
        ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
      }
    }
  }
}
