using System.Text;
using UHFHelper;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //判断串口是否打开
            if (!UHFCardHelper.GetInstance().ComOpen)
            {
                Resultoutput resultoutput = UHFCardHelper.GetInstance().OpenPort();
                if (resultoutput.Res)
                {
                    MessageBox.Show(resultoutput.ResMes);
                }
                else
                {
                    MessageBox.Show(resultoutput.ResMes);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!UHFCardHelper.GetInstance().ComOpen)
            {
                MessageBox.Show("串口未打开");
                return;
            }
            string res = UHFCardHelper.GetInstance().ReadCard();
            label1.Text = res;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            UHFCardHelper.GetInstance().ClosePort();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var res = UHFCardHelper.GetInstance().WriteCard(textBox1.Text);
            MessageBox.Show(res.ResMes);
        }
        List<int> add = new List<int>();
        private void Form1_Load(object sender, EventArgs e)
        {

            //// 查看本机可用串口
            //foreach (var val in UHF_RS485_Helper.GetLocalSerialPortNames())
            //{
            //    this.comboBox1.Items.Add(val);
            //}
            add.Add(01);
            add.Add(02);
        }
        UHF_RS485_Helper uHF_RS485_Helper;
        private void button5_Click(object sender, EventArgs e)
        {
            // 初始化
            UHF_RS485_Helper.GetInstance().Open(new SerialParam
            {
                PortName = "COM8",
                BaudRate = 57600,
                DataBits = 8
            });
            if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
            {
                MessageBox.Show("打开失败");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (UHF_RS485_Helper.GetInstance().GetSerialPortState())
            {
                var oldss = UHF_RS485_Helper.GetInstance().ReadCard(01);
                var sss = oldss?.ResData;
                textBox4.Text = sss;
                label2.Text = sss;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            UHFCardHelper.GetInstance().WorkMode();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            System.Timers.Timer ti = new();
            ti.Interval = 500;
            ti.Elapsed += Ti_Elapsed;
            ti.Start();
        }

        private void Ti_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var item in add)
            {
                var oldss = UHF_RS485_Helper.GetInstance().ReadCard(item);
                if (oldss != null)
                {
                    //lvi.SubItems.Add(oldss.ResData ?? "无");
                    //this.listView1.Items.Add(lvi);
                    this.Invoke(() =>
                    {
                        if (listView1.Items.Count == 0)
                        {
                            ListViewItem lvi = new ListViewItem();
                            lvi.SubItems.Add(oldss.ResData ?? "无");
                            lvi.SubItems.Add("1");
                            this.listView1.Items.Add(lvi);
                        }
                        bool isCunzai = false;

                        for (int i = 0; i < listView1.Items.Count; i++)     //判断是否在Listview列表内
                        {
                            if (oldss.ResData == listView1.Items[i].SubItems[1].Text)
                            {
                                var aListItem = listView1.Items[i];
                                aListItem.SubItems[2].Text = Convert.ToString(Convert.ToInt32(aListItem.SubItems[2].Text) + 1);
                                isCunzai = true;
                            }
                        }
                        if (!isCunzai)
                        {
                            ListViewItem lvi = new ListViewItem();
                            lvi.SubItems.Add(oldss.ResData ?? "无");
                            lvi.SubItems.Add("1");
                            this.listView1.Items.Add(lvi);
                        }
                    });
                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = new ListViewItem("1");
            lvi.SubItems.Add("第1列,第1行");
            lvi.SubItems.Add("第2列,第2行");

        }

        private void button10_Click(object sender, EventArgs e)
        {
            HKControl.Main.GetInstance.Init();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button11_Click(object sender, EventArgs e)
        {
            int num = Convert.ToInt32(textBox2.Text.Trim());
            HKControl.Main.GetInstance.Start(num);
        }
    }
}