
using System.Configuration;
using System.Windows.Forms;

namespace IrisSetting
{
    public class ConfigMethod
    {
         public static string GetAppConfig(string strKey)
        {
            string file = Application.StartupPath+ "\\irisRefactor.exe";
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            foreach (string key in config.AppSettings.Settings.AllKeys)
            {
                if (key == strKey)
                {
                    return config.AppSettings.Settings[strKey].Value.ToString();
                }
            }
            return null;
        }

        public static void UpdateAppConfig(string newKey, string newValue)
        {
            string file = Application.StartupPath + "\\irisRefactor.exe";
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);

            config.AppSettings.Settings[newKey].Value = newValue;
            config.Save();
        }

        #region nomerol常规设置
        public static ConfigModel GetConfigModel() {
            ConfigModel configModel = new ConfigModel();
            configModel.FormType = GetAppConfig("formType");
            configModel.MotherboardType = GetAppConfig("motherboardType");
            configModel.Language = GetAppConfig("language");
            configModel.TempType = GetAppConfig("tempType");
            configModel.RejectTimeOut = GetAppConfig("rejectTimeOut");
            configModel.ShowTimeFailure = GetAppConfig("showTimeFailure");
            configModel.ShowTimeSuccess = GetAppConfig("showTimeSuccess");

            DoorConfig doorConfig = new DoorConfig();
            doorConfig.DoorSystemEnable = GetAppConfig("doorSystemEnable");
            doorConfig.NeedPermisson = GetAppConfig("needPermisson");
            doorConfig.SerialPort = GetAppConfig("serialPort");
            doorConfig.SleepTime = GetAppConfig("sleepTime");
            doorConfig.CheckTemp = GetAppConfig("checkTemp");

            PrintConfig printConfig = new PrintConfig();
            printConfig.PrintEnable = GetAppConfig("printEnable");
            printConfig.DeviceSN = GetAppConfig("deviceSN");
            printConfig.DestIp = GetAppConfig("destIp");
            printConfig.DestPort = GetAppConfig("destPort");

            WorkConfig workConfig = new WorkConfig();
            workConfig.WorkEnable = GetAppConfig("workEnable");

            configModel.DoorConfig = doorConfig;
            configModel.PrintConfig = printConfig;
            configModel.WorkConfig = workConfig;

            return configModel;
        }

        public static void SaveConfigModel(ConfigModel configModel)
        {
            UpdateAppConfig("formType", configModel.FormType);
            UpdateAppConfig("motherboardType", configModel.MotherboardType);
            UpdateAppConfig("tempType", configModel.TempType);
            UpdateAppConfig("language", configModel.Language);
            UpdateAppConfig("rejectTimeOut", configModel.RejectTimeOut);
            UpdateAppConfig("showTimeFailure", configModel.ShowTimeFailure);
            UpdateAppConfig("showTimeSuccess", configModel.ShowTimeSuccess);

            UpdateAppConfig("doorSystemEnable", configModel.DoorConfig.DoorSystemEnable);
            UpdateAppConfig("needPermisson", configModel.DoorConfig.NeedPermisson);
            UpdateAppConfig("serialPort", configModel.DoorConfig.SerialPort);
            UpdateAppConfig("sleepTime", configModel.DoorConfig.SleepTime);
            
            UpdateAppConfig("printEnable", configModel.PrintConfig.PrintEnable);
            UpdateAppConfig("deviceSN", configModel.PrintConfig.DeviceSN);
            UpdateAppConfig("destIp", configModel.PrintConfig.DestIp);
            UpdateAppConfig("destPort", configModel.PrintConfig.DestPort);

            UpdateAppConfig("workEnable",configModel.WorkConfig.WorkEnable);

            UpdateAppConfig("checkTemp", configModel.DoorConfig.CheckTemp);
        }

        public static ConfigModel InitConfigModel()
        {
            ConfigModel configModel = new ConfigModel();
            configModel.FormType = "1";
            configModel.MotherboardType = "2";
            configModel.Language = "1";
            configModel.RejectTimeOut = "10000";
            configModel.ShowTimeFailure = "3000";
            configModel.ShowTimeSuccess = "3000";

            DoorConfig doorConfig = new DoorConfig();
            doorConfig.DoorSystemEnable = "false";
            doorConfig.NeedPermisson = "false";
            doorConfig.SerialPort = "COM1";
            doorConfig.SleepTime = "1000";
            doorConfig.CheckTemp = "false";

            PrintConfig printConfig = new PrintConfig();
            printConfig.PrintEnable = "false";
            printConfig.DeviceSN = "JG_DY_1201";
            printConfig.DestIp = "20.1.2.1";
            printConfig.DestPort = "2055";

            WorkConfig workConfig = new WorkConfig();
            workConfig.WorkEnable = "false";

            configModel.DoorConfig = doorConfig;
            configModel.PrintConfig = printConfig;
            configModel.WorkConfig = workConfig;
            return configModel;
        }
        #endregion

        #region db数据库设置
        public static DbConfig GetDbConfig() {

            DbConfig dbConfig = new DbConfig();
            dbConfig.SqlType = GetAppConfig("sqlType");
            dbConfig.LocalDatabaseName = GetAppConfig("localDatabaseName");
            dbConfig.LocalDatabasePort = GetAppConfig("localDatabasePort");
            dbConfig.LocalDatabaseUser = GetAppConfig("localDatabaseUser");
            dbConfig.LocalDatabasePassword = GetAppConfig("localDatabasePassword");
            dbConfig.ServerIp = GetAppConfig("serverIp");
            dbConfig.ServerDatabaseName = GetAppConfig("serverDatabaseName");
            dbConfig.ServerDatabasePort = GetAppConfig("serverDatabasePort");
            dbConfig.ServerDatabaseUser = GetAppConfig("serverDatabaseUser");
            dbConfig.ServerDatabasePassword = GetAppConfig("serverDatabasePassword");

            dbConfig.ServerAppPort = GetAppConfig("serverAppPort");
            dbConfig.ListenPort = GetAppConfig("listenPort");

            return dbConfig;
        }
        
        public static void SaveDbConfig(DbConfig dbConfig) {
            UpdateAppConfig("sqlType", dbConfig.SqlType);

            UpdateAppConfig("localDatabaseName", dbConfig.LocalDatabaseName);
            UpdateAppConfig("localDatabasePort", dbConfig.LocalDatabasePort);
            UpdateAppConfig("localDatabaseUser", dbConfig.LocalDatabaseUser);
            UpdateAppConfig("localDatabasePassword", dbConfig.LocalDatabasePassword);

            UpdateAppConfig("serverIp", dbConfig.ServerIp);
            UpdateAppConfig("serverDatabaseName", dbConfig.ServerDatabaseName);
            UpdateAppConfig("serverDatabasePort", dbConfig.ServerDatabasePort);
            UpdateAppConfig("serverDatabaseUser", dbConfig.ServerDatabaseUser);
            UpdateAppConfig("serverDatabasePassword", dbConfig.ServerDatabasePassword);

            UpdateAppConfig("serverAppPort", dbConfig.ServerAppPort);
            UpdateAppConfig("listenPort", dbConfig.ListenPort);
        }

        public static DbConfig InitDbConfig()
        {
            DbConfig dbConfig = new DbConfig();
            dbConfig.SqlType = "1";
            dbConfig.LocalDatabaseName = "casic_iris_temperature";
            dbConfig.LocalDatabasePort = "3306";
            dbConfig.LocalDatabaseUser = "root";
            dbConfig.LocalDatabasePassword = "root";

            dbConfig.ServerIp = "192.168.8.107";
            dbConfig.ServerDatabaseName = "casic_iris_temperature";
            dbConfig.ServerDatabasePort = "3306";
            dbConfig.ServerDatabaseUser = "root";
            dbConfig.ServerDatabasePassword = "root";

            dbConfig.ServerAppPort = "20005";
            dbConfig.ListenPort = "8000";

            return dbConfig;
        }
        #endregion

        #region temp参数设置
        public static TempConfig GetTempConfig() {
            TempConfig tempConfig = new TempConfig();
            tempConfig.Correction = GetAppConfig("correction");

            tempConfig.ParamRegion0 = GetAppConfig("paramRegion0");
            tempConfig.ParamRegion11 = GetAppConfig("paramRegion11");
            tempConfig.ParamRegion12= GetAppConfig("paramRegion12");

            tempConfig.ParamRegion21 = GetAppConfig("paramRegion21");
            tempConfig.ParamRegion22= GetAppConfig("paramRegion22");
            tempConfig.ParamRegion31 = GetAppConfig("paramRegion31");
            tempConfig.ParamRegion32 = GetAppConfig("paramRegion32");

            tempConfig.Separate0 = GetAppConfig("separate0");
            tempConfig.Separate1 = GetAppConfig("separate1");
            tempConfig.Separate2 = GetAppConfig("separate2");
            tempConfig.Separate3 = GetAppConfig("separate3");

            tempConfig.ParamHigh = GetAppConfig("paramHigh");
            tempConfig.Tnor = GetAppConfig("tnor");
            tempConfig.Tabnor = GetAppConfig("tabnor");

            return tempConfig;
        }

        public static void SaveTempConfig(TempConfig tempConfig) {
            UpdateAppConfig("correction", tempConfig.Correction);

            UpdateAppConfig("paramRegion0", tempConfig.ParamRegion0);
            UpdateAppConfig("paramRegion11", tempConfig.ParamRegion11);
            UpdateAppConfig("paramRegion12", tempConfig.ParamRegion12);

            UpdateAppConfig("paramRegion21", tempConfig.ParamRegion21);
            UpdateAppConfig("paramRegion22", tempConfig.ParamRegion22);
            UpdateAppConfig("paramRegion31", tempConfig.ParamRegion31);
            UpdateAppConfig("paramRegion32", tempConfig.ParamRegion32);

            UpdateAppConfig("separate0", tempConfig.Separate0);
            UpdateAppConfig("separate1", tempConfig.Separate1);
            UpdateAppConfig("separate2", tempConfig.Separate2);
            UpdateAppConfig("separate3", tempConfig.Separate3);

            UpdateAppConfig("paramHigh", tempConfig.ParamHigh);
            UpdateAppConfig("tnor", tempConfig.Tnor);
            UpdateAppConfig("tabnor", tempConfig.Tabnor);
        }

        public static TempConfig InitTempConfig()
        {
            TempConfig tempConfig = new TempConfig();
            tempConfig.Correction = "0.0";
            tempConfig.ParamRegion0 = "0";
            tempConfig.ParamRegion11 = "0.3";
            tempConfig.ParamRegion12 = "26.4";

            tempConfig.ParamRegion21 = "0.2";
            tempConfig.ParamRegion22 = "29.6";
            tempConfig.ParamRegion31 = "0.3";
            tempConfig.ParamRegion32 = "26.2";

            tempConfig.Separate0 = "0";
            tempConfig.Separate1 = "2";
            tempConfig.Separate2 = "4";
            tempConfig.Separate3 = "7";

            tempConfig.ParamHigh = "0.5";
            tempConfig.Tnor = "7.3";
            tempConfig.Tabnor = "9";


            return tempConfig;
        }
        #endregion
    }
}