using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
using System.Globalization;
using RedHotRoast;

public static class Language
{
    private static Dictionary<string, Dictionary<string, string>> _localizedText;
    private static string _currentLanguage;

    public static void Initialize()
    {
        var lang = PlayerPrefsKit.ReadString("LangIdKey");
        if (lang.IsNullOrWhiteSpace())
        { 
            var systemLanguage = Application.systemLanguage;
            if (systemLanguage == SystemLanguage.English)
            {
                lang = "en";
            }
            else if (systemLanguage == SystemLanguage.French)
            {
                lang = "fr";
            }
            else if (systemLanguage == SystemLanguage.German)
            {
                lang = "de";
            }
            else if (systemLanguage == SystemLanguage.Spanish)
            {
                lang = "es";
            }
            else if (systemLanguage == SystemLanguage.Portuguese)
            {
                lang = "pt";
            }
            else if (systemLanguage == SystemLanguage.Japanese)
            {
                lang = "ja";
            }
            else if (systemLanguage == SystemLanguage.Korean)
            {
                lang = "ko";
            }
            else if (systemLanguage == SystemLanguage.Russian)
            {
                lang = "ru";
            } else
            {
                lang = "en";
            }
        }

        setCurrentLanguage(lang);
    }

    public static void setCurrentLanguage(string lang)
    {
        _currentLanguage = lang;
        PlayerPrefsKit.WriteString("LangIdKey", lang);
        UIManager.Instance.SetSwitchLanguage(lang);
    }

    public static string getCurrentLanguage()
    {
        return _currentLanguage;
    }

    public static void LoadLocalizedText()
    {
        if (_localizedText == null)
        {
            _localizedText = new Dictionary<string, Dictionary<string, string>>();
        }

        var localization = LoadKit.Instance.LoadAsset<TextAsset>("TextAsset", "localization");
        string json = localization.text;
        _localizedText = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(json);
    }

    public static string GetContent(string key)
    {
        if (_localizedText == null || !_localizedText.ContainsKey(_currentLanguage) || !_localizedText[_currentLanguage].ContainsKey(key))
        {
            return key; // 如果找不到对应的键，返回键本身
        }

        return _localizedText[_currentLanguage][key];
    }

    public static string GetContentParams(string key, params object[] args)
    {
        if (_localizedText == null || !_localizedText.ContainsKey(_currentLanguage) || !_localizedText[_currentLanguage].ContainsKey(key))
        {
            return key; // 如果找不到对应的键，返回键本身
        }

        string localizedString = _localizedText[_currentLanguage][key];
        return string.Format(localizedString, args);
    }

    public static string getISOCode(SystemLanguage code)
    {
        if (code == SystemLanguage.English)
        {
            return "en";
        }
        else if (code == SystemLanguage.French)
        {
            return "fr";
        }
        else if (code == SystemLanguage.German)
        {
            return "de";
        }
        else if (code == SystemLanguage.Spanish)
        {
            return "es";
        }
        else if (code == SystemLanguage.Portuguese)
        {
            return "pt";
        }
        else if (code == SystemLanguage.Japanese)
        {
            return "ja";
        }
        else if (code == SystemLanguage.Korean)
        {
            return "ko";
        }
        else if (code == SystemLanguage.Russian)
        {
            return "ru";
        }
        return "en";
    }

    public static SystemLanguage GetSystemLanguageCode(string ISO_Code)
    {
        if (ISO_Code == "fr") {
            return SystemLanguage.French;
        } else if (ISO_Code == "de") {
            return SystemLanguage.German;
        } else if (ISO_Code == "es") {
            return SystemLanguage.Spanish;
        } else if (ISO_Code == "ja") {
            return SystemLanguage.Japanese;
        } else if (ISO_Code == "ko") {
            return SystemLanguage.Korean;
        } else if (ISO_Code == "ru") {
            return SystemLanguage.Russian;
        } else if (ISO_Code == "pt") {
            return SystemLanguage.Portuguese;
        } else {
            return SystemLanguage.English;
        }
    }

}