using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Data;
using CasunCommon.SystemLog;
using PMSUI.Config;

namespace PMSUI
{
    public class CLanguageManager
    {
        private static Dictionary<string, string> m_DisplayLanguageDic = new Dictionary<string, string>();
        private static string m_DisplayLanguage = "";
        

        /// <summary>
        /// 记录当前语言切换关键字
        /// </summary>
        private static List<string> m_LanguageKey = new List<string>();

        public static List<string> LanguageKey
        {
            get
            {
                return m_LanguageKey;
            }
            set
            {
                m_LanguageKey = value;
            }
        }

        /// <summary>
        /// 初始化关键字
        /// </summary>
        private static void InitLanguageKey()
        {
            List<string> ltemID = new List<string>();
            //DataTable dt = CasunModelDB.CDBManager.Instance.PMS_sys_LanguageKey.GetAllList().Tables[0];
            DataTable dt = PmsCoreDB.DBHost.Instance.LanguageKey.GetAllList().Tables[0];
            foreach (DataRow r in dt.Rows)
            {
                string strkey = r.ItemArray[0].ToString();
                ltemID.Add(strkey);
            }
            m_LanguageKey = ltemID;
        }

        /// <summary>
        /// 初始化语言
        /// </summary>
        private static bool InitDisplayLanaguage()
        {
            try
            {
                string strwhere = " Language  = '" + m_DisplayLanguage + "'";
                //List<CasunModelDB.Model.PMS_sys_LanguageVo> smodelList = CasunModelDB.CDBManager.Instance.PMS_sys_Language.GetModelList(strwhere);
                List<PmsCoreDB.Model.PMS_sys_Language> smodelList = PmsCoreDB.DBHost.Instance.Language.GetModelList(strwhere);

                //Dictionary<string, string> m_DisplayLanguage = new Dictionary<string, string>();
                m_DisplayLanguageDic.Clear();

                foreach (PmsCoreDB.Model.PMS_sys_Language smodel in smodelList)
                {
                    if (!m_DisplayLanguageDic.ContainsKey(smodel.Keyword))
                    {
                        m_DisplayLanguageDic.Add(smodel.Keyword, smodel.Displayword);
                    }
                }
            }
            catch(Exception e)
            {
                string strlog = "初始化语言异常：" + e.Message + e.StackTrace;
                CLog.Instance.WriteLog("Error", strlog);
                return false;
            }
            return true;
        }

        #region SetLang
        /// <summary>
        /// 设置当前程序的界面语言
        /// </summary>
        /// <param name="language">language:zh-CN, en-US</param>
        /// <param name="form">窗体实例</param>
        public static void SetLang(Form form)
        {
            if (form != null)
            {
                if (m_DisplayLanguage != CConfigManager.gs_globalConfig.gs_DisplayLanguage)
                {
                    m_DisplayLanguage = CConfigManager.gs_globalConfig.gs_DisplayLanguage;
                    InitDisplayLanaguage();
                    InitLanguageKey();
                }
                AppLang(form);

                foreach (Control c in form.Controls)
                {
                    if(c is ComboBox)
                    {
                        continue;
                    }
                    if(c is TextBox)
                    {
                        continue;
                    }
                    if(c is DataGridView)
                    {
                        continue;
                    }
                    if(c is DateTimePicker)
                    {
                        continue;
                    }
                    if(c is Form)
                    {
                        continue;
                    }
                    AppLang(c);
                }
            }
        }
        #endregion
        
        /// <summary>
        /// 语言切换
        /// </summary>
        /// <param name="strKeyword"></param>
        /// <returns></returns>
        public static string LanguageChange(string strKeyword)
        {
            if (strKeyword == "")
            {
                return strKeyword;
            }
            if (m_DisplayLanguageDic.ContainsKey(strKeyword))
            {
                return m_DisplayLanguageDic[strKeyword] == "" ? strKeyword : m_DisplayLanguageDic[strKeyword];
            }
            else
            {
                AddKeyword(strKeyword);
                return strKeyword;
            }
        }

        /// <summary>
        /// 添加关键字
        /// </summary>
        /// <param name="Keyword"></param>
        /// <returns></returns>
        public static bool AddKeyword(string Keyword)
        {
            if (!PmsCoreDB.DBHost.Instance.LanguageKey.Exists(Keyword))
            {
                m_LanguageKey.Add(Keyword);
                PmsCoreDB.Model.PMS_sys_LanguageKey sLanguageKey = new PmsCoreDB.Model.PMS_sys_LanguageKey();
                sLanguageKey.Keyword = Keyword;
                PmsCoreDB.DBHost.Instance.LanguageKey.Add(sLanguageKey);
            }

            m_DisplayLanguageDic.Add(Keyword, "");

            List<string> LanguageList = PmsCoreDB.DBHost.Instance.Language.GetLanguage();
            foreach (string Language in LanguageList)
            {
                if (!PmsCoreDB.DBHost.Instance.Language.Exists(Language, Keyword))
                {
                    PmsCoreDB.Model.PMS_sys_Language slanguage = new PmsCoreDB.Model.PMS_sys_Language();
                    slanguage.Language = Language;
                    slanguage.Keyword = Keyword;
                    PmsCoreDB.DBHost.Instance.Language.Add(slanguage);
                }
            }
            return true;
        }

        public static bool DeleteKeyword(string Keyword)
        {
            PmsCoreDB.DBHost.Instance.LanguageKey.Delete(Keyword);
            m_DisplayLanguageDic.Remove(Keyword);
            PmsCoreDB.DBHost.Instance.Language.DeleteKeyword(Keyword);
            return true;
        }


        #region AppLang for control
        /// <summary>
        /// 递归遍历窗体所有控件，针对其设置当前界面语言
        /// </summary>
        /// <param name="control"></param>
        /// <param name="resources"></param>
        private static void AppLang(Control control)
        {
            if (control is MenuStrip)
            {
                MenuStrip ms = control as MenuStrip;
                foreach (ToolStripMenuItem c in ms.Items)
                {
                    AllToolStripMenuItems(c);
                    c.Text = LanguageChange(c.Text);
                }
            }
            else if(control is Form)
            {
                control.Text = LanguageChange(control.Text);
            }
            else if(control is Panel)
            {
                Panel pane = control as Panel;
                foreach (Control c in pane.Controls)
                {
                    AppLang(c);
                }
            }
            else if (control is SplitContainer)
            {
                SplitContainer SC = control as SplitContainer;
                foreach (Control c in SC.Panel1.Controls)
                {
                    AppLang(c);
                }
                foreach (Control c in SC.Panel2.Controls)
                {
                    AppLang(c);

                }
            }
            else if (control is ToolStripContainer)
            {
                ToolStripContainer tpc = control as ToolStripContainer;
                
                foreach (Control c in tpc.Controls)
                {
                    AppLang(c);
                }
            }
            else if (control is GroupBox)
            {
                GroupBox GP = control as GroupBox;

                control.Text = LanguageChange(control.Text);
                foreach (Control c in GP.Controls)
                {
                    AppLang(c);
                }
            }
            else if (control is TreeViewEx)
            {
                TreeViewEx TVE = control as TreeViewEx;
                foreach (TreeNode c in TVE.Nodes)
                {
                    AllNodes(c);
                    c.Text = LanguageChange(c.Text);
                }
            }
            else
            {
                control.Text = LanguageChange(control.Text);
            }
        }
        #endregion

        #region 遍历TreeNode的所有节点
        public static void AllNodes(TreeNode tn)
        {
            //1.将当前节点显示到lable上
            foreach (TreeNode c in tn.Nodes)
            {
                //string strText;
                //if (CLanguageManager.LanguageChange(c.Text, out strText))
                //{
                //    c.Text = strText;
                //}
                c.Text = LanguageChange(c.Text);
                AllNodes(c);
            }
        }
        #endregion

        # region 遍历MenuItem的所有节点
        /// <summary>
        /// 遍历菜单的所有节点
        /// </summary>
        /// <param name="Item"></param>
        private static void AllToolStripMenuItems(ToolStripMenuItem Item)
        {

            for (int i = 0; i < Item.DropDownItems.Count; i++)
            {
                if (Item.DropDownItems[i] is ToolStripSeparator)
                {
                    continue;
                }
                else
                {
                    ToolStripMenuItem c = Item.DropDownItems[i] as ToolStripMenuItem;
                    //string strText;
                    //if (CLanguageManager.LanguageChange(c.Text, out strText))
                    //{
                    //    c.Text = strText;
                    //}
                    c.Text = LanguageChange(c.Text);
                    AllToolStripMenuItems(c);
                }
            }
        }
        #endregion
    }
}
