import 'package:get/get.dart';
import 'package:kaer_with_panels/app/localization/kr_language_utils.dart';

class KRLanguageSelectorController extends GetxController {
  // 使用 KRLanguage 枚举来加载语言
  final RxList<KRLanguage> kr_languages = <KRLanguage>[].obs;
  // 当前选中的语言代码
  final RxString kr_selectedLanguage = ''.obs;

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

    kr_selectedLanguage.value = KRLanguageUtils.getCurrentLanguage().countryCode;
    kr_loadLanguages();
  }

  // 加载语言数据
  void kr_loadLanguages() {
    // 将英语放在前面
    final sortedLanguages = KRLanguage.values.toList()
      ..sort((a, b) => a == KRLanguage.en ? -1 : 1);

    kr_languages.value = sortedLanguages;
  }

  // 选择语言
  Future<void> kr_selectLanguage(KRLanguage language) async {
    try {
      // 先更新选中状态
      kr_selectedLanguage.value = language.countryCode;
      // 然后切换语言
      await KRLanguageUtils.switchLanguage(language);
    } catch (err) {
      Get.snackbar(
        '错误',
        '切换语言失败: $err',
        snackPosition: SnackPosition.BOTTOM,
      );
    }
  }
} 