using System;
using System.Collections.Generic;
using System.Text;
using CoreFS.CA06;
using System.Xml;
using System.Windows.Forms;

namespace Core.Mes.ClientManager
{
   
    /// <summary>
    /// 记录操作用户使用的系统配置参数
    /// </summary>
    /// <remarks>add maliang</remarks>
    public class CoreClientOption
    {
        /// <summary>
        /// 记录操作用户使用的系统配置参数
        /// </summary>
        public CoreClientOption()
        {
        }

        /// <summary>
        /// 记录操作用户使用的系统配置参数
        /// </summary>
        public static CoreClientOption StaticCoreClientOption = null;

        public static string Application_Path = "";

        /// <summary>
        /// 用于存放所有的服务端连接字符串
        /// </summary>
        private Dictionary<string, CommunicationConfig> urlList = null;
        public Dictionary<string, CommunicationConfig> UrlList
        {
            get { return urlList; }
            set { urlList = value; }
        }

        private bool appRightMenuFlag = true;
        /// <summary>
        /// 指定是否记录操作界面历史信息
        /// </summary>
        public bool AppRightMenuFlag
        {
            get { return appRightMenuFlag; }
            set { appRightMenuFlag = value; }
        }

        private int appRightMenuNum = 0;
        /// <summary>
        /// 指定系统记录操作界面历史信息的个数
        /// </summary>
        public int AppRightMenuNum
        {
            get { return appRightMenuNum; }
            set { appRightMenuNum = value; }
        }

        private string commIP = "";
        /// <summary>
        /// 服务器连接IP
        /// </summary>
        public string CommIP
        {
            get { return commIP; }
            set { commIP = value; }
        }

        private string commPort = "";
        /// <summary>
        /// 服务器连接端口
        /// </summary>
        public string CommPort
        {
            get { return commPort; }
            set { commPort = value; }
        }
        private string commServer = "";
        /// <summary>
        /// 服务器名称
        /// </summary>
        public string CommServer
        {
            get { return commServer; }
            set { commServer = value; }
        }
        private bool if_Online = CoreFS.CA06.OpeBase.STATIC_IF_ONLINE;
        /// <summary>
        /// 设置是否联机运行客户端
        /// </summary>
        public bool IF_Online
        {
            get { return if_Online; }
            set
            {
                if_Online = value;
                CoreFS.CA06.OpeBase.STATIC_IF_ONLINE = value;
            }
        }

        private bool if_SavePasswd = false;
        /// <summary>
        /// 是否保存密码
        /// </summary>
        public bool IF_SavePasswd
        {
            get { return if_SavePasswd; }
            set { if_SavePasswd = value; }
        }

        private bool if_OrgTree = true;
        public bool IF_OrgTree
        {
            get { return if_OrgTree; }
            set { if_OrgTree = value; }
        }

        private bool if_Schedule = true;
        public bool IF_Schedule
        {
            get { return if_Schedule; }
            set { if_Schedule = value; }
        }

        private bool if_Title = true;
        public bool IF_Title
        {
            get { return if_Title; }
            set { if_Title = value; }
        }

        private Int32 scheHisDay = 7;
        public Int32 ScheHisDay
        {
            get { return scheHisDay; }
            set { scheHisDay = value; }
        }

        private bool if_ShowUserInfo = true;
        public bool IF_ShowUserInfo
        {
            get { return if_ShowUserInfo; }
            set { if_ShowUserInfo = value; }
        }
        private List<OptionUserGroupInfo> userGroup = new List<OptionUserGroupInfo>();
        public List<OptionUserGroupInfo> UserGroup
        {
            get { return userGroup; }
            set { userGroup = value; }
        }
        private List<OptionUserOrderInfo> userOrder = new List<OptionUserOrderInfo>();
        public List<OptionUserOrderInfo> UserOrder
        {
            get { return userOrder; }
            set { userOrder = value; }
        }
        private string loginPage = "0";
        public string LoginPage
        {
            get { return loginPage; }
            set { loginPage = value; }
        }
        /// <summary>
        /// 项目名称，显示在主窗体
        /// </summary>
        private string projectName = "";
        public string ProjectName
        {
            get { return projectName; }
            set { projectName = value; }
        }
        /// <summary>
        /// 登录、操记记录控制
        /// </summary>
        private int recordLevel = 3;
        public int RecoreLevel
        {
            get { return recordLevel; }
            set { recordLevel = value; }
        }
        /// <summary>
        /// 系统ID
        /// </summary>
        private string systemId = "";
        public string SystemId
        {
            get { return systemId; }
            set { systemId = value; }
        }
        /// <summary>
        /// 是否与服务端进行系统对时
        /// </summary>
        private bool systemdate = true;
        public bool Systemdate
        {
            get { return systemdate; }
            set { systemdate = value; }
        }
        /// <summary>
        /// 状态栏公司的设置
        /// </summary>
        private string companyName = "";
        public string CompanyName
        {
            get { return companyName; }
            set { companyName = value; }
        }
        /// <summary>
        /// 从配置文件中加载配置信息
        /// </summary>
        public void Load()
        {
            StaticCoreClientOption = new CoreClientOption();
            try
            {
                this.Load1();
                this.Load2();
            }
            catch { }
        }
        /// <summary>
        /// 从配置文件option中加载配置信息
        /// </summary>
        private void Load1()
        {
            //实例化xml
            string path = CoreClientOption.Application_Path + CoreResConfig.SYSTEM_OPTION;

            //读取xml文件
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            if (xml != null && xml.FirstChild != null && xml.FirstChild.Name == "xml")
            {
                xml.RemoveChild(xml.FirstChild);
            }

            foreach (XmlNode node in xml.FirstChild.ChildNodes)
            {
                switch (node.Name)
                {
                    case "AppRightMenuFlag":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.AppRightMenuFlag = true;
                        else
                            StaticCoreClientOption.AppRightMenuFlag = Convert.ToBoolean(node.FirstChild.Value);
                        break;
                    case "AppRightMenuNum":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.AppRightMenuNum = 10;
                        else
                            StaticCoreClientOption.AppRightMenuNum = Convert.ToInt32(node.FirstChild.Value);
                        break;
                    case "IF_Online":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.IF_Online = true;
                        else
                            StaticCoreClientOption.IF_Online = Convert.ToBoolean(node.FirstChild.Value);
                        break;
                    case "IF_SavePasswd":
                        //if (node.ChildNodes.Count == 0)
                        //    StaticCoreClientOption.IF_SavePasswd = false;
                        //else
                        //    StaticCoreClientOption.IF_SavePasswd = Convert.ToBoolean(node.FirstChild.Value);
                        //break;
                        StaticCoreClientOption.IF_SavePasswd = false;
                        break;
                    case "IF_OrgTree":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.IF_OrgTree = true;
                        else
                            StaticCoreClientOption.IF_OrgTree = Convert.ToBoolean(node.FirstChild.Value);
                        break;
                    case "IF_Schedule":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.IF_Schedule = true;
                        else
                            StaticCoreClientOption.IF_Schedule = Convert.ToBoolean(node.FirstChild.Value);
                        break;
                    case "IF_Title":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.IF_Title = true;
                        else
                            StaticCoreClientOption.IF_Title = Convert.ToBoolean(node.FirstChild.Value);
                        break;
                    case "ScheHisDay":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.ScheHisDay = 7;
                        else
                            StaticCoreClientOption.ScheHisDay = Convert.ToInt32(node.FirstChild.Value);
                        break;
                    case "IF_ShowUserInfo":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.IF_ShowUserInfo = true;
                        else
                            StaticCoreClientOption.IF_ShowUserInfo = Convert.ToBoolean(node.FirstChild.Value);
                        break;
                    case "UserGroup":
                        StaticCoreClientOption.UserGroup = new List<OptionUserGroupInfo>();

                        foreach (XmlNode xn in node.ChildNodes)
                        {
                            OptionUserGroupInfo oug = new OptionUserGroupInfo(xn.Attributes["UserID"].Value, xn.Attributes["UserGroup"].Value);
                            StaticCoreClientOption.UserGroup.Add(oug);
                        }
                        break;
                    case "UserOrder":
                        StaticCoreClientOption.UserOrder = new List<OptionUserOrderInfo>();

                        foreach (XmlNode xn in node.ChildNodes)
                        {
                            OptionUserOrderInfo oug = new OptionUserOrderInfo(xn.Attributes["UserID"].Value, xn.Attributes["UserOrder"].Value);
                            StaticCoreClientOption.UserOrder.Add(oug);
                        }
                        break;
                    case "LoginPage":
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.LoginPage = "0";
                        else
                            StaticCoreClientOption.LoginPage = node.FirstChild.Value;
                        break;
                    case "ProjectName":                 //项目名称，主窗体的Text
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.ProjectName = "";
                        else
                            StaticCoreClientOption.ProjectName = node.FirstChild.Value;
                        break;
                    case "RecordLevel":                 //记录系统登录、操作的控制水平
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.RecoreLevel = 3;
                        else
                        {
                            try
                            {
                                int level = int.Parse(node.FirstChild.Value);
                                StaticCoreClientOption.RecoreLevel = level;
                            }
                            catch
                            {
                                StaticCoreClientOption.RecoreLevel = 3;
                            }
                        }
                        break;
                    case "SystemId":                        //该系统的系统Id
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.systemId = "";
                        else
                            StaticCoreClientOption.systemId = node.FirstChild.Value;
                        break;
                    case "Systemdate":                      //是否进行系统对时
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.systemdate = true;
                        else
                            StaticCoreClientOption.systemdate = Convert.ToBoolean(node.FirstChild.Value);
                        break;
                    case "CompanyInfo":                     //状态栏公司设置
                        if (node.ChildNodes.Count == 0)
                            StaticCoreClientOption.companyName = "湖南视拓科技";
                        else
                            StaticCoreClientOption.companyName = node.FirstChild.Value;
                        break;
                }
            }
        }
        /// <summary>
        /// 从配置文件comm中加载配置信息
        /// </summary>
        private void Load2()
        {
            //实例化xml
            string path = CoreClientOption.Application_Path + CoreResConfig.SYSTEM_COMMCONFIG;

            //读取xml文件
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            foreach (XmlNode node in xml.FirstChild.FirstChild.ChildNodes)
            {
                switch (node.Name)
                {
                    case "url":
                        string str = node.FirstChild.Value;
                        string ustr = str.Substring(7);
                        str = ustr.Split('/')[0];

                        StaticCoreClientOption.commIP = str.Split(':')[0];
                        StaticCoreClientOption.commPort = str.Split(':')[1];
                        StaticCoreClientOption.commServer = ustr.Split('/')[1];
                        break;
                }
            }
        }
        public void Save()
        {
            try
            {
                this.Save1();
                this.Save2();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Save1()
        {
            if (StaticCoreClientOption == null) return;

            //实例化xml
            string path = CoreClientOption.Application_Path + CoreResConfig.SYSTEM_OPTION;

            //读取xml文件
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            foreach (XmlNode node in xml.ChildNodes[0].ChildNodes)
            {
                switch (node.Name)
                {
                    case "AppRightMenuFlag":
                        node.FirstChild.Value = StaticCoreClientOption.AppRightMenuFlag.ToString();
                        break;
                    case "AppRightMenuNum":
                        node.FirstChild.Value = StaticCoreClientOption.AppRightMenuNum.ToString();
                        break;
                    case "IF_Online":
                        node.FirstChild.Value = StaticCoreClientOption.IF_Online.ToString();
                        break;
                    case "IF_SavePasswd":
                        node.FirstChild.Value = StaticCoreClientOption.IF_SavePasswd.ToString();
                        break;
                    case "IF_OrgTree":
                        node.FirstChild.Value = StaticCoreClientOption.IF_OrgTree.ToString();
                        break;
                    case "IF_Schedule":
                        node.FirstChild.Value = StaticCoreClientOption.IF_Schedule.ToString();
                        break;
                    case "IF_Title":
                        node.FirstChild.Value = StaticCoreClientOption.IF_Title.ToString();
                        break;
                    case "ScheHisDay":
                        node.FirstChild.Value = StaticCoreClientOption.ScheHisDay.ToString();
                        break;
                    case "IF_ShowUserInfo":
                        node.FirstChild.Value = StaticCoreClientOption.IF_ShowUserInfo.ToString();
                        break;
                    case "UserGroup":
                        foreach (OptionUserGroupInfo oug in StaticCoreClientOption.UserGroup)
                        {
                            bool flag = false;
                            foreach (XmlNode xn in node.ChildNodes)
                            {
                                if (xn.Attributes["UserID"].Value == oug.UserID)
                                {
                                    xn.Attributes["UserGroup"].Value = oug.UserGroup;
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag)
                            {
                                node.InnerXml += string.Format("<Group UserID=\"{0}\" UserGroup=\"{1}\" />", oug.UserID, oug.UserGroup);
                            }
                        }
                        break;
                    case "UserOrder":
                        foreach (OptionUserOrderInfo oug in StaticCoreClientOption.UserOrder)
                        {
                            bool flag = false;
                            foreach (XmlNode xn in node.ChildNodes)
                            {
                                if (xn.Attributes["UserID"].Value == oug.UserID)
                                {
                                    xn.Attributes["UserOrder"].Value = oug.UserOrder;
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag)
                            {
                                node.InnerXml += string.Format("<Group UserID=\"{0}\" UserOrder=\"{1}\" />", oug.UserID, oug.UserOrder);
                            }
                        }
                        break;
                    case "LoginPage":
                        node.FirstChild.Value = StaticCoreClientOption.LoginPage;
                        break;
                    case "ProjectName":
                        node.FirstChild.Value = StaticCoreClientOption.projectName;
                        break;
                    //case "RecordLevel":
                    //    node.FirstChild.Value = StaticCoreClientOption.RecoreLevel;
                    //    break;
                    case "SystemId":
                        node.FirstChild.Value = StaticCoreClientOption.SystemId;
                        break;
                    case "Systemdate":
                        node.FirstChild.Value = StaticCoreClientOption.Systemdate.ToString();
                        break;
                }
            }
            xml.Save(path);
        }
        private void Save2()
        {
            if (StaticCoreClientOption == null) return;

            //实例化xml
            string path = CoreClientOption.Application_Path + CoreResConfig.SYSTEM_COMMCONFIG;

            //读取xml文件
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            foreach (XmlNode node in xml.FirstChild.FirstChild.ChildNodes)
            {
                switch (node.Name)
                {
                    case "url":
                        string str = CoreResConfig.COMM_STRING.Replace("IP",
                            StaticCoreClientOption.CommIP + ":" + StaticCoreClientOption.CommPort);
                        str = str.Replace("ServerName", StaticCoreClientOption.commServer);
                        node.FirstChild.Value = str;
                        break;
                }
            }
            xml.Save(path);
        }

    }

}
