import 'package:get/get.dart';

import '../../../../config/translations/strings_enum.dart';
import '../../../../utils/awesome_notifications_helper.dart';
import '../../../controllers/api_controller.dart';
import '../../../dialog/loading/loading_dialog.dart';

class SettingController extends GetxController {
  final _apiController = Get.find<ApiController>();

  // 自动重连开关
  final autoReconnect = true.obs;

  final pushNotifications = false.obs;

  final isPremium = true.obs;

  @override
  void onInit() {
    super.onInit();
    initPushNotifications();
  }

  Future<void> initPushNotifications() async {
    pushNotifications.value =
        await AwesomeNotificationsHelper.checkNotificationPermission();
  }

  Future<void> showNotificationConfigPage() async {
    await AwesomeNotificationsHelper.showNotificationConfigPage();
    initPushNotifications();
  }

  // 处理退出登录
  Future<void> handleLogout() async {
    await LoadingDialog.show(
      context: Get.context!,
      loadingText: Strings.loggingOut.tr,
      successText: Strings.logoutSuccessful.tr,
      onRequest: () async {
        // 执行你的异步请求
        await _apiController.logout();
      },
      onSuccess: () {
        // 成功后的操作
        Get.back();
      },
    );
  }

  // 处理删除账户
  Future<void> handleDeleteAccount() async {
    await LoadingDialog.show(
      context: Get.context!,
      loadingText: Strings.deletingAccount.tr,
      successText: Strings.deleteAccountSuccessful.tr,
      onRequest: () async {
        // 执行你的异步请求
        await _apiController.deleteAccount();
      },
      onSuccess: () {
        // 成功后的操作
        Get.back();
      },
    );
  }
}
