using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace UHFReader288Demo
{
    class ResourceCulture
    {
        public static void SetCurrentThreadCulture(string lang)
        {
            if (lang == null)
            {
                lang = string.Empty;
            }
            Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
        }

        public static string GetString(string id)
        {
            string strCurLanguage = "";
           
            try
            {
                ResourceManager rm = new ResourceManager("UHFReader288Demo.DemoString", Assembly.GetExecutingAssembly());
                CultureInfo ci = Thread.CurrentThread.CurrentCulture;
                strCurLanguage = rm.GetString(id, ci);
            }
            catch
            {
                strCurLanguage = "No id:" + id + ", please add.";
            }

            return strCurLanguage;
        }

        public static void SetLang(string lang, Form form, Type formType)
        {
            if (lang == null)
            {
                lang = string.Empty;
            }
            SetCurrentThreadCulture(lang);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            if (form != null)
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);
                resources.ApplyResources(form, "$this");
                AppLang(form, resources);
            }
        }

        private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
        {
            if (control is MenuStrip)
            {
                resources.ApplyResources(control, control.Name);
                MenuStrip ms = (MenuStrip)control;
                if (ms.Items.Count > 0)
                {
                    foreach (ToolStripMenuItem c in ms.Items)
                    {
                        AppLang(c, resources);
                    }
                }
            }
            if (control is ComboBox)
            {
                ComboBox cb = (ComboBox)control;
                AppLang(cb, resources);
            }
            if (control is DataGridView)
            {
                DataGridView dgv = (DataGridView)control;
                AppLang(dgv, resources);
            }

            if (control is TabControl)
            {
                TabControl dgv = (TabControl)control;
                AppLang(dgv, resources);
            }

            foreach (Control c in control.Controls)
            {
                resources.ApplyResources(c, c.Name);
                AppLang(c, resources);

            }


        }

        private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
        {
            if (item is ToolStripMenuItem)
            {
                resources.ApplyResources(item, item.Name);
                if (item.DropDownItems.Count > 0)
                {
                    foreach (ToolStripMenuItem c in item.DropDownItems)
                    {
                        AppLang(c, resources);
                    }
                }
            }
        }

        private static void AppLang(ComboBox comBox, System.ComponentModel.ComponentResourceManager resources)
        {
            if (comBox is ComboBox)
            {
                if (comBox.Items.Count > 0)
                {
                    var count = comBox.Items.Count;
                    comBox.BeginUpdate();
                    for (int i = 0; i < count; i++)
                    {
                        var number = i == 0 ? "" : $"{i}";
                        var item = resources.GetString($"{comBox.Name}.Items{number}");
                        if (item != null)
                        {
                            comBox.Items[i] = item;
                        }
                    }
                    comBox.EndUpdate();
                }
                resources.ApplyResources(comBox, comBox.Name);
            }
        }

        private static void AppLang(DataGridView dataGridView, System.ComponentModel.ComponentResourceManager resources)
        {
            if (dataGridView is DataGridView)
            {
                foreach (DataGridViewColumn column in dataGridView.Columns)
                {
                    resources.ApplyResources(column, column.Name);
                }
            }
        }

        private static void AppLang(TabControl tab, System.ComponentModel.ComponentResourceManager resources)
        {
            if (tab is TabControl)
            {
                foreach (TabPage page in tab.TabPages)
                {
                    resources.ApplyResources(page, page.Name);
                }
            }
        }
    }
}
