using Sunny.UI;
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;

namespace SLC1_N
{
    public partial class Form_RootSet : Form
    {
        public Form_RootSet()
        {
            InitializeComponent();
        }

        private void Form_RootSet_Load(object sender, EventArgs e)
        {
            chk_ch1AutoConnect.Checked = Form1.f1.ch1AutoConnect;
            chk_ch2AutoConnect.Checked = Form1.f1.ch2AutoConnect;
            chk_ch3AutoConnect.Checked = Form1.f1.ch3AutoConnect;
            chk_ch4AutoConnect.Checked = Form1.f1.ch4AutoConnect;

            var jsconfig = new JsonConfig("config.json");

            // 日志使能
            chk_DebugEnabled.Checked = jsconfig.GetValue<bool>("IsDebugEnabled", false);
            chk_ErrorEnabled.Checked = jsconfig.GetValue<bool>("IsErrorEnabled", false);
            chk_InfoEnabled.Checked = jsconfig.GetValue<bool>("IsInfoEnabled", false);
            chk_WarningEnabled.Checked = jsconfig.GetValue<bool>("IsWarningEnabled", false);
            chk_XXXEnabled.Checked = jsconfig.GetValue<bool>("IsXXXEnabled", false);

            string saoma = jsconfig.GetValue<string>("CodeScanner", "SerialPort");
            if (saoma == "SerialPort")
            {
                rbt_SerialPort.Checked = true;
            }
            else if (saoma == "TCP")
            {
                rbt_TCP.Checked = true;
            }
        }

        // 仪器自动连接
        private void chk_ch1AutoConnect_CheckedChanged(object sender, EventArgs e)
        {
            var jsconfig = new JsonConfig("config.json");
            jsconfig.SetValue("ch1AutoConnect", chk_ch1AutoConnect.Checked);
            Form1.f1.ch1AutoConnect = chk_ch1AutoConnect.Checked;
        }
        // 仪器自动连接
        private void chk_ch2AutoConnect_CheckedChanged(object sender, EventArgs e)
        {
            var jsconfig = new JsonConfig("config.json");
            jsconfig.SetValue("ch2AutoConnect", chk_ch2AutoConnect.Checked);
            Form1.f1.ch2AutoConnect = chk_ch2AutoConnect.Checked;
        }
        // 仪器自动连接
        private void chk_ch3AutoConnect_CheckedChanged(object sender, EventArgs e)
        {
            var jsconfig = new JsonConfig("config.json");
            jsconfig.SetValue("ch3AutoConnect", chk_ch3AutoConnect.Checked);
            Form1.f1.ch3AutoConnect = chk_ch3AutoConnect.Checked;
        }
        // 仪器自动连接
        private void chk_ch4AutoConnect_CheckedChanged(object sender, EventArgs e)
        {
            var jsconfig = new JsonConfig("config.json");
            jsconfig.SetValue("ch4AutoConnect", chk_ch4AutoConnect.Checked);
            Form1.f1.ch4AutoConnect = chk_ch4AutoConnect.Checked;
        }

        // 改站号
        private void bt_CH1zhanhao_Click(object sender, EventArgs e)
        {
            if (tb_CH1zhanhao.Text.IsNullOrEmpty())
                return;
            int zhanhao = Convert.ToUInt16(tb_CH1zhanhao.Text);
            Form1.f1.LL28CH1client?.writeRegister(1076, zhanhao);
        }
        // 改站号
        private void bt_CH2zhanhao_Click(object sender, EventArgs e)
        {
            if (tb_CH2zhanhao.Text.IsNullOrEmpty())
                return;
            int zhanhao = Convert.ToUInt16(tb_CH2zhanhao.Text);
            Form1.f1.LL28CH2client?.writeRegister(1076, zhanhao);
        }
        // 改站号
        private void bt_CH3zhanhao_Click(object sender, EventArgs e)
        {
            if (tb_CH3zhanhao.Text.IsNullOrEmpty())
                return;
            int zhanhao = Convert.ToUInt16(tb_CH3zhanhao.Text);
            Form1.f1.LL28CH3client?.writeRegister(1076, zhanhao);
        }
        // 改站号
        private void bt_CH4zhanhao_Click(object sender, EventArgs e)
        {
            if (tb_CH4zhanhao.Text.IsNullOrEmpty())
                return;
            int zhanhao = Convert.ToUInt16(tb_CH4zhanhao.Text);
            Form1.f1.LL28CH4client?.writeRegister(1076, zhanhao);
        }

        // 日志模式保存
        private void bt_logmode_save_Click(object sender, EventArgs e)
        {
            var jsconfig = new JsonConfig("config.json");

            jsconfig.SetValue("IsDebugEnabled", chk_DebugEnabled.Checked);
            jsconfig.SetValue("IsErrorEnabled", chk_ErrorEnabled.Checked);
            jsconfig.SetValue("IsInfoEnabled", chk_InfoEnabled.Checked);
            jsconfig.SetValue("IsWarningEnabled", chk_WarningEnabled.Checked);
            jsconfig.SetValue("IsXXXEnabled", chk_XXXEnabled.Checked);

            mxlLog.Instance.IsDebugEnabled = chk_DebugEnabled.Checked;
            mxlLog.Instance.IsErrorEnabled = chk_ErrorEnabled.Checked;
            mxlLog.Instance.IsInfoEnabled = chk_InfoEnabled.Checked;
            mxlLog.Instance.IsWarningEnabled = chk_WarningEnabled.Checked;
            mxlLog.Instance.IsXXXEnabled = chk_XXXEnabled.Checked;
        }

        // 扫码枪选用
        private void bt_saomamode_Click(object sender, EventArgs e)
        {
            var jsconfig = new JsonConfig("config.json");
            if (rbt_SerialPort.Checked)
            {
                jsconfig.SetValue("CodeScanner", "SerialPort");
            }
            else if (rbt_TCP.Checked)
            {
                jsconfig.SetValue("CodeScanner", "TCP");
            }
        }

        // 清理日志
        private void bt_ClearLog_Click(object sender, EventArgs e)
        {
            if (tb_logday.Text.IsNullOrEmpty())
                return;

            bt_ClearLog.Enabled = false;
            mxlLog.Instance.ClearOldLogs(Convert.ToInt32(tb_logday.Text.Trim()));
            bt_ClearLog.Enabled = true;
        }

    }
}
