using System.Collections.Generic;
using FairyGUI;
using TMPro;
using UnityEngine;

namespace NBC
{
    public class LanguageFont : ILanguage
    {
        /// <summary>
        /// 多语言字体配置数据
        /// </summary>
        private readonly Dictionary<SystemLanguage, Dictionary<string, string>> _languages =
            new Dictionary<SystemLanguage, Dictionary<string, string>>();

        /// <summary>
        /// 当前语言和字体
        /// </summary>
        private Dictionary<string, string> _currentLanguageDictionary = new Dictionary<string, string>();

        public string Get(string key)
        {
            if (_currentLanguageDictionary != null && _currentLanguageDictionary.TryGetValue(key, out var value))
            {
                return value;
            }

            return null;
        }

        public void AddLanguage(SystemLanguage language)
        {
            var configDic = Lan.Inst.GetLanguageFontConfig(LanguageConst.languageMap[language]);
            // string fontLocation = "Fonts/";//"Assets/Resources/Fonts/";
            //字体使用前需要先注册
            foreach (var config in configDic.Values)
            {
                // if (config.Type == "1")
                // {
                //     DynamicFont font = new DynamicFont();
                //     font.name = config.Name;
                //     font.nativeFont = Resources.Load<Font>(fontLocation + config.RelativePath);
                //     FontManager.RegisterFont(font);
                // }
                // else if (config.Type == "2")
                // {
                //     TMP_FontAsset fontAsset =
                //         Resources.Load<TMP_FontAsset>(fontLocation + config.RelativePath) as TMP_FontAsset;
                //     TMPFont font = new TMPFont();
                //     font.name = config.Name; //这个名字要和编辑器里字体资源的名字一致
                //     font.fontAsset = fontAsset;
                //     FontManager.RegisterFont(font);
                // }
            }

            var font2KeyDic = Lan.Inst.GetFont2KeyDic();
            Dictionary<string, string> keys = new Dictionary<string, string>();
            foreach (var pair in font2KeyDic)
            {
                keys.Add(pair.Key, configDic[pair.Value].Name);
            }
            
            _languages[language] = keys;
        }

        public bool UseLanguage(SystemLanguage language)
        {
            if (_languages.TryGetValue(language, out var language1))
            {
                _currentLanguageDictionary = language1;
                return true;
            }

            Debug.LogError("字体配置不存在");
            return false;
        }
    }
}