import 'package:cpt_profile/modules/change_mobile/change_mobile_page.dart';
import 'package:cpt_profile/modules/reset_password/reset_password_page.dart';
import 'package:cpt_profile/modules/setting/dialog/account_deactivation_dialog.dart';
import 'package:cpt_profile/modules/setting/setting_state.dart';
import 'package:cs_resources/generated/l10n.dart';
import 'package:domain/repository/auth_repository.dart';
import 'package:plugin_basic/constants/app_constant.dart';
import 'package:plugin_basic/provider/user_config/user_config_service.dart';
import 'package:plugin_platform/engine/dialog/dialog_engine.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:riverpod_annotation/riverpod_annotation.dart';
import 'package:router/componentRouter/component_service_manager.dart';
import 'package:shared/utils/event_bus.dart';
import 'package:widgets/dialog/app_default_dialog.dart';

part 'setting_view_model.g.dart';

@riverpod
class SettingViewModel extends _$SettingViewModel with DioCancelableMixin {
  late final AuthRepository _authRepository;

  @override
  SettingState build() {
    _authRepository = ref.read(authRepositoryProvider);
    return const SettingState();
  }

  void changeEnableNotification(bool value) {
    state = state.copyWith(enbaleNofitication: value);
  }

  //去修改手机号码的页面
  void gotoChangeMobilePage() {
    ChangeMobilePage.startInstance();
  }

  void gotoResetPasswordPage() {
    ResetPasswordPage.startInstance();
  }

  // 退出登录
  void doLogout() {
    DialogEngine.show(
        widget: AppDefaultDialog(
      message: S.current.logout_alert,
      confirmAction: () {
        _requestLogout();
      },
    ));
  }

  /// 调用接口注退出登录
  void _requestLogout() async {
    final result = await _authRepository.authLogout(cancelToken: cancelToken);
    //请求成功去首页
    if (result.isSuccess) {
      //清除用户登录信息
      _gotoLoginPage();
    } else {
      ToastEngine.show(result.errorMsg ?? "UnKnow Error");
    }
  }

  void _gotoLoginPage() {
    UserConfigService.getInstance().handleLogoutParams();
    // 清除 garagesale category 缓存
    SPUtil.remove(AppConstant.storageGarageCategoryList);
    // 清除 servives category 缓存
    SPUtil.remove(AppConstant.storagePaidServiceCategoryList);
    SPUtil.remove(AppConstant.storageRepairServiceCategoryList);
    //清除全部页面栈去登录页面
    ComponentServiceManager().authService.startAndPopAllLoginPage();
  }

  //删除账号的提示弹窗
  void doDeleteAccount() {
    DialogEngine.show(widget: AccountDeactivationDialog(
      confirmAction: () {
        //请求接口删除账号
        _requestAccountDeactivate();
      },
    ));
  }

  //请求删除账号
  void _requestAccountDeactivate() async {
    final result = await _authRepository.accountDeactivate(cancelToken: cancelToken);
    //请求成功去首页
    if (result.isSuccess) {
      //清除用户登录信息
      _gotoLoginPage();
    } else {
      ToastEngine.show(result.errorMsg ?? "UnKnow Error");
    }
  }

  //评价我们，跳转到各平台的应用商城
  void rateUs() {

  }
}
