using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_BMS
{
    public partial class Form1 : Form
    {
        arDev.BMS bms;
        public Form1()
        {
            InitializeComponent();
            bms = new arDev.BMS();
            bms.BMSDataReceive += Bms_BMSDataReceive;
            bms.BMSCellDataReceive += Bms_BMSCellDataReceive;
            bms.Message += Bms_Message;
            this.Text = $"{Application.ProductName} ver{Application.ProductVersion}";
        }

        private void Bms_BMSCellDataReceive(object sender, arDev.BMSCelvoltageEventArgs e)
        {
            var valuestr = e.ToString();// string.Join(" ", e.voltage.Select(t => t.ToString()));
            addmsg2($"{valuestr}");
        }

        private void Bms_BMSDataReceive(object sender, arDev.BMSInformationEventArgs e)
        {
            //최종 데이터가 수신되는 경우
            addmsg(e.Data.ToString());
        }

        private void Bms_Message(object sender, arDev.BMSSerialComm.MessageEventArgs e)
        {
            var sb = new System.Text.StringBuilder();
            if (e.Data != null)
            {
                foreach (var b in e.Data)
                    sb.Append(" " + b.ToString("X2"));
            }
            else sb.Append(e.Message);

            if (e.MsgType == arDev.BMSSerialComm.MessageType.Error)
                addmsg(e.Message);
            else if(e.MsgType == arDev.BMSSerialComm.MessageType.Send)
            {
                addmsg($"Tx:{sb}");
            }
            else if(e.MsgType == arDev.BMSSerialComm.MessageType.Recv)
            {
                addmsg($"Rx:{sb}");
            }
            else
                addmsg($"{e.MsgType}:{sb}");
        }
        void addmsg(string m)
        {
            if (logTextBox1.InvokeRequired)
            {
                logTextBox1.BeginInvoke(new Action(() => {
                    logTextBox1.AddMsg(m);
                }));
            }
            else
            {
                logTextBox1.AddMsg(m);
            }
        }

        void addmsg2(string m)
        {
            if (logTextBox2.InvokeRequired)
            {
                logTextBox2.BeginInvoke(new Action(() => {
                    logTextBox2.AddMsg(m);
                }));
            }
            else
            {
                logTextBox2.AddMsg(m);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.Items.Clear();
            foreach (var port in System.IO.Ports.SerialPort.GetPortNames())
                this.comboBox1.Items.Add(port);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bms.BaudRate = int.Parse(this.comboBox2.Text);
            bms.PortName = comboBox1.Text;
            bms.Open();
            addmsg($"open:{bms.IsOpen}");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bms.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bms.SendQuery_ReadStatue();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            comboBox1.Text = "COM41";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            bms.SendQuery_ReadCellvoltage();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            bms.SendQuery();
        }
    }
}
