using ProjectManagementSystem.Common.Function;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace ProjectManagementSystem.Config
{
    public class CConfiManager
    {
        public static CustomConfig customConfig = new CustomConfig();
        public static PmsAutoupdateConfig PmsConfig = new PmsAutoupdateConfig();

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

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

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

        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(customConfig.GetType());
                if (!File.Exists(_GlobalDataFilePath))
                {
                    SaveGlobalData();
                }
                else
                {
                    using (TextReader tr = new StreamReader(_GlobalDataFilePath))
                    {
                        customConfig = (CustomConfig)serializer.Deserialize(tr);
                    }
                }

            }
            catch (Exception e)
            {
                string strlog = e.Message;
            }
        }

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

        public static void InitPmsAutoupdateXml()
        {
            try
            {
                string s = System.Reflection.Assembly.GetExecutingAssembly().Location;
                FileInfo finfo = new FileInfo(s);

                string currPath = finfo.DirectoryName;
                //string configDir = Path.Combine(currPath, "PMS", "Config");
                string configFile = Path.Combine(currPath, "pms.xml");

                PmsConfig.Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                PmsConfig.Url = AppSetting.GetAutoUpdateUrl().Replace("xml", "zip");

                string xml = XmlTools.XmlSerialize(PmsConfig);
                //XmlSerializer serializer = new XmlSerializer(PmsConfig.GetType());
                using (TextWriter tw = new StreamWriter(configFile))
                {
                    tw.Write(xml);
                    //serializer.Serialize(tw, PmsConfig);
                }



                string dataFile = Path.Combine(currPath, "pms.zip");
                if (File.Exists(dataFile))
                {
                    File.Delete(dataFile);
                }

                List<string> fileList = new List<string>();

                DirectoryInfo theFolder = new DirectoryInfo(currPath);
                FileInfo[] fileInfos = theFolder.GetFiles();
                //遍历文件
                foreach (FileInfo fileInfo in fileInfos)
                {
                    if (fileInfo.Extension == ".config"
                        || fileInfo.Name.Contains("vshost"))
                    {
                        continue;
                    }

                    if (fileInfo.Name.StartsWith("ProjectManagementSystem")
                        || fileInfo.Name.StartsWith("DbCommon"))
                    {
                        fileList.Add(fileInfo.FullName);
                    }

                }

                ZipUtility zipUtility = new ZipUtility();
                zipUtility.ZipFileFromFiles(currPath, fileList, dataFile, 9);
            }
            catch (Exception)
            {
            }
        }
    }
}
