import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';

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

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

  // 自动重连开关
  final _autoReconnect = true.obs;
  bool get autoReconnect => _autoReconnect.value;
  set autoReconnect(bool value) => _autoReconnect.value = value;

  final _pushNotifications = false.obs;
  bool get pushNotifications => _pushNotifications.value;
  set pushNotifications(bool value) => _pushNotifications.value = value;

  final _version = ''.obs;
  String get version => _version.value;
  set version(String value) => _version.value = value;

  final _hasUpdate = false.obs;
  bool get hasUpdate => _hasUpdate.value;
  set hasUpdate(bool value) => _hasUpdate.value = value;

  final _themeMode = ''.obs;
  String get themeMode => _themeMode.value;
  set themeMode(String value) => _themeMode.value = value;

  @override
  void onInit() {
    super.onInit();
    initThemeMode();
    // _initPushNotifications();
    _getVersion();
  }

  void initThemeMode() {
    themeMode = _getThemeModeText();
  }

  /// 获取当前主题模式文本
  String _getThemeModeText() {
    final mode = IXSP.getThemeMode();
    switch (mode) {
      case 'light':
        return Strings.lightMode.tr;
      case 'dark':
        return Strings.darkMode.tr;
      case 'system':
      default:
        return Strings.followSystem.tr;
    }
  }

  /// 获取版本信息
  void _getVersion() async {
    final packageInfo = await PackageInfo.fromPlatform();
    version = packageInfo.version;
    final code = int.tryParse(packageInfo.buildNumber) ?? 0;
    final upgrade = IXSP.getUpgrade();
    if (upgrade?.versionCode == code) {
      hasUpdate = false;
    } else {
      hasUpdate = upgrade?.upgradeType == 1;
    }
  }

  Future<void> _initPushNotifications() async {
    pushNotifications =
        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();
      },
    );
  }
}
