
using System.Configuration;
using System;
namespace Core.Mes.ServerFrameWork
{
    public delegate void InvokeCallback(string msg);

    [Serializable]
    public class ServerConfigure
    {
        public Core.Mes.ServerFrameWork.ExtSrvDaemon.ShowExtSrvEvent ssc_event = null;

        private int _magicID = 0xFACE;
        public int MAGIC_ID
        {
            get { return _magicID; }
        }

        public int PreserveSystemMemory = 500;
        public string MainServerIP = "";
        public string MainServerPort = "";
        public string ExtServers = "";
        public string Debug = "false";
        public int CompressThreshold = 100;
        public int MethodTime = 30;
        public int MemoryMaxSize = 1000;
        public string ConfigFilePath = "";
        public string MESDB = "";
        public string ServerPort = "";
        public string UserTableSpace = "";
        public int CheckPassword = 0;
        public int CheckIpRule = 0;
        public double PriorityFactor = 1;

        public InvokeCallback OnMasterServerChanged = null;
        private string _masterServer = "";
        public string MasterServer
        {
            get { return _masterServer; }
            set
            {
                if (_masterServer == value) return;
                _masterServer = value;
                if (OnMasterServerChanged != null)
                {
                    OnMasterServerChanged.Invoke(_masterServer);
                }
            }
        }

        public ServerConfigure()
        {
            MainServerIP = ReadConfigure("ServerIP");
            MainServerPort = ReadConfigure("ServerPort");
            ExtServers = ReadConfigure("RemoteServerUrl");
            Debug = ReadConfigure("Debug").ToLower();
            MESDB = ReadConfigure("MESDB");
            ServerPort = ReadConfigure("ServerPort");
            UserTableSpace = ReadConfigure("UserTableSpace");
            ConfigFilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;

            //进行序列化压缩传送的数据阀值；（定义数据集所有表行数之和>CompressThreshold 时，进行压缩传输）,数据阀值不能太小，最小100； 
            _magicID = (new Random()).Next();
            CompressThreshold = ReadConfigure("CompressThreshold", 100);
            PriorityFactor = ReadConfigure("PriorityFactor", 1.0);
            MethodTime = ReadConfigure("MethodTime", 30);
            MemoryMaxSize = ReadConfigure("MemoryMaxSize", 1000);
            PreserveSystemMemory = ReadConfigure("PreserveSysMem", 500);
            CheckPassword = ReadConfigure("CheckPassword", 0);
            CheckIpRule = ReadConfigure("CheckIpRule", 0);
        }

        private string ReadConfigure(string ItemName)
        {
            try
            {
                return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings[ItemName].Value.ToString().Trim();
            }
            catch { }
            return "";
        }

        private int ReadConfigure(string ItemName, int default_value)
        {
            int _out = default_value;
            try
            {
                string _str_tmp = ReadConfigure(ItemName);
                if (!string.IsNullOrEmpty(_str_tmp) && int.TryParse(_str_tmp, out _out))
                {
                    _out = (_out < default_value ? default_value : _out);
                }
                return _out;
            }
            catch { }
            return _out;
        }

        private double ReadConfigure(string ItemName, double default_value)
        {
            double _out = default_value;
            try
            {
                string _str_tmp = ReadConfigure(ItemName);
                if (!string.IsNullOrEmpty(_str_tmp) && double.TryParse(_str_tmp, out _out))
                {
                    _out = (_out < default_value ? default_value : _out);
                }
                return _out;
            }
            catch { }
            return _out;
        }

    }
     
}