using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Pms.WebHandle
{
    public class UriData
    {
        public string Uri = "http://127.0.0.1:8804/taiguang";
        public string MESUri = "http://127.0.0.1:8804/taiguang";
        public string WMSUri = "http://127.0.0.1:8804/taiguang";
    }

    public class UriSetting
    {
        private static UriData g_DBSetting = new UriData();
        private static string m_ServiceUri = "";
        private static string m_MESUri = "";
        private static string m_WMSUri = "";

        public static string g_ServiceUri
        {
            get
            {
                if (m_ServiceUri == "")
                {
                    InitDBSetting();

                    try
                    {
                        XmlSerializer serializer1 = new XmlSerializer(UriSetting.g_DBSetting.GetType());
                        string strPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins\\PMS\\Config";
                        string strFileName = strPath + "\\UriSetting.xml";

                        DirectoryInfo dir = new DirectoryInfo(strPath);
                        if (!dir.Exists)
                        {
                            dir.Create();
                        }

                        using (TextWriter tw = new StreamWriter(strFileName))
                        {
                            serializer1.Serialize(tw, UriSetting.g_DBSetting);
                        }
                    }
                    catch
                    { }
                }
                return m_ServiceUri;
            }
        }
        public static string g_MESUri
        {
            get
            {
                if (m_MESUri == "")
                {
                    InitDBSetting();

                    try
                    {
                        XmlSerializer serializer1 = new XmlSerializer(UriSetting.g_DBSetting.GetType());
                        string strPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins\\PMS\\Config";
                        string strFileName = strPath + "\\UriSetting.xml";

                        DirectoryInfo dir = new DirectoryInfo(strPath);
                        if (!dir.Exists)
                        {
                            dir.Create();
                        }

                        using (TextWriter tw = new StreamWriter(strFileName))
                        {
                            serializer1.Serialize(tw, UriSetting.g_DBSetting);
                        }
                    }
                    catch
                    { }
                }
                return m_MESUri;
            }
        }
        public static string g_WMSUri
        {
            get
            {
                if (m_WMSUri == "")
                {
                    InitDBSetting();

                    try
                    {
                        XmlSerializer serializer1 = new XmlSerializer(UriSetting.g_DBSetting.GetType());
                        string strPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins\\PMS\\Config";
                        string strFileName = strPath + "\\UriSetting.xml";

                        DirectoryInfo dir = new DirectoryInfo(strPath);
                        if (!dir.Exists)
                        {
                            dir.Create();
                        }

                        using (TextWriter tw = new StreamWriter(strFileName))
                        {
                            serializer1.Serialize(tw, UriSetting.g_DBSetting);
                        }
                    }
                    catch
                    { }
                }
                return m_WMSUri;
            }
        }

        private static void InitDBSetting()
        {
            //反序列化
            XmlSerializer serializer = new XmlSerializer(UriSetting.g_DBSetting.GetType());
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "Plugins\\PMS\\Config\\UriSetting.xml";
                using (TextReader tr = new StreamReader(path))
                {
                    UriSetting.g_DBSetting = (UriData)serializer.Deserialize(tr);
                }
            }
            catch (Exception)
            { }

            m_ServiceUri = g_DBSetting.Uri;
            m_MESUri = g_DBSetting.MESUri;
            m_WMSUri = g_DBSetting.WMSUri;
        }
    }
}

