import 'package:get/get.dart';
import 'package:kaer_with_panels/app/localization/kr_language_utils.dart';
import 'package:kaer_with_panels/app/routes/app_pages.dart';
import 'package:kaer_with_panels/app/services/singbox_imp/kr_sing_box_imp.dart';
import 'package:kaer_with_panels/app/utils/kr_country_util.dart';
import '../../../localization/app_translations.dart';
import '../../../themes/kr_theme_service.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:kaer_with_panels/app/common/app_run_data.dart';
import 'package:kaer_with_panels/app/utils/kr_log_util.dart';

class KRSettingController extends GetxController {
  // 创建 AppTranslationsSetting 的实例
  final AppTranslationsSetting kr_appTranslationsSetting =
      AppTranslationsSetting();

  // 当前选择的国家
  final RxString kr_currentCountry = ''.obs;

  // 自动连接开关
  final RxBool kr_autoConnect = true.obs;

  // 通知开关
  final RxBool kr_notification = true.obs;

  // 帮助改进开关
  final RxBool kr_helpImprove = true.obs;

  // 版本号
  final RxString kr_version = ''.obs;

  // IOS评分
  final String kr_iosRating = '';

  // 当前语言
  final RxString kr_language = ''.obs;

  // 当前主题选项
  final RxString kr_themeOption = ''.obs;

  final RxString kr_vpnMode = ''.obs;

  final RxString kr_vpnModeRemark = ''.obs;

  // 修改 VPN 模式切换方法
  void kr_changeVPNMode(String mode) {
    KRLogUtil.kr_i('设置的VPN模式文本: ${kr_vpnMode.value}', tag: 'SettingController');
  }

  // 切换语言
  void kr_changeLanguage() {
    Get.toNamed(Routes.KR_LANGUAGE_SELECTOR);
  }

  // 删除账号
  void kr_deleteAccount() {
    // 检查是否已登录
    if (!KRAppRunData.getInstance().kr_isLogin.value) {
      // 如果未登录，跳转到登录页面
      // Get.toNamed(Routes.MR_LOGIN);
      return;
    }
    // 已登录，跳转到删除账号页面
    Get.toNamed(Routes.KR_DELETE_ACCOUNT);
  }

  @override
  void onInit() {
    super.onInit();
    _loadThemeOption();
    kr_language.value = KRLanguageUtils.getCurrentLanguage().languageName;

    // 语言变化时更新所有翻译文本
    ever(KRLanguageUtils.kr_language, (_) {
      kr_language.value = KRLanguageUtils.kr_language.value;
      _loadThemeOption();

      kr_currentCountry.value = "";
      kr_currentCountry.value = KRCountryUtil.kr_getCurrentCountryName();

      kr_vpnMode.value = '';
      kr_vpnMode.value =
          kr_getConnectionTypeString(KRSingBoxImp().kr_connectionType.value);

       kr_vpnModeRemark.value = '';
      kr_vpnModeRemark.value = kr_getConnectionTypeRemark(KRSingBoxImp().kr_connectionType.value);
    });

    ever(KRCountryUtil.kr_currentCountry, (_) {
      kr_currentCountry.value = KRCountryUtil.kr_getCurrentCountryName();
    });

    kr_currentCountry.value = KRCountryUtil.kr_getCurrentCountryName();
    kr_vpnMode.value =
        kr_getConnectionTypeString(KRSingBoxImp().kr_connectionType.value);
    kr_vpnModeRemark.value = kr_getConnectionTypeRemark(KRSingBoxImp().kr_connectionType.value);
    _kr_getVersion();
  }

  String kr_getConnectionTypeString(KRConnectionType type) {
    switch (type) {
      case KRConnectionType.global:
        return AppTranslations.kr_setting.connectionTypeGlobal;
      case KRConnectionType.rule:
        return AppTranslations.kr_setting.connectionTypeRule;
      // case KRConnectionType.direct:
      //   return AppTranslations.kr_setting.connectionTypeDirect;
    }
  }

   String kr_getConnectionTypeRemark(KRConnectionType type) {
    
    switch (type) {
      case KRConnectionType.global:
        return AppTranslations.kr_setting.connectionTypeGlobalRemark;
      case KRConnectionType.rule:
        return AppTranslations.kr_setting.connectionTypeRuleRemark;
      // case KRConnectionType.direct:
      //   return AppTranslations.kr_setting.connectionTypeDirectRemark;
    }
  }

  void _loadThemeOption() async {
    final KRThemeService themeService = KRThemeService();
    await themeService.init();

    switch (themeService.kr_Theme) {
      case ThemeMode.system:
        kr_themeOption.value = AppTranslations.kr_setting.system;
        break;
      case ThemeMode.light:
        kr_themeOption.value = AppTranslations.kr_setting.light;
        break;
      case ThemeMode.dark:
        kr_themeOption.value = AppTranslations.kr_setting.dark;
        break;
    }
  }

  final count = 0.obs;

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  void kr_updateConnectionType(KRConnectionType newType) {
    if (KRSingBoxImp().kr_connectionType.value != newType) {
      KRLogUtil.kr_i('更新连接类型: $newType', tag: 'SettingController');
      KRSingBoxImp().kr_updateConnectionType(newType);
      kr_vpnMode.value = kr_getConnectionTypeString(newType);
      kr_vpnModeRemark.value = kr_getConnectionTypeRemark(newType);
      // 这里可以添加其他需要的逻辑
    }
  }

  // 获取版本号
  Future<void> _kr_getVersion() async {
    final PackageInfo packageInfo = await PackageInfo.fromPlatform();
    kr_version.value = packageInfo.version;
  }
}
