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;
using COMM;

namespace Project.ViewForm
{
    public partial class fBms : Form
    {
        public fBms()
        {
            InitializeComponent();


            this.FormClosed += FIO_FormClosed;
            PUB.BMS.BMSCellDataReceive += BMS_BMSCellDataReceive;
            PUB.BMS.BMSDataReceive += BMS_BMSDataReceive;
        }

        private void BMS_BMSDataReceive(object sender, arDev.BMSInformationEventArgs e)
        {
            if (this.IsDisposed == true || this.Visible == false) return;
            this.BeginInvoke(new Action(() =>
            {
                if (lbinfost.ForeColor == Color.FromArgb(32, 32, 32))
                    lbinfost.ForeColor = Color.Lime;
                else
                    lbinfost.ForeColor = Color.FromArgb(32, 32, 32);
            }));
        }

        private void BMS_BMSCellDataReceive(object sender, arDev.BMSCelvoltageEventArgs e)
        {
            if (this.IsDisposed == true || this.Visible == false) return;
            this.BeginInvoke(new Action(() =>
            {
                if (lbcvSt.ForeColor == Color.FromArgb(15, 15, 15))
                    lbcvSt.ForeColor = Color.Lime;
                else
                    lbcvSt.ForeColor = Color.FromArgb(15, 15, 15);
            }));
        }

        private void fFlag_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
        }

        private void FIO_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.timer1.Stop();
            PUB.BMS.BMSCellDataReceive -= BMS_BMSCellDataReceive;
            PUB.BMS.BMSDataReceive -= BMS_BMSDataReceive;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            var data = PUB.BMS.BMSInformation;
            var volt = PUB.BMS.BMSCellVoltage;
            if (PUB.BMS.IsOpen == false)
                this.lbCycle.Text = "통신이 열려있지 않습니다";
            else if (PUB.BMS.IsValid == false)
                this.lbCycle.Text = "데이터가 유효하지 않습니다";
            else
                this.lbCycle.Text = $"Average:{volt.Average:N2}v, Delta:{volt.Delta:N0}, Cycle({data.cycleCount})";
            this.arLabel1.Text = $"{data.rsoc}%";
            this.arLabel1.Sign = $"{data.packVoltage}v, {data.watt:N0}w";// PUB.BMS.Current_Volt.ToString() + "v";

            if (data.watt < 0) this.arLabel1.SignColor = Color.Tomato;
            else this.arLabel1.SignColor = Color.Lime;

                this.cv1.Text = volt.Voltage[0].ToString("N3") + "v";
            this.cv2.Text = volt.Voltage[1].ToString("N3") + "v";
            this.cv3.Text = volt.Voltage[2].ToString("N3") + "v";
            this.cv4.Text = volt.Voltage[3].ToString("N3") + "v";
            this.cv5.Text = volt.Voltage[4].ToString("N3") + "v";
            this.cv6.Text = volt.Voltage[5].ToString("N3") + "v";
            this.cv7.Text = volt.Voltage[6].ToString("N3") + "v";
            this.cv8.Text = volt.Voltage[7].ToString("N3") + "v";
            this.lbTemp1.Text = data.ntcTemp[0].ToString() + "℃";
            this.lbtemp2.Text = data.ntcTemp[1].ToString() + "℃";
            timer1.Start();
        }

        private void fAgv_VisibleChanged(object sender, EventArgs e)
        {
            this.timer1.Enabled = this.Visible;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PUB.BMS.SendQuery_ReadStatue();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PUB.BMS.SendQuery_ReadCellvoltage();
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel6_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel8_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
