import 'package:cpt_auth/modules/login/login_page.dart';
import 'package:cpt_auth/modules/reset_psd/reset_psd_page.dart';
import 'package:domain/repository/auth_repository.dart';
import 'package:get/get.dart';
import 'package:plugin_basic/service/user_service.dart';
import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/http/dio/dio_cancelable_mixin.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 SettingState state = SettingState();

  //切换账号
  void switchProjects() {
    ToastEngine.show("切换账号");
  }

  //去重置密码页面
  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 {
    var result = await _authRepository.userLogout(cancelToken: cancelToken);

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