import 'package:get/get.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: 'zh',
      name: 'Chinese (Simplified)',
      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: '韩语'),
  ];

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

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

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

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

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