using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SLZ_4
{
    class Config
    {
        public static void loadConfig()
        {
            FormMain.form.arrDevParam[0].iPort = 9600;
            //FormMain.form.arrDevParam[1].iPort = 9600;
            string strRead = FormMain.form.configIni.IniReadValue("Device", "CH1IP", "");
            if (strRead != null)
            {
                FormMain.form.arrDevParam[0].sDevAddr = strRead;
            }
            strRead = FormMain.form.configIni.IniReadValue("Device", "CH1Port", "");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrDevParam[0].iPort = int.Parse(strRead);
            }

            //strRead = FormMain.form.configIni.IniReadValue("Device", "CH2IP", "");
            //if (strRead != null)
            //{
            //    FormMain.form.arrDevParam[1].sDevAddr = strRead;
            //}
            //strRead = FormMain.form.configIni.IniReadValue("Device", "CH2Port", "");
            //if (strRead != null && strRead.Length > 0)
            //{
            //    FormMain.form.arrDevParam[1].iPort = int.Parse(strRead);
            //}

            //密码信息
            strRead = FormMain.form.configIni.IniReadValue("User", "Password", "123456");
            if (strRead != null)
            {
                FormMain.form.logonForm.password = strRead;
            }
            //操作员
            strRead = FormMain.form.configIni.IniReadValue("User", "Operator", "");
            if (strRead != null)
            {
                FormMain.form.strOperator = strRead;
            }
        }

        public static void savePassword(string password)
        {
            FormMain.form.configIni.IniWriteValue("User", "Password", password);
        }

        public static void saveOperator(string sOperator)
        {
            FormMain.form.configIni.IniWriteValue("User", "Operator", sOperator);
        }

        public static bool saveDevParam(int i)
        {
            if (i == 0)
            {
                FormMain.form.configIni.IniWriteValue("Device", "CH1IP", FormMain.form.arrDevParam[i].sDevAddr);
                FormMain.form.configIni.IniWriteValue("Device", "CH1Port", FormMain.form.arrDevParam[i].iPort.ToString());
            }
            else if (i == 1)
            {
                FormMain.form.configIni.IniWriteValue("Device", "CH2IP", FormMain.form.arrDevParam[i].sDevAddr);
                FormMain.form.configIni.IniWriteValue("Device", "CH2Port", FormMain.form.arrDevParam[i].iPort.ToString());
            }
            return true;
        }

        public static void loadScanParam()
        {
            string strRead = FormMain.form.configIni.IniReadValue("Code", "CH1CheckCode", "false");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScanParam[0].bCheckLen = bool.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Code", "CH1CodeLength", "0");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScanParam[0].iCodeLength = int.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Code", "CH1ScanStart", "false");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScanParam[0].bStart = bool.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Code", "CH2CheckCode", "false");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScanParam[1].bCheckLen = bool.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Code", "CH2CodeLength", "0");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScanParam[1].iCodeLength = int.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Code", "CH2ScanStart", "false");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScanParam[1].bStart = bool.Parse(strRead);
            }
        }
        public static bool saveScanParam(int i)
        {
            if (i == 0)
            {
                FormMain.form.configIni.IniWriteValue("Code", "CH1CheckCode", FormMain.form.arrScanParam[i].bCheckLen.ToString());
                FormMain.form.configIni.IniWriteValue("Code", "CH1CodeLength", FormMain.form.arrScanParam[i].iCodeLength.ToString());
                FormMain.form.configIni.IniWriteValue("Code", "CH1ScanStart", FormMain.form.arrScanParam[i].bStart.ToString());
            }
            else if (i == 1)
            {
                FormMain.form.configIni.IniWriteValue("Code", "CH2CheckCode", FormMain.form.arrScanParam[i].bCheckLen.ToString());
                FormMain.form.configIni.IniWriteValue("Code", "CH2CodeLength", FormMain.form.arrScanParam[i].iCodeLength.ToString());
                FormMain.form.configIni.IniWriteValue("Code", "CH2ScanStart", FormMain.form.arrScanParam[i].bStart.ToString());
            }

            return true;
        }

        public static void loadScanner()
        {
            string strRead = FormMain.form.configIni.IniReadValue("Scanner", "Number", "1");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.scannerForm.scanNum = int.Parse(strRead);
            }

            strRead = FormMain.form.configIni.IniReadValue("Scanner", "ScanType", "0");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.scannerForm.scanType = int.Parse(strRead);
            }
            //扫码枪1
            FormMain.form.arrScannerObj[0].sPort = "";
            FormMain.form.arrScannerObj[0].iBaudrate = 9600;
            strRead = FormMain.form.configIni.IniReadValue("Scanner", "Port1", "");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScannerObj[0].sPort = strRead;
            }
            strRead = FormMain.form.configIni.IniReadValue("Scanner", "Baudrate1", "9600");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScannerObj[0].iBaudrate = int.Parse(strRead);
            }

            strRead = FormMain.form.configIni.IniReadValue("Scanner", "NetPort1", "");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScannerObj[0].iPort = int.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Scanner", "IpAddress1", "");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScannerObj[0].sIpaddr = strRead;
            }

            //扫码枪2
            FormMain.form.arrScannerObj[1].sPort = "";
            FormMain.form.arrScannerObj[1].iBaudrate = 9600;
            strRead = FormMain.form.configIni.IniReadValue("Scanner", "Port2", "");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScannerObj[1].sPort = strRead;
            }
            strRead = FormMain.form.configIni.IniReadValue("Scanner", "Baudrate2", "9600");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScannerObj[1].iBaudrate = int.Parse(strRead);
            }

            strRead = FormMain.form.configIni.IniReadValue("Scanner", "NetPort2", "");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScannerObj[1].iPort = int.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Scanner", "IpAddress2", "");
            if (strRead != null && strRead.Length > 0)
            {
                FormMain.form.arrScannerObj[1].sIpaddr = strRead;
            }
        }
        /// <summary>
        /// 保存扫码枪信息
        /// </summary>
        /// <param name="i">序号</param>
        public static void saveScanner(int i)
        {
            if (i == 0)
            {
                FormMain.form.configIni.IniWriteValue("Scanner", "Port1", FormMain.form.arrScannerObj[i].sPort);
                FormMain.form.configIni.IniWriteValue("Scanner", "Baudrate1", FormMain.form.arrScannerObj[i].iBaudrate.ToString());

                FormMain.form.configIni.IniWriteValue("Scanner", "NetPort1", FormMain.form.arrScannerObj[i].iPort.ToString());
                FormMain.form.configIni.IniWriteValue("Scanner", "IpAddress1", FormMain.form.arrScannerObj[i].sIpaddr);
            }
            else if (i == 1)
            {
                FormMain.form.configIni.IniWriteValue("Scanner", "Port2", FormMain.form.arrScannerObj[i].sPort);
                FormMain.form.configIni.IniWriteValue("Scanner", "Baudrate2", FormMain.form.arrScannerObj[i].iBaudrate.ToString());

                FormMain.form.configIni.IniWriteValue("Scanner", "NetPort2", FormMain.form.arrScannerObj[i].iPort.ToString());
                FormMain.form.configIni.IniWriteValue("Scanner", "IpAddress2", FormMain.form.arrScannerObj[i].sIpaddr);
            }
        }
        public static void saveScanNumber(int iNum)
        {
            FormMain.form.configIni.IniWriteValue("Scanner", "Number", iNum.ToString());
        }

        public static void saveScanType(int iType)
        {
            FormMain.form.configIni.IniWriteValue("Scanner", "ScanType", iType.ToString());
        }

        public static void loadStorage(ref STORAGE_PARAM pParam)
        {
            string  strRead = FormMain.form.configIni.IniReadValue("Storage", "ExportTXT", "true");
            if (strRead != null && strRead.Length > 0)
            {
                pParam.bTxt = bool.Parse(strRead);
            }
            else
            {
                pParam.bTxt = true;
            }
            strRead = FormMain.form.configIni.IniReadValue("Storage", "ExportCSV", "false");
            if (strRead != null && strRead.Length > 0)
            {
                pParam.bCSV = bool.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Storage", "ExportExcel", "false");
            if (strRead != null && strRead.Length > 0)
            {
                pParam.bExcel = bool.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Storage", "ExportSQL", "false");
            if (strRead != null && strRead.Length > 0)
            {
                pParam.bSQL = bool.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Storage", "StoragePath", "");
            if (strRead != null && strRead.Length > 0)
            {
                pParam.sPath = strRead;
            }
            else
            {
                pParam.sPath = AppDomain.CurrentDomain.BaseDirectory + "Storage";
            }
        }
        public static void saveStorage(STORAGE_PARAM param)
        {
            FormMain.form.configIni.IniWriteValue("Storage", "ExportTXT", param.bTxt.ToString());
            FormMain.form.configIni.IniWriteValue("Storage", "ExportCSV", param.bCSV.ToString());
            FormMain.form.configIni.IniWriteValue("Storage", "ExportExcel", param.bExcel.ToString());
            FormMain.form.configIni.IniWriteValue("Storage", "ExportSQL", param.bSQL.ToString());
            FormMain.form.configIni.IniWriteValue("Storage", "StoragePath", param.sPath);
        }

        public static void loadPrint(ref PRINTER_PARAM pInfo)
        {
            string strRead = FormMain.form.configIni.IniReadValue("Printer", "PrintEnable", "false");
            if (strRead != null && strRead.Length > 0)
            {
                pInfo.bPrint = bool.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Printer", "PrintName", "");
            if (strRead != null)
            {
                pInfo.printName = strRead;
            }
            strRead = FormMain.form.configIni.IniReadValue("Printer", "PrintModel", "");
            if (strRead != null)
            {
                pInfo.printModel = strRead;
            }

            strRead = FormMain.form.configIni.IniReadValue("Printer", "CodePre", "型号RSFP-76*64A-4*8Q-BL-JS-流水号HT");
            if (strRead != null && strRead.Length > 0)
            {
                pInfo.codePre = strRead;
            }
            pInfo.snPre = DateTime.Now.ToString("yyyyMMdd");
            strRead = FormMain.form.configIni.IniReadValue("Printer", "SN", "1");
            if (strRead != null && strRead.Length > 0)
            {
                pInfo.sn = int.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Printer", "CodeSuffix", "-气密");
            if (strRead != null && strRead.Length > 0)
            {
                pInfo.codeSuffix = strRead;
            }
            strRead = FormMain.form.configIni.IniReadValue("Printer", "CodeSuffix2", "PASS");
            if (strRead != null && strRead.Length > 0)
            {
                pInfo.codeSuffix2 = strRead;
            }
        }

        public static void savePrint(ref PRINTER_PARAM pInfo)
        {
            FormMain.form.configIni.IniWriteValue("Printer", "PrintEnable", pInfo.bPrint.ToString());
            FormMain.form.configIni.IniWriteValue("Printer", "PrintName", pInfo.printName);
            FormMain.form.configIni.IniWriteValue("Printer", "PrintModel", pInfo.printModel);

            FormMain.form.configIni.IniWriteValue("Printer", "CodePre", pInfo.codePre);
            FormMain.form.configIni.IniWriteValue("Printer", "SN", pInfo.sn.ToString());
            FormMain.form.configIni.IniWriteValue("Printer", "CodeSuffix", pInfo.codeSuffix);
            FormMain.form.configIni.IniWriteValue("Printer", "CodeSuffix2", pInfo.codeSuffix2);
        }
        
        public static void savePrintSN(ref PRINTER_PARAM pInfo)  //保存SN号
        {
            FormMain.form.configIni.IniWriteValue("Printer", "SNDate", pInfo.snPre);
            FormMain.form.configIni.IniWriteValue("Printer", "SN", pInfo.sn.ToString());
        }

        public static void loadPLC(ref PLC_PARAM plcParam)
        {
            string strRead = FormMain.form.configIni.IniReadValue("PLC", "Port", "");
            plcParam.sPort = "";
            if (strRead != null && strRead.Length > 0)
            {
                plcParam.sPort = strRead;
            }
            strRead = FormMain.form.configIni.IniReadValue("PLC", "Baudrate", "9600");
            if (strRead != null && strRead.Length > 0)
            {
                plcParam.iBaudrate = int.Parse(strRead);
            }
        }

        public static void savePLC(ref PLC_PARAM plcParam)
        {
            FormMain.form.configIni.IniWriteValue("PLC", "Port", plcParam.sPort);
            FormMain.form.configIni.IniWriteValue("PLC", "Baudrate", plcParam.iBaudrate.ToString());
        }

        public static string loadMes(ref MES_PARAM mesParam)
        {
            string strRead = FormMain.form.configIni.IniReadValue("MES", "Url", "");
            if (strRead != null && strRead.Length > 0)
            {
                mesParam.sUrl = strRead;
            }
            strRead = FormMain.form.configIni.IniReadValue("MES", "Port", "");
            if (strRead != null && strRead.Length > 0)
            {
                mesParam.iPort = int.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("MES", "MesCheckIn", "false");
            if (strRead != null && strRead.Length > 0)
            {
                mesParam.bCheckIn = bool.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("MES", "MesCheckOut", "false");
            if (strRead != null && strRead.Length > 0)
            {
                mesParam.bCheckOut = bool.Parse(strRead);
            }
            return strRead;
        }

        public static void saveMes(ref MES_PARAM mesParam)
        {
            FormMain.form.configIni.IniWriteValue("MES", "Url", mesParam.sUrl);
            FormMain.form.configIni.IniWriteValue("MES", "Port", mesParam.iPort.ToString());
            FormMain.form.configIni.IniWriteValue("MES", "MesCheckIn", mesParam.bCheckIn.ToString());
            FormMain.form.configIni.IniWriteValue("MES", "MesCheckOut", mesParam.bCheckOut.ToString());
        }

        public static void loadCount(ref STATISTICS_INFO statisticsInfo)
        {
            string strRead = FormMain.form.configIni.IniReadValue("Statistics", "Count", "");
            if (strRead != null && strRead.Length > 0)
            {
                statisticsInfo.iCount = int.Parse(strRead);
            }

            strRead = FormMain.form.configIni.IniReadValue("Statistics", "Pass", "");
            if (strRead != null && strRead.Length > 0)
            {
                statisticsInfo.iPass = int.Parse(strRead);
            }
            strRead = FormMain.form.configIni.IniReadValue("Statistics", "Failed", "");
            if (strRead != null && strRead.Length > 0)
            {
                statisticsInfo.iFailed = int.Parse(strRead);
            }

            statisticsInfo.iCount = 0;
            statisticsInfo.iPass = 0;
            statisticsInfo.iFailed = 0;
            string strTemp = "";
            for (int i = 0; i < statisticsInfo.ch.Length; i++)
            {
                statisticsInfo.ch[i].iCount = 0;
                statisticsInfo.ch[i].iPass = 0;
                statisticsInfo.ch[i].iFailed = 0;
                strTemp = "Statistics_CH" + (i + 1).ToString();
                strRead = FormMain.form.configIni.IniReadValue(strTemp, "Count", "");
                if (strRead != null && strRead.Length > 0)
                {
                    statisticsInfo.ch[i].iCount = int.Parse(strRead);
                    statisticsInfo.iCount += int.Parse(strRead);
                }
                strRead = FormMain.form.configIni.IniReadValue(strTemp, "Pass", "");
                if (strRead != null && strRead.Length > 0)
                {
                    statisticsInfo.ch[i].iPass = int.Parse(strRead);
                    statisticsInfo.iPass += int.Parse(strRead);
                }
                strRead = FormMain.form.configIni.IniReadValue(strTemp, "Failed", "");
                if (strRead != null && strRead.Length > 0)
                {
                    statisticsInfo.ch[i].iFailed = int.Parse(strRead);
                    statisticsInfo.iFailed += int.Parse(strRead);
                }
            }
        }
        public static void setCount(ref STATISTICS_INFO statisticsInfo)
        {
            FormMain.form.configIni.IniWriteValue("Statistics", "Count", statisticsInfo.iCount.ToString());
            FormMain.form.configIni.IniWriteValue("Statistics", "Pass", statisticsInfo.iPass.ToString());
            FormMain.form.configIni.IniWriteValue("Statistics", "Failed", statisticsInfo.iFailed.ToString());

            string strTemp = "";
            for (int i = 0; i < statisticsInfo.ch.Length; i++)
            {
                strTemp = "Statistics_CH" + (i + 1).ToString();
                FormMain.form.configIni.IniWriteValue(strTemp, "Count", statisticsInfo.ch[i].iCount.ToString());
                FormMain.form.configIni.IniWriteValue(strTemp, "Pass", statisticsInfo.ch[i].iPass.ToString());
                FormMain.form.configIni.IniWriteValue(strTemp, "Failed", statisticsInfo.ch[i].iFailed.ToString());
            }
        }
    }

}
