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

namespace Pms.View.Config
{
    /// <summary>
    /// 全局数据
    /// </summary>
    [Serializable]
    public class CPmsViewData
    {
        public List<PmsColor> PmsColorList = new List<PmsColor>();
    }

    public class PmsColor
    {
        public string Elem = "";
        public byte R = 0;
        public byte G = 0;
        public byte B = 0;
    }

    public class CViewConfigManager
    {
        public static CPmsViewData gs_ViewData = new CPmsViewData();

        private static string m_strPath = "";
        private static string _ViewDataFilePath = "";

        public static void Init()
        {
            InitConfigPath();
            InitViewData();
        }

        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");

            _ViewDataFilePath = Path.Combine(m_strPath, "PmsViewConfig.xml");
        }

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

        public static void InitViewData()
        {
            //反序列化
            try
            {
                ExsitConfigPath();
                XmlSerializer serializer = new XmlSerializer(gs_ViewData.GetType());
                if(!File.Exists(_ViewDataFilePath))
                {
                    SaveViewData();
                }
                else
                {
                    using (TextReader tr = new StreamReader(_ViewDataFilePath))
                    {
                        gs_ViewData = (CPmsViewData)serializer.Deserialize(tr);
                    }
                }
     
            }
            catch (Exception e)
            {
                string strlog = e.Message;
            }
        }

        public static void SaveViewData()
        {
            try
            {
                ExsitConfigPath();
                XmlSerializer serializer = new XmlSerializer(gs_ViewData.GetType());
                using (TextWriter tw = new StreamWriter(_ViewDataFilePath))
                {
                    serializer.Serialize(tw, gs_ViewData);
                }
            }
            catch
            { }
        }
    }
}
