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: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: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 {
  @override
  SettingState build() {
    return 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: "Are you sure you want to logout?",
      confirmAction: () {
        //清除用户登录信息
        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: () {
        //请求接口删除账号

        //清除用户登录信息
        UserConfigService.getInstance().handleLogoutParams();
        //清除全部页面栈去登录页面
        ComponentServiceManager().authService.startAndPopAllLoginPage();
      },
    ));
  }

  //评价我们，跳转到各平台的应用商城
  void rateUs() {
    bus.emit(AppConstant.eventProfileRefresh, true);
  }

}
