using System.Collections.Generic;
using FairyGUI;

namespace NBC
{
    public static class Lan
    {
        private static LanguageManager _inst;

        public static LanguageManager Inst
        {
            get
            {
                if (_inst == null)
                {
                    _inst = new LanguageManager();
                }

                return _inst;
            }
        }

        public static ILanguage Text => Inst.GetLanguageModule((int)LanguageModuleType.Text);

        public static string Get(string key)
        {
            return Text.Get(key);
        }

        public static string Get(string key, params object[] args)
        {
            var format = string.Format(Text.Get(key), args);
            return format;
        }

        public static void SetLanguage(this GObject child, string key, params object[] args)
        {
            if(string.IsNullOrEmpty(key)) return;
            if (Text == null) return;
            // child.lang = key;

            
            var value = Text.Get(key);

            if (args != null && args.Length > 0)
            {
                value = string.Format(value, args);
                // child.langArgs = args;
            }
            else
            {
                // child.langArgs = null;
            }


            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;
            }
        }

        public static void SetLanguageImage(this GObject obj, string key)
        {
        }

        // public static string Get(string key)
        // {
        //     if(Text == null) return key;
        //     return Text.Get(key);
        // }
        //
        // public static string Get(string key, params object[] args)
        // {
        //     var format = string.Format(Text.Get(key), args);
        //     return format;
        // }


        public static string GetLanImagePath(string key)
        {
            return Inst.GetLanguageModule((int)LanguageModuleType.Image).Get(key);
        }

        public static string GetLanFontByCurFont(string key)
        {
            if (key != null)
                return Inst.GetLanguageModule((int)LanguageModuleType.Font).Get(key);
            return null;
        }
    }

    public enum LanguageModuleType
    {
        Text = 0,
        Image = 1,
        Font = 2,
    }
}