using Microsoft.Office.Interop.Excel;
using System;
using System.Drawing;
using System.Net;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SLZ_4
{
    public partial class ConfigScanner : Form
    {
        public string[] ports;
        public int scanNum;         //扫码枪数量
        public int scanType;        //扫码枪类型 0：普通扫码枪， 1：工业扫码枪-串口，2：工业扫码枪-网路
        public ConfigScanner()
        {
            InitializeComponent();

            cmbScanType.Items.Add("普通扫码枪");
            cmbScanType.Items.Add("工业扫码枪-串口");
            cmbScanType.Items.Add("工业扫码枪-网络");
        }

        private void Form_Load(object sender, EventArgs e)
        {
            uiGroupNetwork1.Location = uiGroupCom1.Location;
            uiGroupNetwork2.Location = uiGroupCom2.Location;
        }
        public void setConfig()
        {
            refreshPort();
            cmbScanType.SelectedIndex = scanType;
            if (scanType == (int)SCANNER_TYPE.COMMON || scanType == (int)SCANNER_TYPE.INDUSTRY_COM)
            {
                CH1CodeBaud.Text = FormMain.form.arrScannerObj[0].iBaudrate.ToString();
                CH1CodePort.Text = FormMain.form.arrScannerObj[0].sPort;

                CH2CodeBaud.Text = FormMain.form.arrScannerObj[1].iBaudrate.ToString();
                CH2CodePort.Text = FormMain.form.arrScannerObj[1].sPort;
            }
            else if (scanType == (int)SCANNER_TYPE.INDUSTRY_NETWORK)
            {
                textIP1.Text = FormMain.form.arrScannerObj[0].sIpaddr;
                textPort1.Text = FormMain.form.arrScannerObj[0].iPort.ToString();

                textIP2.Text = FormMain.form.arrScannerObj[1].sIpaddr;
                textPort2.Text = FormMain.form.arrScannerObj[1].iPort.ToString();
            }

            showComm1Status();
            showComm2Status();
            showNetwork1Status();
            showNetwork2Status();
            ShowScanBox();
        }

        public void showComm1Status()
        {
            if (FormMain.form.serialPortCode1.IsOpen)
            {
                CH1CodeIsComm.Text = "已打开";
                CH1CodeIsComm.BackColor = Color.Green;
                CH1CodeCon.Enabled = false;
                CH1CodeBreak.Enabled = true;
            }
            else
            {
                CH1CodeIsComm.Text = "未打开";
                CH1CodeIsComm.BackColor = Color.Red;
                CH1CodeCon.Enabled = true;
                CH1CodeBreak.Enabled = false;
            }
        }
        public void showComm2Status()
        {
            if (FormMain.form.serialPortCode2.IsOpen)
            {
                CH2CodeIsComm.Text = "已打开";
                CH2CodeIsComm.BackColor = Color.Green;
                CH2CodeCon.Enabled = false;
                CH2CodeBreak.Enabled = true;
            }
            else
            {
                CH2CodeIsComm.Text = "未打开";
                CH2CodeIsComm.BackColor = Color.Red;
                CH2CodeCon.Enabled = true;
                CH2CodeBreak.Enabled = false;
            }
        }
        public void showNetwork1Status()
        {
            //网口1
            if (FormMain.form.arrScanNetwork[0] != null && FormMain.form.arrScanNetwork[0].IsConnected)
            {
                CH1NetworkComm.Text = "已连接";
                CH1NetworkComm.BackColor = Color.Green;
                buttonConnect1.Enabled = false;
                buttonDisconnect1.Enabled = true;
            }
            else
            {
                CH1NetworkComm.Text = "未连接";
                CH1NetworkComm.BackColor = Color.Red;
                buttonConnect1.Enabled = true;
                buttonDisconnect1.Enabled = false;
            }
        }
        public void showNetwork2Status()
        {
            //网口2
            if (FormMain.form.arrScanNetwork[1] != null && FormMain.form.arrScanNetwork[1].IsConnected)
            {
                CH2NetworkComm.Text = "已连接";
                CH2NetworkComm.BackColor = Color.Green;
                buttonConnect2.Enabled = false;
                buttonDisconnect2.Enabled = true;
            }
            else
            {
                CH2NetworkComm.Text = "未连接";
                CH2NetworkComm.BackColor = Color.Red;
                buttonConnect2.Enabled = true;
                buttonDisconnect2.Enabled = false;
            }
        }
        public void refreshPort()
        {
            CH1CodePort.Items.Clear();
            CH2CodePort.Items.Clear();
            ports = System.IO.Ports.SerialPort.GetPortNames();
            if (ports.Length > 0)
            {
                CH1CodePort.Items.AddRange(ports);
                CH2CodePort.Items.AddRange(ports);
            }
        }
        private void CH1CodeRefresh_Click(object sender, EventArgs e)
        {
            refreshPort();
        }

        private void CH1CodeCon_Click(object sender, EventArgs e)
        {
            FormMain.form.arrScannerObj[0].iBaudrate = int.Parse(CH1CodeBaud.Text);
            FormMain.form.arrScannerObj[0].sPort = CH1CodePort.Text;
            Config.saveScanner(0);

            FormMain.form.openScanner1();
            showComm1Status();
        }

        private void CH1CodeBreak_Click(object sender, EventArgs e)
        {
            if (FormMain.form.serialPortCode1.IsOpen)
            {
                FormMain.form.serialPortCode1.Close();
            }

            showComm1Status();
        }

        private void CH2CodeCon_Click(object sender, EventArgs e)
        {
            FormMain.form.arrScannerObj[1].iBaudrate = int.Parse(CH1CodeBaud.Text);
            FormMain.form.arrScannerObj[1].sPort = CH2CodePort.Text;
            Config.saveScanner(1);

            FormMain.form.openScanner2();
            showComm2Status();
        }

        private void CH2CodeBreak_Click(object sender, EventArgs e)
        {
            if (FormMain.form.serialPortCode2.IsOpen)
            {
                FormMain.form.serialPortCode2.Close();
            }

            showComm2Status();
        }

        private void ShowScanBox()
        {
            cmbScanNum.SelectedIndex = scanNum - 1;
            if (scanType == (int)SCANNER_TYPE.COMMON || scanType == (int)SCANNER_TYPE.INDUSTRY_COM)
            {
                uiGroupCom1.Visible = true;
                uiGroupCom2.Visible = cmbScanNum.SelectedIndex == 1 ? true : false;

                uiGroupNetwork1.Visible = false;
                uiGroupNetwork2.Visible = false;
                CH1CodeRefresh.Visible = true;
            }
            else if (scanType == (int)SCANNER_TYPE.INDUSTRY_NETWORK)
            {
                uiGroupNetwork1.Visible = true;
                uiGroupNetwork2.Visible = cmbScanNum.SelectedIndex == 1 ? true : false;

                uiGroupCom1.Visible = false;
                uiGroupCom2.Visible = false;
                CH1CodeRefresh.Visible = false;
            }
        }
        private void cmbScanNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            scanNum = cmbScanNum.SelectedIndex + 1;
            Config.saveScanNumber(scanNum);
            ShowScanBox();
        }
        private void cmbScanType_SelectedIndexChanged(object sender, EventArgs e)
        {
            scanType = cmbScanType.SelectedIndex;
            Config.saveScanType(scanType);
            ShowScanBox();
        }
        private void buttonConnect1_Click(object sender, EventArgs e)
        {
            FormMain.form.arrScannerObj[0].iPort = int.Parse(textPort1.Text);
            FormMain.form.arrScannerObj[0].sIpaddr = textIP1.Text;
            Config.saveScanner(0);

            FormMain.form.addScanNetwork(0);
            FormMain.form.openScannerNetwork(0);
            showNetwork1Status();
        }

        private void buttonDisconnect1_Click(object sender, EventArgs e)
        {
            FormMain.form.closeScannerNetwork(0);
            showNetwork1Status();
        }

        private void buttonConnect2_Click(object sender, EventArgs e)
        {
            FormMain.form.arrScannerObj[1].iPort = int.Parse(textPort2.Text);
            FormMain.form.arrScannerObj[1].sIpaddr = textIP2.Text;
            Config.saveScanner(1);

            FormMain.form.addScanNetwork(1);
            FormMain.form.openScannerNetwork(1);
            showNetwork2Status();
        }

        private void buttonDisconnect2_Click(object sender, EventArgs e)
        {
            FormMain.form.closeScannerNetwork(1);
            showNetwork2Status();
        }

        public static bool IsValidIp(string ip)
        {
            // 基础格式检查
            if (!IPAddress.TryParse(ip, out _) || ip.Split('.').Length != 4)
                return false;

            // 每段数值范围检查
            string pattern = @"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$";
            return Regex.IsMatch(ip, pattern);
        }

    }
}
