import 'package:get/get.dart';

import '../../../../config/translations/localization_service.dart';

class LanguageInfo {
  final String code;
  final String name;
  final String nativeName;

  LanguageInfo({
    required this.code,
    required this.name,
    required this.nativeName,
  });
}

class LanguageController extends GetxController {
  // 当前选中的语言，默认为英语
  final selectedLanguage = 'en'.obs;

  // 支持的语言列表
  final List<LanguageInfo> languages = [
    LanguageInfo(code: 'en', name: 'English', nativeName: '英语'),
    // LanguageInfo(code: 'es', name: 'Español', nativeName: '西班牙语'),
    // LanguageInfo(code: 'fr', name: 'Français', nativeName: '法语'),
    // LanguageInfo(code: 'de', name: 'Deutsch', nativeName: '德语'),
    // LanguageInfo(code: 'ja', name: '日本語', nativeName: '日语'),
    // LanguageInfo(code: 'ko', name: '한국어', nativeName: '韩语'),
    // LanguageInfo(code: 'fa', name: 'فارسی', nativeName: '波斯语'),
    // LanguageInfo(code: 'my', name: 'မြန်မာဘာသာ', nativeName: '缅甸语'),
    // LanguageInfo(code: 'ar', name: 'عربي', nativeName: '阿拉伯语'),
    // LanguageInfo(code: 'ru', name: 'Русский', nativeName: '俄语'),
    LanguageInfo(code: 'zh_CN', name: '简体中文', nativeName: '简体中文'),
    // LanguageInfo(code: 'zh_TW', name: '繁體中文', nativeName: '繁体中文'),
    // LanguageInfo(code: 'tk', name: 'Türkmençe', nativeName: '土库曼语'),
    // LanguageInfo(
    //   code: 'pt',
    //   name: 'Português (Brasil)',
    //   nativeName: '葡萄牙语 (巴西)',
    // ),
    // LanguageInfo(code: 'vi', name: 'Tiếng Việt', nativeName: '越南语'),
    // LanguageInfo(code: 'id', name: 'Bahasa Indonesia', nativeName: '印尼语'),
    // LanguageInfo(code: 'tl', name: 'Filipino', nativeName: '菲律宾语'),
    // LanguageInfo(code: 'th', name: 'ไทย', nativeName: '泰语'),
    // LanguageInfo(code: 'hi', name: 'हिन्दी', nativeName: '印地语'),
    // LanguageInfo(code: 'tr', name: 'Türkçe', nativeName: '土耳其语'),
  ];

  @override
  void onInit() {
    super.onInit();
    final locale = LocalizationService.getCurrentLocal();
    // 中文需要区分简体和繁体
    if (locale.languageCode == 'zh') {
      selectedLanguage.value = locale.countryCode == 'TW' ? 'zh_TW' : 'zh_CN';
    } else {
      selectedLanguage.value = locale.languageCode;
    }
  }

  /// 选择语言
  void selectLanguage(String languageCode) {
    selectedLanguage.value = languageCode;
    LocalizationService.updateLanguage(languageCode);
  }

  /// 获取当前选中的语言信息
  LanguageInfo? get currentLanguage {
    try {
      return languages.firstWhere(
        (lang) => lang.code == selectedLanguage.value,
      );
    } catch (e) {
      return languages.first; // 默认返回第一个
    }
  }
}
