using System;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Concurrent;
using System.Drawing;
using System.Collections.Generic;

namespace PMSUI.Config
{
    /// <summary>
    /// 全局数据
    /// </summary>
    [Serializable]
    public class CGlobalData
    {
        public bool gs_IsSavePassword = false;
        public string gs_UserName = "";
        public string gs_Password = "";

        public byte gs_FormBackColor_R = Color.White.R;
        public byte gs_FormBackColor_G = Color.White.G;
        public byte gs_FormBackColor_B = Color.White.B;

        public bool gs_IsShowMaterial = false;              // 是否显示物料信息
        public bool gs_IsUseBatchOpt = false;               // 是否批量操作
        public string gs_DisplayLanguage = "zh-CN";             // 界面显示语言
    }

    public class CConfigManager
    {
        public static CGlobalData gs_globalConfig = new CGlobalData();
        public static CFormHelpConfig gs_FormHelpConfig = new CFormHelpConfig();

        private static string m_strPath = "";
        private static string _GlobalDataFilePath = "";
        private static string _FormHelpFilePath = "";

        public static void Init()
        {
            InitConfigPath();
            InitGlobalData();
            InitFormHelp();
        }

        private static void InitConfigPath()
        {
            //获取到文件完整路径
            string s = System.Reflection.Assembly.GetExecutingAssembly().Location;
            //获取到文件所在目录
            FileInfo finfo = new FileInfo(s);
            string BaseDirectory = finfo.DirectoryName;

            m_strPath = Path.Combine(BaseDirectory, "PMS", "Config");

            _GlobalDataFilePath = Path.Combine(m_strPath, "CConfigSetting.xml");
            _FormHelpFilePath = Path.Combine(m_strPath, "FormHelpConfig.xml");
        }

        private static void ExsitConfigPath()
        {
            DirectoryInfo dir = new DirectoryInfo(m_strPath);
            if (!dir.Exists)
            {
                dir.Create();
            }
        }

        public static void InitGlobalData()
        {
            //反序列化
            try
            {
                ExsitConfigPath();
                XmlSerializer serializer = new XmlSerializer(gs_globalConfig.GetType());
                if(!File.Exists(_GlobalDataFilePath))
                {
                    SaveGlobalData();
                }
                else
                {
                    using (TextReader tr = new StreamReader(_GlobalDataFilePath))
                    {
                        gs_globalConfig = (CGlobalData)serializer.Deserialize(tr);
                    }
                }
     
            }
            catch (Exception e)
            {
                string strlog = e.Message;
            }
        }

        public static void SaveGlobalData()
        {
            try
            {
                ExsitConfigPath();
                XmlSerializer serializer = new XmlSerializer(gs_globalConfig.GetType());
                using (TextWriter tw = new StreamWriter(_GlobalDataFilePath))
                {
                    serializer.Serialize(tw, gs_globalConfig);
                }
            }
            catch
            { }
        }

        public static void InitFormHelp()
        {
            //反序列化
            try
            {
                ExsitConfigPath();
                if (!File.Exists(_FormHelpFilePath))
                {
                    gs_FormHelpConfig.FormHelps = DefaultFormHelp.FormHelps();
                    SaveFormHelp();
                }
                else
                {
                    XmlSerializer serializer = new XmlSerializer(gs_FormHelpConfig.GetType());
                    using (TextReader tr = new StreamReader(_FormHelpFilePath))
                    {
                        gs_FormHelpConfig = (CFormHelpConfig)serializer.Deserialize(tr);//Serialize(tw, CConfigData.gs_globalConfig)
                    }
                }
            }
            catch (Exception e)
            {
                string strlog = e.Message;
            }
        }

        public static void SaveFormHelp()
        {
            try
            {
                ExsitConfigPath();
                XmlSerializer serializer = new XmlSerializer(gs_FormHelpConfig.GetType());
                using (TextWriter tw = new StreamWriter(_FormHelpFilePath))
                {
                    serializer.Serialize(tw, gs_FormHelpConfig);
                }
            }
            catch (Exception e)
            {
                string strlog = e.Message;
            }
        }

    }
}
