using Newtonsoft.Json.Linq;
using AssistClient.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace AssistClient
{
    public partial class FrmSysSetting : Form
    {
        public FrmSysSetting()
        {
            InitializeComponent();
        }
      
        private void initData()
        {
            string configPath = Application.StartupPath + "\\SysConfig.ini";
            string lsTmp = "";

            var comNameList = Utils.SSerialPort.getPortNames().ToList();
            comNameList.ForEach(comName =>
            {
                this.cobLight_PortNum.Items.Add(comName);  
            });

            //Server
            this.txtDBConStr.Text = Util.ReadIniValue(configPath, "Server", "DBConStr");   
            this.txtServerIp.Text = Util.ReadIniValue(configPath, "Server", "ServerIP");
            lsTmp = Util.ReadIniValue(configPath, "Server", "ServerPort");
            if (Util.IsNumber(lsTmp)) 
                this.numServerPort.Value = Convert.ToInt32(lsTmp);

            //Skip 
            this.chkSkipAxis0.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipAxis0") == "1";
            this.chkSkipAxis1.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipAxis1") == "1";
            this.chkSkipAxis2.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipAxis2") == "1";
            this.chkSkipAxis3.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipAxis3") == "1";

            this.chkSkipLight.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipLight") == "1"; 
            this.chkSkipScannerCC.Checked = Util.ReadIniValue(configPath, "SKIP", "SkipScannerCC") == "1"; 

            //DEV
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_PulseOutMode");
            EnumUtil.BindToEnumName<AxisPulseOutMode>(this.cobAxis0PulseOutMode, typeof(AxisPulseOutMode), lsTmp == "" ? AxisPulseOutMode.O_CW_CCW : (AxisPulseOutMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_PulseOutMode");
            EnumUtil.BindToEnumName<AxisPulseOutMode>(this.cobAxis1PulseOutMode, typeof(AxisPulseOutMode), lsTmp == "" ? AxisPulseOutMode.O_CW_CCW : (AxisPulseOutMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_PulseOutMode");
            EnumUtil.BindToEnumName<AxisPulseOutMode>(this.cobAxis2PulseOutMode, typeof(AxisPulseOutMode), lsTmp == "" ? AxisPulseOutMode.O_CW_CCW : (AxisPulseOutMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_PulseOutMode");
            EnumUtil.BindToEnumName<AxisPulseOutMode>(this.cobAxis3PulseOutMode, typeof(AxisPulseOutMode), lsTmp == "" ? AxisPulseOutMode.O_CW_CCW : (AxisPulseOutMode)Convert.ToInt32(lsTmp));
             
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeMode");
            EnumUtil.BindToEnumName<AxitHomeMode>(this.cobAxis0HomeMode, typeof(AxitHomeMode), lsTmp == "" ? AxitHomeMode.MODE11_LmtSearch_Ref : (AxitHomeMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeMode");
            EnumUtil.BindToEnumName<AxitHomeMode>(this.cobAxis1HomeMode, typeof(AxitHomeMode), lsTmp == "" ? AxitHomeMode.MODE11_LmtSearch_Ref : (AxitHomeMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeMode");
            EnumUtil.BindToEnumName<AxitHomeMode>(this.cobAxis2HomeMode, typeof(AxitHomeMode), lsTmp == "" ? AxitHomeMode.MODE11_LmtSearch_Ref : (AxitHomeMode)Convert.ToInt32(lsTmp));
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeMode");
            EnumUtil.BindToEnumName<AxitHomeMode>(this.cobAxis3HomeMode, typeof(AxitHomeMode), lsTmp == "" ? AxitHomeMode.MODE11_LmtSearch_Ref : (AxitHomeMode)Convert.ToInt32(lsTmp));


            this.cobLight_PortNum.Text = "COM" + Util.ReadIniValue(configPath, "DEV", "Light_PortNum");  

            this.cobAxis0HomeDir.SelectedIndex = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "Axis0_HomeDir"));
            this.cobAxis1HomeDir.SelectedIndex = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "Axis1_HomeDir"));
            this.cobAxis2HomeDir.SelectedIndex = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "Axis2_HomeDir"));
            this.cobAxis3HomeDir.SelectedIndex = Convert.ToInt32(Util.ReadIniValue(configPath, "DEV", "Axis3_HomeDir"));

            #region 回HOME速度
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelLow");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_HomeVelLow = JArray.Parse(lsTmp);
                this.numAxis0HomeVelLow.Value = Axis_HomeVelLow.Count > 0 ? (decimal)Axis_HomeVelLow[0] : 0;
                this.numAxis1HomeVelLow.Value = Axis_HomeVelLow.Count > 1 ? (decimal)Axis_HomeVelLow[1] : 0;
                this.numAxis2HomeVelLow.Value = Axis_HomeVelLow.Count > 2 ? (decimal)Axis_HomeVelLow[2] : 0;
                this.numAxis3HomeVelLow.Value = Axis_HomeVelLow.Count > 3 ? (decimal)Axis_HomeVelLow[3] : 0; 
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelHigh");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_HomeVelHigh = JArray.Parse(lsTmp);
                this.numAxis0HomeVelHigh.Value = Axis_HomeVelHigh.Count > 0 ? (decimal)Axis_HomeVelHigh[0] : 0;
                this.numAxis1HomeVelHigh.Value = Axis_HomeVelHigh.Count > 1 ? (decimal)Axis_HomeVelHigh[1] : 0;
                this.numAxis2HomeVelHigh.Value = Axis_HomeVelHigh.Count > 2 ? (decimal)Axis_HomeVelHigh[2] : 0;
                this.numAxis3HomeVelHigh.Value = Axis_HomeVelHigh.Count > 3 ? (decimal)Axis_HomeVelHigh[3] : 0;
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeAcc");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_HomeAcc = JArray.Parse(lsTmp);
                this.numAxis0HomeAcc.Value = Axis_HomeAcc.Count > 0 ? (decimal)Axis_HomeAcc[0] : 0;
                this.numAxis1HomeAcc.Value = Axis_HomeAcc.Count > 1 ? (decimal)Axis_HomeAcc[1] : 0;
                this.numAxis2HomeAcc.Value = Axis_HomeAcc.Count > 2 ? (decimal)Axis_HomeAcc[2] : 0;
                this.numAxis3HomeAcc.Value = Axis_HomeAcc.Count > 3 ? (decimal)Axis_HomeAcc[3] : 0;
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeDec");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_HomeDec = JArray.Parse(lsTmp);
                this.numAxis0HomeDec.Value = Axis_HomeDec.Count > 0 ? (decimal)Axis_HomeDec[0] : 0;
                this.numAxis1HomeDec.Value = Axis_HomeDec.Count > 1 ? (decimal)Axis_HomeDec[1] : 0;
                this.numAxis2HomeDec.Value = Axis_HomeDec.Count > 2 ? (decimal)Axis_HomeDec[2] : 0;
                this.numAxis3HomeDec.Value = Axis_HomeDec.Count > 3 ? (decimal)Axis_HomeDec[3] : 0;
            }
            #endregion
            #region JOG速度
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_JogVelLow");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_JogVelLow = JArray.Parse(lsTmp);
                this.numAxis0JogVelLow.Value = Axis_JogVelLow.Count > 0 ? (decimal)Axis_JogVelLow[0] : 0;
                this.numAxis1JogVelLow.Value = Axis_JogVelLow.Count > 1 ? (decimal)Axis_JogVelLow[1] : 0;
                this.numAxis2JogVelLow.Value = Axis_JogVelLow.Count > 2 ? (decimal)Axis_JogVelLow[2] : 0;
                this.numAxis3JogVelLow.Value = Axis_JogVelLow.Count > 3 ? (decimal)Axis_JogVelLow[3] : 0;
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_JogVelHigh");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_JogVelHigh = JArray.Parse(lsTmp);
                this.numAxis0JogVelHigh.Value = Axis_JogVelHigh.Count > 0 ? (decimal)Axis_JogVelHigh[0] : 0;
                this.numAxis1JogVelHigh.Value = Axis_JogVelHigh.Count > 1 ? (decimal)Axis_JogVelHigh[1] : 0;
                this.numAxis2JogVelHigh.Value = Axis_JogVelHigh.Count > 2 ? (decimal)Axis_JogVelHigh[2] : 0;
                this.numAxis3JogVelHigh.Value = Axis_JogVelHigh.Count > 3 ? (decimal)Axis_JogVelHigh[3] : 0;
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_JogAcc");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_JogAcc = JArray.Parse(lsTmp);
                this.numAxis0JogAcc.Value = Axis_JogAcc.Count > 0 ? (decimal)Axis_JogAcc[0] : 0;
                this.numAxis1JogAcc.Value = Axis_JogAcc.Count > 1 ? (decimal)Axis_JogAcc[1] : 0;
                this.numAxis2JogAcc.Value = Axis_JogAcc.Count > 2 ? (decimal)Axis_JogAcc[2] : 0;
                this.numAxis3JogAcc.Value = Axis_JogAcc.Count > 3 ? (decimal)Axis_JogAcc[3] : 0;
            }
            lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_JogDec");
            if (!string.IsNullOrWhiteSpace(lsTmp))
            {
                JArray Axis_JogDec = JArray.Parse(lsTmp);
                this.numAxis0JogDec.Value = Axis_JogDec.Count > 0 ? (decimal)Axis_JogDec[0] : 0;
                this.numAxis1JogDec.Value = Axis_JogDec.Count > 1 ? (decimal)Axis_JogDec[1] : 0;
                this.numAxis2JogDec.Value = Axis_JogDec.Count > 2 ? (decimal)Axis_JogDec[2] : 0;
                this.numAxis3JogDec.Value = Axis_JogDec.Count > 3 ? (decimal)Axis_JogDec[3] : 0;
            }
            #endregion

            this.txtIO_DevNum.Text = Util.ReadIniValue(configPath, "DEV", "IOCard_DeviceNum"); 
            this.txtSizeEnginePath.Text= Util.ReadIniValue(configPath, "DEV", "SizeEnginePath");
            this.txtSizeBmp_Path.Text= Util.ReadIniValue(configPath, "DEV", "SizeBmp_Path");
            this.ckbSizeBmp_IsSave.Checked = (Util.ReadIniValue(configPath, "DEV", "SizeBmp_IsSave") == "1");
            this.txtSizeRepairTable.Text = Util.ReadIniValue(configPath, "DEV", "SizeRepairTablePath");
            //LOG
            this.txtLogPath.Text = Util.ReadIniValue(configPath, "LOG", "LogPath"); 
        }
        private void FrmSetParams_Load(object sender, EventArgs e)
        {
            initData();
        }

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string configPath = Application.StartupPath + "\\SysConfig.ini";
                 
                //DEV  
                string Light_PortNum = this.cobLight_PortNum.Text.Trim();  
                if (Light_PortNum == "" )
                    throw new Exception("请设置设备COM端口！"); 

                string IOCard_DeviceNum = this.txtIO_DevNum.Text.Trim();
                if (IOCard_DeviceNum == "")
                    throw new Exception("请设置I/O设备编号 ！");
                  
                //LOG
                string LogPath = this.txtLogPath.Text.Trim();
                if (LogPath == "")
                    throw new Exception("请设置日志保存路径 ！");

                //最后统一保存
                //Skip
                Util.WriteIniValue(configPath, "SKIP", "SkipAxis0", this.chkSkipAxis0.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipAxis1", this.chkSkipAxis1.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipAxis2", this.chkSkipAxis2.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipAxis3", this.chkSkipAxis3.Checked ? "1" : "0");

                Util.WriteIniValue(configPath, "SKIP", "SkipLight", this.chkSkipLight.Checked ? "1" : "0");
                //Util.WriteIniValue(configPath, "SKIP", "SkipScannerGL", this.chkSkipScannerGL.Checked ? "1" : "0");
                Util.WriteIniValue(configPath, "SKIP", "SkipScannerCC", this.chkSkipScannerCC.Checked ? "1" : "0");

                Utils.Util.WriteIniValue(configPath, "DEV", "Light_PortNum", Light_PortNum.Substring(3));

                //DEV 
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis0_PulseOutMode", ((int)EnumUtil.GetSelectedItemToEnum<AxisPulseOutMode>(this.cobAxis0PulseOutMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis1_PulseOutMode", ((int)EnumUtil.GetSelectedItemToEnum<AxisPulseOutMode>(this.cobAxis1PulseOutMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis2_PulseOutMode", ((int)EnumUtil.GetSelectedItemToEnum<AxisPulseOutMode>(this.cobAxis2PulseOutMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis3_PulseOutMode", ((int)EnumUtil.GetSelectedItemToEnum<AxisPulseOutMode>(this.cobAxis3PulseOutMode)).ToString());
               
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis0_HomeMode", ((int)EnumUtil.GetSelectedItemToEnum<AxitHomeMode>(this.cobAxis0HomeMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis1_HomeMode", ((int)EnumUtil.GetSelectedItemToEnum<AxitHomeMode>(this.cobAxis1HomeMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis2_HomeMode", ((int)EnumUtil.GetSelectedItemToEnum<AxitHomeMode>(this.cobAxis2HomeMode)).ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis3_HomeMode", ((int)EnumUtil.GetSelectedItemToEnum<AxitHomeMode>(this.cobAxis3HomeMode)).ToString());
                 
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis0_HomeDir", this.cobAxis0HomeDir.SelectedIndex.ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis1_HomeDir", this.cobAxis1HomeDir.SelectedIndex.ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis2_HomeDir", this.cobAxis2HomeDir.SelectedIndex.ToString());
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis3_HomeDir", this.cobAxis3HomeDir.SelectedIndex.ToString());

                #region 回HOME速度
                JArray Axis_HomeVelLow = new JArray()
                {
                    numAxis0HomeVelLow.Value,numAxis1HomeVelLow.Value,numAxis2HomeVelLow.Value,numAxis3HomeVelLow.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_HomeVelLow", Axis_HomeVelLow.ToString().Replace("\r\n",""));
                
                JArray Axis_HomeVelHigh = new JArray()
                {
                    numAxis0HomeVelHigh.Value,numAxis1HomeVelHigh.Value,numAxis2HomeVelHigh.Value,numAxis3HomeVelHigh.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_HomeVelHigh", Axis_HomeVelHigh.ToString().Replace("\r\n", ""));
                
                JArray Axis_HomeAcc = new JArray()
                {
                    numAxis0HomeAcc.Value,numAxis1HomeAcc.Value,numAxis2HomeAcc.Value,numAxis3HomeAcc.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_HomeAcc", Axis_HomeAcc.ToString().Replace("\r\n", ""));

                JArray Axis_HomeDec = new JArray()
                {
                    numAxis0HomeDec.Value,numAxis1HomeDec.Value,numAxis2HomeDec.Value,numAxis3HomeDec.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_HomeDec", Axis_HomeDec.ToString().Replace("\r\n", ""));
                #endregion
                #region 回Jog速度
                JArray Axis_JogVelLow = new JArray()
                {
                    numAxis0JogVelLow.Value,numAxis1JogVelLow.Value,numAxis2JogVelLow.Value,numAxis3JogVelLow.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_JogVelLow", Axis_JogVelLow.ToString().Replace("\r\n", ""));

                JArray Axis_JogVelHigh = new JArray()
                {
                    numAxis0JogVelHigh.Value,numAxis1JogVelHigh.Value,numAxis2JogVelHigh.Value,numAxis3JogVelHigh.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_JogVelHigh", Axis_JogVelHigh.ToString().Replace("\r\n", ""));

                JArray Axis_JogAcc = new JArray()
                {
                    numAxis0JogAcc.Value,numAxis1JogAcc.Value,numAxis2JogAcc.Value,numAxis3JogAcc.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_JogAcc", Axis_JogAcc.ToString().Replace("\r\n", ""));

                JArray Axis_JogDec = new JArray()
                {
                    numAxis0JogDec.Value,numAxis1JogDec.Value,numAxis2JogDec.Value,numAxis3JogDec.Value
                };
                Utils.Util.WriteIniValue(configPath, "DEV", "Axis_JogDec", Axis_JogDec.ToString().Replace("\r\n", ""));
                #endregion

                Utils.Util.WriteIniValue(configPath, "DEV", "IOCard_DeviceNum", IOCard_DeviceNum);  
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeEnginePath", this.txtSizeEnginePath.Text.Trim());
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeBmp_Path", this.txtSizeBmp_Path.Text.Trim());
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeBmp_IsSave", this.ckbSizeBmp_IsSave.Checked?"1":"0");
                Utils.Util.WriteIniValue(configPath, "DEV", "SizeRepairTablePath", this.txtSizeRepairTable.Text.Trim());

                //
                Utils.Util.WriteIniValue(configPath, "Server", "DBConStr", this.txtDBConStr.Text.Trim());
                Utils.Util.WriteIniValue(configPath, "Server", "ServerIP", this.txtServerIp.Text.Trim());
                Utils.Util.WriteIniValue(configPath, "Server", "ServerPort", ((int)this.numServerPort.Value).ToString());
                //LOG
                Utils.Util.WriteIniValue(configPath, "LOG", "LogPath", LogPath);

                //
                this.Hide();
                MessageBox.Show("保存成功，生效需重启程序！");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
         
         

        private void txtLogPath_Click(object sender, EventArgs e)
        {
            this.txtLogPath.Text = FileUtil.selectFolder(Application.StartupPath);
        }
        private void txtSizeEnginePath_Click(object sender, EventArgs e)
        {
            this.txtSizeEnginePath.Text = FileUtil.selectFolder(Application.StartupPath);
        }
        private void txtSizeRepairTable_Click(object sender, EventArgs e)
        {
            this.txtSizeRepairTable.Text = FileUtil.openFile();
        }
        private void txtSizeBmp_Path_Click(object sender, EventArgs e)
        {
            this.txtSizeBmp_Path.Text = FileUtil.selectFolder(Application.StartupPath);
        }

        private void btnInitDB_Click(object sender, EventArgs e)
        {
            try
            {
                Service.InitDB.initDB( false);
                MessageBox.Show("成功！"); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            } 
        }
         
    }
}
