using Microsoft.Office.Interop.Excel;
using NetWorkHelper.IModels;
using Sunny.UI;
using System;
using System.Drawing;
using System.Reflection.Emit;
using System.Windows.Forms;

namespace SLC1_N
{
    public partial class Form_SaomaSet : Form
    {
        public Form_SaomaSet(string mode)
        {
            InitializeComponent();
        }

        private void Port_Load(object sender, EventArgs e)
        {
            // 串口扫码枪初始化
            if(Form1.f1.serialport_saoma_Enabled)
                SerialPort_Init();

            // TCP扫码枪初始化
            if (Form1.f1.tcpSaoma_Enabled)
                TCP_Init();

            timer_ConnectStatus.Interval = 1000;
            timer_ConnectStatus.Start();
        }

        // 串口扫码枪初始化
        private void SerialPort_Init()
        {
            uiTabControlMenu1.TabPages[1].Enabled = false;
            uiTabControlMenu1.SelectTab($"tabPage_SerialPort");

            // 扫码枪1
            if (Form1.f1.SerialPort_CH1Saoma.IsOpen)
            {
                lb_CH1Com_status.Text = "已打开";
                lb_CH1Com_status.ForeColor = Color.Green;
                cb_CH1PortName.Text = Form1.f1.SerialPort_CH1Saoma.PortName;
                cb_CH1BaudRate.Text = Form1.f1.SerialPort_CH1Saoma.BaudRate.ToString();

                cb_CH1PortName.Enabled = false;
                cb_CH1BaudRate.Enabled = false;
                bt_CH1Connect.Enabled = false;
            }
            else
            {
                lb_CH1Com_status.Text = "未打开";
                lb_CH1Com_status.ForeColor = Color.Red;

                var jsconfig = new JsonConfig("config.json");
                cb_CH1PortName.Text = jsconfig.GetValue<string>("SerialPort_CH1BaudRate", "COM21");
                cb_CH1BaudRate.Text = jsconfig.GetValue<int>("SerialPort_CH1PortName", 9600).ToString();

                cb_CH1PortName.Enabled = true;
                cb_CH1BaudRate.Enabled = true;
                bt_CH1Connect.Enabled = true;
            }

            // 串口扫码枪2
            if (Form1.f1.SerialPort_CH2Saoma.IsOpen)
            {
                lb_CH2Com_status.Text = "已打开";
                lb_CH2Com_status.ForeColor = Color.Green;
                cb_CH2PortName.Text = Form1.f1.SerialPort_CH2Saoma.PortName;
                cb_CH2BaudRate.Text = Form1.f1.SerialPort_CH2Saoma.BaudRate.ToString();

                cb_CH2PortName.Enabled = false;
                cb_CH2BaudRate.Enabled = false;
                bt_CH2Connect.Enabled = false;
            }
            else
            {
                lb_CH2Com_status.Text = "未打开";
                lb_CH2Com_status.ForeColor = Color.Red;

                var jsconfig = new JsonConfig("config.json");
                cb_CH2PortName.Text = jsconfig.GetValue<string>("SerialPort_CH2BaudRate", "COM22");
                cb_CH2BaudRate.Text = jsconfig.GetValue<int>("SerialPort_CH2PortName", 9600).ToString();

                cb_CH2PortName.Enabled = true;
                cb_CH2BaudRate.Enabled = true;
                bt_CH2Connect.Enabled = true;
            }
        }

        // TCP扫码枪初始化
        private void TCP_Init()
        {
            uiTabControlMenu1.TabPages[0].Enabled = false;
            uiTabControlMenu1.SelectTab($"tabPage_TCP");

            var jsconfig = new JsonConfig("config.json");
            tb_CH1TCP_ip.Text = jsconfig.GetValue<string>("Saoma_IP1", "192.168.1.45");
            tb_CH1TCP_port.Text = jsconfig.GetValue<int>("Saoma_Port1", 51111).ToString();
            tb_CH2TCP_ip.Text = jsconfig.GetValue<string>("Saoma_IP2", "192.168.1.46");
            tb_CH2TCP_port.Text = jsconfig.GetValue<int>("Saoma_Port2", 51111).ToString();
        }

        // 串口扫码枪1连接
        private void CodeCon_Click(object sender, EventArgs e)
        {
            try
            {
                //设置端口的参数，包括波特率等
                Form1.f1.SerialPort_CH1Saoma.BaudRate = Convert.ToInt32(cb_CH1BaudRate.Text);
                Form1.f1.SerialPort_CH1Saoma.PortName = cb_CH1PortName.Text;
                Form1.f1.SerialPort_CH1Saoma.DataBits = 8;
                Form1.f1.SerialPort_CH1Saoma.StopBits = System.IO.Ports.StopBits.One;
                Form1.f1.SerialPort_CH1Saoma.Parity = System.IO.Ports.Parity.None;
                Form1.f1.SerialPort_CH1Saoma.Open();
                if (Form1.f1.SerialPort_CH1Saoma.IsOpen)
                {
                    lb_CH1Com_status.Text = "已打开";
                    lb_CH1Com_status.ForeColor = Color.Green;
                    cb_CH1PortName.Enabled = false;
                    cb_CH1BaudRate.Enabled = false;
                    bt_CH1Connect.Enabled = false;

                    //将端口和波特率进行存储
                    var jsconfig = new JsonConfig("config.json");
                    jsconfig.SetValue("SerialPort_CH1BaudRate", cb_CH1BaudRate.Text);
                    jsconfig.SetValue("SerialPort_CH1PortName", cb_CH1PortName.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // 串口扫码枪2连接
        private void CH2CodeCon_Click(object sender, EventArgs e)
        {
            try
            {
                //设置端口的参数，包括波特率等
                Form1.f1.SerialPort_CH2Saoma.BaudRate = Convert.ToInt32(cb_CH2BaudRate.Text);
                Form1.f1.SerialPort_CH2Saoma.PortName = cb_CH2PortName.Text;
                Form1.f1.SerialPort_CH2Saoma.DataBits = 8;
                Form1.f1.SerialPort_CH2Saoma.StopBits = System.IO.Ports.StopBits.One;
                Form1.f1.SerialPort_CH2Saoma.Parity = System.IO.Ports.Parity.None;
                Form1.f1.SerialPort_CH2Saoma.Open();
                if (Form1.f1.SerialPort_CH2Saoma.IsOpen)
                {
                    lb_CH2Com_status.Text = "已打开";
                    lb_CH2Com_status.ForeColor = Color.Green;
                    cb_CH2PortName.Enabled = false;
                    cb_CH2BaudRate.Enabled = false;
                    bt_CH2Connect.Enabled = false;

                    //将端口和波特率进行存储
                    var jsconfig = new JsonConfig("config.json");
                    jsconfig.SetValue("SerialPort_CH2BaudRate", cb_CH2BaudRate.Text);
                    jsconfig.SetValue("SerialPort_CH2PortName", cb_CH2PortName.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // 串口扫码枪1刷新
        private void CodeRefresh_Click(object sender, EventArgs e)
        {
            cb_CH1PortName.Items.Clear();
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            cb_CH1PortName.Items.AddRange(ports);
        }

        // 串口扫码枪2刷新
        private void CH2CodeRefresh_Click(object sender, EventArgs e)
        {
            cb_CH2PortName.Items.Clear();
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            cb_CH2PortName.Items.AddRange(ports);
        }

        // 串口扫码枪1关闭
        private void CodeBreak_Click(object sender, EventArgs e)
        {
            Form1.f1.SerialPort_CH1Saoma.Close();
            if (!Form1.f1.SerialPort_CH1Saoma.IsOpen)
            {
                lb_CH1Com_status.Text = "未打开";
                lb_CH1Com_status.ForeColor = Color.Red;
                cb_CH1PortName.Enabled = true;
                cb_CH1BaudRate.Enabled = true;
                bt_CH1Connect.Enabled = true;
            }
        }

        // 串口扫码枪2关闭
        private void CH2CodeBreak_Click(object sender, EventArgs e)
        {
            Form1.f1.SerialPort_CH2Saoma.Close();
            if (!Form1.f1.SerialPort_CH2Saoma.IsOpen)
            {
                lb_CH2Com_status.Text = "未打开";
                lb_CH2Com_status.ForeColor = Color.Red;
                cb_CH2PortName.Enabled = true;
                cb_CH2BaudRate.Enabled = true;
                bt_CH2Connect.Enabled = true;
            }
        }

        // 网口扫码枪1重连
        private void bt_CH1TCP_Reconnect_Click(object sender, EventArgs e)
        {
            if(tb_CH1TCP_ip.Text.Trim().IsNullOrEmpty() || tb_CH1TCP_port.Text.Trim().IsNullOrEmpty())
                return;

            try
            {
                Form1.f1.saomaClient1?.Reconnect(tb_CH1TCP_ip.Text.Trim(), Convert.ToInt32(tb_CH1TCP_port.Text.Trim()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        // 网口扫码枪1断开连接
        private void bt_CH1TCP_Disconnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (Form1.f1.saomaClient1 != null && Form1.f1.saomaClient1.isRunning)
                {
                    Form1.f1.saomaClient1.Disconnect();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        // 网口扫码枪1保存
        private void bt_CH1TCPsave_Click(object sender, EventArgs e)
        {
            var jsconfig = new JsonConfig("config.json");
            jsconfig.SetValue("Saoma_IP1", tb_CH1TCP_ip.Text);
            jsconfig.SetValue("Saoma_Port1", tb_CH1TCP_port.Text);
        }

        // 网口扫码枪2重连
        private void bt_CH2TCP_Reconnect_Click(object sender, EventArgs e)
        {
            if (tb_CH2TCP_ip.Text.Trim().IsNullOrEmpty() || tb_CH2TCP_port.Text.Trim().IsNullOrEmpty())
                return;

            try
            {
                Form1.f1.saomaClient2?.Reconnect(tb_CH2TCP_ip.Text.Trim(), Convert.ToInt32(tb_CH2TCP_port.Text.Trim()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        // 网口扫码枪2断开连接
        private void bt_CH2TCP_Disconnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (Form1.f1.saomaClient2 != null && Form1.f1.saomaClient2.isRunning)
                {
                    Form1.f1.saomaClient2.Disconnect();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        // 网口扫码枪2保存
        private void bt_CH2TCPsave_Click(object sender, EventArgs e)
        {
            var jsconfig = new JsonConfig("config.json");
            jsconfig.SetValue("Saoma_IP2", tb_CH2TCP_ip.Text);
            jsconfig.SetValue("Saoma_Port2", tb_CH2TCP_port.Text);
        }

        // 连接状态 - 定时器
        private void timer_ConnectStatus_Tick(object sender, EventArgs e)
        {
            // 串口扫码枪1
            if (Form1.f1.SerialPort_CH1Saoma.IsOpen)
            {
                lb_CH1Com_status.Text = "已打开";
                lb_CH1Com_status.ForeColor = Color.Green;
                cb_CH1PortName.Enabled = false;
                cb_CH1BaudRate.Enabled = false;
                bt_CH1Connect.Enabled = false;
            }
            else
            {
                lb_CH1Com_status.Text = "未打开";
                lb_CH1Com_status.ForeColor = Color.Red;
                cb_CH1PortName.Enabled = true;
                cb_CH1BaudRate.Enabled = true;
                bt_CH1Connect.Enabled = true;
            }
            // 串口扫码枪2
            if (Form1.f1.SerialPort_CH2Saoma.IsOpen)
            {
                lb_CH2Com_status.Text = "已打开";
                lb_CH2Com_status.ForeColor = Color.Green;
                cb_CH2PortName.Enabled = false;
                cb_CH2BaudRate.Enabled = false;
                bt_CH2Connect.Enabled = false;
            }
            else
            {
                lb_CH2Com_status.Text = "未打开";
                lb_CH2Com_status.ForeColor = Color.Red;
                cb_CH2PortName.Enabled = true;
                cb_CH2BaudRate.Enabled = true;
                bt_CH2Connect.Enabled = true;
            }
            // 网口扫码枪1
            if (Form1.f1.saomaClient1 != null && Form1.f1.saomaClient1.isRunning)
            {
                lb_CH1TCP_ConnectStatus.Text = "OK";
                lb_CH1TCP_ConnectStatus.ForeColor = Color.Green;
            }
            else
            {
                lb_CH1TCP_ConnectStatus.Text = "NG";
                lb_CH1TCP_ConnectStatus.ForeColor = Color.Red;
            }
            // 网口扫码枪2
            if (Form1.f1.saomaClient2 != null && Form1.f1.saomaClient2.isRunning)
            {
                lb_CH2TCP_ConnectStatus.Text = "OK";
                lb_CH2TCP_ConnectStatus.ForeColor = Color.Green;
            }
            else
            {
                lb_CH2TCP_ConnectStatus.Text = "NG";
                lb_CH2TCP_ConnectStatus.ForeColor = Color.Red;
            }
        }
    }
}
