import 'package:flutter/material.dart';
import 'package:get/get.dart';
// import 'package:google_fonts/google_fonts.dart';

import '../../app/data/sp/ix_sp.dart';
import 'ar_AR/ar_ar_translation.dart';
import 'en_US/en_us_translation.dart';
import 'es_ES/es_es_translation.dart';
import 'my_MM/my_mm_translation.dart';
import 'fa_IR/fa_ir_translation.dart';
import 'de_DE/de_de_translation.dart';
import 'ja_JP/ja_jp_translation.dart';
import 'fr_FR/fr_fr_translation.dart';
import 'ru_RU/ru_ru_translation.dart';
import 'ko_KR/ko_kr_translation.dart';
import 'strings_enum.dart';

class LocalizationService extends Translations {
  // prevent creating instance
  LocalizationService._();

  static LocalizationService? _instance;

  static LocalizationService getInstance() {
    _instance ??= LocalizationService._();
    return _instance!;
  }

  // 根据系统语言获取默认语言
  static Locale getDefaultLanguage() {
    // 获取系统语言代码
    final String deviceLanguage =
        WidgetsBinding.instance.platformDispatcher.locale.languageCode;

    // 检查系统语言是否在支持的语言列表中
    if (isLanguageSupported(deviceLanguage)) {
      return supportedLanguages[deviceLanguage]!;
    }

    // 如果系统语言不受支持，则返回英语作为默认语言
    return supportedLanguages['en']!;
  }

  // 获取系统国家
  static String getSystemCountry() {
    return WidgetsBinding.instance.platformDispatcher.locale.countryCode ?? '';
  }

  // default language
  // todo change the default language
  static Locale defaultLanguage = getDefaultLanguage();

  static final all = [
    const Locale('en'), // 英语
    const Locale('es'), // 西班牙语
    const Locale('ar'), // 阿拉伯语
    const Locale('de'), // 德语
    const Locale('ja'), // 日语
    const Locale('fr'), // 法语
    const Locale('ru'), // 俄语
    const Locale('ko'), // 韩语
    const Locale('fa'), // 波斯语
    const Locale('my'), // 缅甸语
  ];

  // supported languages
  static Map<String, Locale> supportedLanguages = {
    'en': const Locale('en', 'US'),
    'es': const Locale('es', 'ES'),
    'ar': const Locale('ar', 'AR'),
    'de': const Locale('de', 'DE'),
    'ja': const Locale('ja', 'JP'),
    'fr': const Locale('fr', 'FR'),
    'ru': const Locale('ru', 'RU'),
    'ko': const Locale('ko', 'KR'),
    'fa': const Locale('fa', 'IR'),
    'my': const Locale('my', 'MM'),
  };

  // supported languages fonts family (must be in assets & pubspec yaml) or you can use google fonts
  static Map<String, TextStyle> supportedLanguagesFontsFamilies = {
    // 'en': GoogleFonts.poppins(),
    // 'es': GoogleFonts.poppins(),
    // 'ar': GoogleFonts.cairo(),
    // 'fa': GoogleFonts.vazirmatn(),
    // 'my': GoogleFonts.notoSansMyanmar(),
    'en': const TextStyle(fontFamily: 'Inter'),
    'es': const TextStyle(fontFamily: 'FiraSans'),
    'ar': const TextStyle(fontFamily: 'Vazirmatn'),
    'de': const TextStyle(fontFamily: 'FiraSans'),
    'ja': const TextStyle(fontFamily: 'FiraSans'),
    'fr': const TextStyle(fontFamily: 'FiraSans'),
    'ru': const TextStyle(fontFamily: 'FiraSans'),
    'ko': const TextStyle(fontFamily: 'FiraSans'),
    'fa': const TextStyle(fontFamily: 'Vazirmatn'),
    'my': const TextStyle(fontFamily: 'NotoSansMyanmar'),
  };

  @override
  Map<String, Map<String, String>> get keys => {
    'en_US': enUs,
    'es_ES': esEs,
    'ar_AR': arAR,
    'de_DE': deDE,
    'ja_JP': jaJP,
    'fr_FR': frFR,
    'ru_RU': ruRU,
    'ko_KR': koKR,
    'fa_IR': faIR,
    'my_MM': myMM,
  };

  /// check if the language is supported
  static isLanguageSupported(String languageCode) =>
      supportedLanguages.keys.contains(languageCode);

  /// update app language by code language for example (en,ar..etc)
  static updateLanguage(String languageCode) async {
    // check if the language is supported
    if (!isLanguageSupported(languageCode)) return;
    // update current language in shared pref
    await IXSP.setCurrentLanguage(languageCode);
    if (!Get.testMode) {
      Get.updateLocale(supportedLanguages[languageCode]!);
    }
  }

  /// check if the language is english
  static bool isItEnglish() =>
      IXSP.getCurrentLocal().languageCode.toLowerCase().contains('en');

  /// get current locale
  static Locale getCurrentLocal() => IXSP.getCurrentLocal();

  /// 检测指定的[languageCode]是否从右往左(RTL)布局
  static bool isRTL() {
    switch (getCurrentLocal().languageCode) {
      case 'ar':
      case 'fa':
      case 'he':
        return true;
      default:
        return false;
    }
  }

  /// 获取本地化配置[locale]中的语言简称
  static String getGlobalLanguageTitle({Locale? locale}) {
    final languageCode = (locale ?? getCurrentLocal()).languageCode
        .toLowerCase();
    switch (languageCode) {
      case 'en':
        return Strings.enLang.tr;
      case 'es':
        return Strings.esLang.tr;
      case 'ar':
        return Strings.arLang.tr;
      case 'de':
        return Strings.deLang.tr;
      case 'ja':
        return Strings.jaLang.tr;
      case 'fr':
        return Strings.frLang.tr;
      case 'ru':
        return Strings.ruLang.tr;
      case 'ko':
        return Strings.koLang.tr;
      case 'fa':
        return Strings.faLang.tr;
      case 'my':
        return Strings.myLang.tr;
      default:
        return 'Unknown';
    }
  }

  /// 获取本地化配置[locale]中的语言全称
  static String getGlobalLanguageName({Locale? locale}) {
    final languageCode = (locale ?? getCurrentLocal()).languageCode
        .toLowerCase();
    switch (languageCode) {
      case 'en':
        return 'English';
      case 'tr':
        return 'Türk';
      case 'vi':
        return 'Tiếng Việt';
      case 'uk':
        return 'Українська';
      case 'es':
        return 'Español';
      case 'ru':
        return 'Русский';
      case 'pt':
        return 'Português';
      case 'pl':
        return 'Polski';
      case 'ko':
        return '한국어';
      case 'ja':
        return '日本語';
      case 'it':
        return 'Italiano';
      case 'id':
        return 'Bahasa Indonesia';
      case 'de':
        return 'Deutsch';
      case 'fr':
        return 'Français';
      case 'fa':
        return 'فارسی';
      case 'hi':
        return 'हिन्दी';
      case 'ar':
        return 'عربي';
      case 'zh':
        return '繁體中文';
      case 'my':
        return 'မြန်မာဘာသာ';
      default:
        return 'Unknown';
    }
  }
}
