
using irisHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IrisSetting
{
    public partial class SettingForm : Form
    {
        ConfigModel configModel;
        DbConfig dbConfig;
        TempConfig tempConfig;

        public SettingForm()
        {
            InitializeComponent();
        }

        private void SettingForm_Load(object sender, EventArgs e)
        {
            configModel = ConfigMethod.GetConfigModel();
            dbConfig = ConfigMethod.GetDbConfig();
            tempConfig = ConfigMethod.GetTempConfig();

            comboBoxBoardType.Items.Add("研华");
            comboBoxBoardType.Items.Add("研域");

            comboBoxLangue.Items.Add("中文");
            comboBoxLangue.Items.Add("英文");

            comboBoxTempType.Items.Add("大山");
            comboBoxTempType.Items.Add("艾瑞");

            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBoxSerialPort.Items.Add(port);
            }

            comboBoxDbType.Items.Add("MySql");
            comboBoxDbType.Items.Add("Sqlite");
            initForm1(configModel);
            initForm2(dbConfig);
            initForm3(tempConfig);
        }

        private void initForm1(ConfigModel _configModel)
        {
            if (_configModel.MotherboardType == "1")
                comboBoxBoardType.SelectedItem = "研华";
            else
                comboBoxBoardType.SelectedItem = "研域";

            if (_configModel.Language == "1")
                comboBoxLangue.SelectedItem = "中文";
            else
                comboBoxLangue.SelectedItem = "英文";

            if (_configModel.TempType == "1")
                comboBoxTempType.SelectedItem = "大山";
            else
                comboBoxTempType.SelectedItem = "艾瑞";

            textBoxRejectTimeOut.Text = _configModel.RejectTimeOut;
            textBoxShowTimeSuccess.Text = _configModel.ShowTimeSuccess;
            textBoxShowTimeFailure.Text = _configModel.ShowTimeFailure;

            if (_configModel.WorkConfig.WorkEnable == "true")
                checkBox5.Checked = true;
            else
                checkBox5.Checked = false;

            if (_configModel.DoorConfig.DoorSystemEnable == "true")
                checkBox1.Checked = true;
            else
                checkBox1.Checked = false;

            if (_configModel.DoorConfig.NeedPermisson == "true")
            {
                checkBox3.Checked = true;
                checkBox4.Checked = false;
            }
            else
            {
                checkBox3.Checked = false;
                checkBox4.Checked = true;
            }

            if (_configModel.DoorConfig.CheckTemp == "true")
            {
                checkBox6.Checked = true;
                checkBox7.Checked = false;
            }
            else
            {
                checkBox6.Checked = false;
                checkBox7.Checked = true;
            }

            comboBoxSerialPort.SelectedItem = _configModel.DoorConfig.SerialPort;
            textBoxSleepTime.Text = _configModel.DoorConfig.SleepTime;

            if (_configModel.PrintConfig.PrintEnable == "true")
                checkBox2.Checked = true;
            else
                checkBox2.Checked = false;

            textBoxDevCode.Text = _configModel.PrintConfig.DeviceSN;
            textBoxDeskIp.Text = _configModel.PrintConfig.DestIp;
            textBoxDeskPort.Text = _configModel.PrintConfig.DestPort;

            labelPic.Text = _configModel.FormType;
            String picPath = Application.StartupPath + "\\pic\\"+ labelPic.Text + ".png";
            pictureBox1.Load(picPath);
            group1Set();
            group2Set();
            
        }

        private void initForm2(DbConfig _dbConfig)
        {
            if (_dbConfig.SqlType == "1")
                comboBoxDbType.SelectedItem = "MySql";
            else
                comboBoxDbType.SelectedItem = "Sqlite";

            textBoxLocalDbName.Text = _dbConfig.LocalDatabaseName;
            textBoxLocalDbUser.Text = _dbConfig.LocalDatabaseUser;
            textBoxLocalDbPort.Text = _dbConfig.LocalDatabasePort;
            textBoxLocalDbPassWord.Text = _dbConfig.LocalDatabasePassword;

            textBoxServerIp.Text = _dbConfig.ServerIp;
            textBoxServerDbName.Text = _dbConfig.ServerDatabaseName;
            textBoxServerDbUser.Text = _dbConfig.ServerDatabaseUser;
            textBoxServerDbPort.Text = _dbConfig.ServerDatabasePort;
            textBoxServerDbPassWord.Text = _dbConfig.ServerDatabasePassword;

            textBoxListenPort.Text = _dbConfig.ListenPort;
            textBoxhttpPort.Text = _dbConfig.ServerAppPort;
        }

        private void initForm3(TempConfig tempConfig)
        {
            textBoxcorrection.Text = tempConfig.Correction;
            textBoxRegion0.Text = tempConfig.ParamRegion0;
            textBoxRegion11.Text = tempConfig.ParamRegion11;
            textBoxRegion12.Text = tempConfig.ParamRegion12;

            textBoxRegion21.Text = tempConfig.ParamRegion21;
            textBoxRegion22.Text = tempConfig.ParamRegion22;
            textBoxRegion31.Text = tempConfig.ParamRegion31;
            textBoxRegion32.Text = tempConfig.ParamRegion32;

            textBoxseparate0.Text = tempConfig.Separate0;
            textBoxseparate1.Text = tempConfig.Separate1;
            textBoxseparate2.Text = tempConfig.Separate2;
            textBoxseparate3.Text = tempConfig.Separate3;

            textBoxparamHigh.Text = tempConfig.ParamHigh;
            textBoxtnor.Text = tempConfig.Tnor;
            textBoxtabnor.Text = tempConfig.Tabnor;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            group1Set();
        }

        private void group1Set() {
            if (checkBox1.Checked == true)
            {
                checkBox3.Enabled = true;
                checkBox4.Enabled = true;

                checkBox6.Enabled = true;
                checkBox7.Enabled = true;

                comboBoxSerialPort.Enabled = true;
                textBoxSleepTime.Enabled = true;
            }
            else
            {
                checkBox3.Enabled = false;
                checkBox4.Enabled = false;

                checkBox6.Enabled = false;
                checkBox7.Enabled = false;

                comboBoxSerialPort.Enabled = false;
                textBoxSleepTime.Enabled = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            group2Set();
        }

        private void group2Set()
        {
            if (checkBox2.Checked == true)
            {
                textBoxDevCode.Enabled = true;
                textBoxDeskIp.Enabled = true;
                textBoxDeskPort.Enabled = true;
            }
            else
            {
                textBoxDevCode.Enabled = false;
                textBoxDeskIp.Enabled = false;
                textBoxDeskPort.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ConfigModel configModel = ConfigMethod.InitConfigModel();
            initForm1(configModel);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DbConfig dbConfig = ConfigMethod.InitDbConfig();
            initForm2(dbConfig);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ConfigModel configModel = new ConfigModel();
                configModel.FormType = labelPic.Text;

                configModel.MotherboardType = comboBoxBoardType.Text == "研华" ? "1" : "2";
                configModel.RejectTimeOut = textBoxRejectTimeOut.Text;
                configModel.ShowTimeSuccess = textBoxShowTimeSuccess.Text;
                configModel.ShowTimeFailure = textBoxShowTimeFailure.Text;
                configModel.Language = comboBoxLangue.Text == "中文" ? "1" : "2";
                configModel.TempType = comboBoxTempType.Text == "大山" ? "1" : "2";

                if (checkBox5.Checked)
                    configModel.WorkConfig.WorkEnable = "true";
                else
                    configModel.WorkConfig.WorkEnable = "false";

                configModel.DoorConfig.DoorSystemEnable = checkBox1.Checked ? "true" : "false";
                if (checkBox3.Checked)
                    configModel.DoorConfig.NeedPermisson = "true";
                else if (checkBox4.Checked)
                    configModel.DoorConfig.NeedPermisson = "false";

                configModel.DoorConfig.SerialPort = comboBoxSerialPort.Text;
                configModel.DoorConfig.SleepTime = textBoxSleepTime.Text;

                if (checkBox6.Checked)
                    configModel.DoorConfig.CheckTemp = "true";
                else if (checkBox7.Checked)
                    configModel.DoorConfig.CheckTemp = "false";

                configModel.PrintConfig.PrintEnable = checkBox2.Checked ? "true" : "false";
                configModel.PrintConfig.DeviceSN = textBoxDevCode.Text;
                configModel.PrintConfig.DestPort = textBoxDeskPort.Text;
                configModel.PrintConfig.DestIp = textBoxDeskIp.Text;

                if (labelPic.Text == "1")
                    ConfigMethod.UpdateAppConfig("tempEnable", "true");
                else if (labelPic.Text == "2")
                    ConfigMethod.UpdateAppConfig("tempEnable", "false");

                ConfigMethod.SaveConfigModel(configModel);
                MessageBox.Show("配置下发成功", "提示");
            }
            catch (Exception ex) {
                MessageBox.Show("配置下发失败"+ex.Message, "提示");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(comboBoxDbType.Text== "Sqlite")
            {
                MessageBox.Show("暂不支持Sqlite", "提示");
                return;
            }
            try
            {
                DbConfig dbConfig = new DbConfig();
                dbConfig.SqlType = comboBoxDbType.Text == "MySql" ? "1" : "2";
                dbConfig.LocalDatabaseName = textBoxLocalDbName.Text;
                dbConfig.LocalDatabaseUser = textBoxLocalDbUser.Text;
                dbConfig.LocalDatabasePort = textBoxLocalDbPort.Text;
                dbConfig.LocalDatabasePassword = textBoxLocalDbPassWord.Text;

                dbConfig.ServerIp = textBoxServerIp.Text;
                dbConfig.ServerDatabaseName = textBoxServerDbName.Text;
                dbConfig.ServerDatabaseUser = textBoxServerDbUser.Text;
                dbConfig.ServerDatabasePort = textBoxServerDbPort.Text;
                dbConfig.ServerDatabasePassword = textBoxServerDbPassWord.Text;

                dbConfig.ListenPort = textBoxListenPort.Text;
                dbConfig.ServerAppPort = textBoxhttpPort.Text;

                ConfigMethod.SaveDbConfig(dbConfig);
                MessageBox.Show("配置下发成功", "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show("配置下发失败" + ex.Message, "提示");
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            checkBox4.Checked = !checkBox3.Checked;
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            checkBox3.Checked = !checkBox4.Checked;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (labelPic.Text == "1")
            {
                labelPic.Text = "2";
                String picPath = Application.StartupPath + "\\pic\\" + labelPic.Text + ".png";
                pictureBox1.Load(picPath);
            }
            else if (labelPic.Text == "2") {
                labelPic.Text = "1";
                String picPath = Application.StartupPath + "\\pic\\" + labelPic.Text + ".png";
                pictureBox1.Load(picPath);
                
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            TempConfig tempConfig = ConfigMethod.InitTempConfig();
            initForm3(tempConfig);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                TempConfig tempConfig = new TempConfig();

                tempConfig.Correction = textBoxcorrection.Text;
                tempConfig.ParamRegion0= textBoxRegion0.Text;
                tempConfig.ParamRegion11= textBoxRegion11.Text;
                tempConfig.ParamRegion12= textBoxRegion12.Text;

                tempConfig.ParamRegion21=textBoxRegion21.Text;
                tempConfig.ParamRegion22=textBoxRegion22.Text;
                tempConfig.ParamRegion31=textBoxRegion31.Text;
                tempConfig.ParamRegion32=textBoxRegion32.Text;

                tempConfig.Separate0=textBoxseparate0.Text;
                tempConfig.Separate1=textBoxseparate1.Text;
                tempConfig.Separate2 =textBoxseparate2.Text;
                tempConfig.Separate3=textBoxseparate3.Text;

                tempConfig.ParamHigh=textBoxparamHigh.Text;
                tempConfig.Tnor=textBoxtnor.Text;
                tempConfig.Tabnor=textBoxtabnor.Text;
                
                ConfigMethod.SaveTempConfig(tempConfig);
                MessageBox.Show("配置下发成功", "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show("配置下发失败" + ex.Message, "提示");
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            checkBox7.Checked = !checkBox6.Checked;
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            checkBox6.Checked = !checkBox7.Checked;
        }


    }
}
