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();
            this.arLabel1.Text = PUB.BMS.Current_Level.ToString("N1") + "%";
            this.arLabel1.Sign = PUB.BMS.Current_Volt.ToString() + "v";
            this.cv1.Text = PUB.BMS.CellVoltage[0].ToString("N3") + "v";
            this.cv2.Text = PUB.BMS.CellVoltage[1].ToString("N3") + "v";
            this.cv3.Text = PUB.BMS.CellVoltage[2].ToString("N3") + "v";
            this.cv4.Text = PUB.BMS.CellVoltage[3].ToString("N3") + "v";
            this.cv5.Text = PUB.BMS.CellVoltage[4].ToString("N3") + "v";
            this.cv6.Text = PUB.BMS.CellVoltage[5].ToString("N3") + "v";
            this.cv7.Text = PUB.BMS.CellVoltage[6].ToString("N3") + "v";
            this.cv8.Text = PUB.BMS.CellVoltage[7].ToString("N3") + "v";
            this.lbTemp1.Text = PUB.BMS.Current_temp1.ToString() + "℃";
            this.lbtemp2.Text = PUB.BMS.Current_temp2.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)
        {

        }
    }
}
