using System.Collections.Generic;
using FairyGUI;

namespace NBC
{
    public class UIComponentLanguage : Dictionary<string, string>
    {
    }

    public abstract class UIComponentLanguagePack : Dictionary<string, UIComponentLanguage>
    {
        public bool Has(string url)
        {
            return ContainsKey(url);
        }

        public void TrySetComponentLanguage(GComponent component)
        {
            if (component.packageItem == null) return;
            SetComponentFont(component);
            if (!Has(component.resourceURL)) return;
            SetComponentLanguage(component);
        }

        /// <summary>
        /// 设置Panel时不判断配置中是否有数据
        /// </summary>
        /// <param name="component"></param>
        public void TrySetPanelLanguage(GComponent component)
        {
            SetComponentFont(component);
            SetComponentLanguage(component);
        }

        /// <summary>
        /// 设置组件多语言
        /// </summary>
        /// <param name="component"></param>
        public void SetComponentLanguage(GComponent component)
        {
            bool comHasLanConfig = false;
            UIComponentLanguage componentLangeage = null;
            if (component.packageItem != null && TryGetValue(component.resourceURL, out componentLangeage))
                comHasLanConfig = true;

            var count = component.numChildren;
            for (var i = 0; i < count; i++)
            {
                var child = component.GetChildAt(i);
                if (child.packageItem != null && child is GComponent childCom)
                {
                    SetComponentLanguage(childCom);
                }
                else if (child is GList list)
                {
                    SetComponentLanguage(list);
                }

                if (comHasLanConfig)
                {
                    var id = child.id;
                    if (componentLangeage.TryGetValue(id, out var key))
                    {
                        if (child is GLoader gLoader)
                        {
                            gLoader.SetLanguageImage(key);
                        }
                        else
                        {
                            child.SetLanguage(key);
                        }
                    }
                }
            }
        }

        // private void SetChildLanguage(object child, string value)
        // {
        //     if (child is TextField textField)
        //     {
        //         textField.text = value;
        //     }
        //     else if (child is GRichTextField richTextField)
        //     {
        //         richTextField.text = value;
        //     }
        //     else if (child is GButton button)
        //     {
        //         button.title = value;
        //     }
        //     else if (child is GLabel label)
        //     {
        //         label.title = value;
        //     }
        //     else if (child is GTextField gtextField)
        //     {
        //         gtextField.text = value;
        //     }
        // }

        void SetComponentFont(GComponent component)
        {
            var count = component.numChildren;
            for (var i = 0; i < count; i++)
            {
                var child = component.GetChildAt(i);
                if (child is GComponent childCom)
                {
                    SetComponentFont(childCom);
                }
                else
                {
                    SetChildFont(child);
                }
            }
        }

        void SetChildFont(GObject child)
        {
            GTextField curField = null;
            if (child is GRichTextField richTextField)
            {
                curField = richTextField;
            }
            else if (child is GButton button)
            {
                curField = button.GetTextField();
            }
            else if (child is GLabel label)
            {
                curField = label.GetTextField();
            }
            else if (child is GTextField gtextField)
            {
                curField = gtextField;
            }

            if (curField == null) return;
            var textFormat = curField.textFormat;
            Log.Info($"font={textFormat.font}");
            var fontName = textFormat.font;
            if (string.IsNullOrEmpty(fontName))
            {
                fontName = UIConfig.defaultFont;
            }

            var font = Lan.GetLanFontByCurFont(fontName);
            if (font == null) return;
            textFormat.font = font;
            curField.textFormat = textFormat;
        }
    }
}