using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using arCtl;
using Project.StateMachine;
using COMM;
using AR;

namespace Project
{
    public partial class fMain
    {
        string lastbms03 = "";
        string lastbms04 = "";
        string lastbmsA03 = "";
        string lastbmsA04 = "";
        string lastbms = "";
        DateTime lastbmstime03 = DateTime.Now;
        DateTime lastbmstime04 = DateTime.Now;
        DateTime lastbmstimeA03 = DateTime.Now;
        DateTime lastbmstimeA04 = DateTime.Now;
        DateTime lastbmstime = DateTime.Now;
        private void Bms_Message(object sender, arDev.BMS.MessageEventArgs e)
        {

            if (e.MsgType == arDev.BMSSerialComm.MessageType.Error) PUB.logbms.AddE(e.Message);
            else
            {
                VAR.TIME[eVarTime.LastRecv_BAT] = DateTime.Now;

                var hexstr = e.Data.GetHexString().Trim();
                bool addlog = false;
                var logtimesec = 30;
                if (hexstr.StartsWith("DD 04"))
                {
                    if (lastbms04.Equals(hexstr.Substring(0, 5)) == false)
                    {
                        addlog = true;
                        lastbms04 = "DD 04";
                        lastbmstime04 = DateTime.Now;
                    }
                    else
                    {
                        var ts = DateTime.Now - lastbmstime04;
                        if (ts.TotalSeconds > logtimesec)
                        {
                            addlog = true;
                            lastbms04 = "DD 04";
                            lastbmstime04 = DateTime.Now;
                        }
                    }

                }
                else if (hexstr.StartsWith("DD 03"))
                {
                    if (lastbms03.Equals(hexstr.Substring(0, 5)) == false)
                    {
                        addlog = true;
                        lastbms03 = "DD 03";
                        lastbmstime03 = DateTime.Now;
                    }
                    else
                    {
                        var ts = DateTime.Now - lastbmstime03;
                        if (ts.TotalSeconds > logtimesec)
                        {
                            addlog = true;
                            lastbms03 = "DD 03";
                            lastbmstime03 = DateTime.Now;
                        }
                    }
                }

                else if (hexstr.StartsWith("DD A5 04"))
                {
                    if (lastbmsA04.Equals(hexstr.Substring(0, 8)) == false)
                    {
                        addlog = true;
                        lastbmsA04 = "DD A5 04";
                        lastbmstimeA04 = DateTime.Now;
                    }
                    else
                    {
                        var ts = DateTime.Now - lastbmstimeA04;
                        if (ts.TotalSeconds > logtimesec)
                        {
                            addlog = true;
                            lastbmsA04 = "DD A5 04";
                            lastbmstimeA04 = DateTime.Now;
                        }
                    }
                }

                else if (hexstr.StartsWith("DD A5 03"))
                {
                    if (lastbmsA03.Equals(hexstr.Substring(0, 8)) == false)
                    {
                        addlog = true;
                        lastbmsA03 = "DD A5 03";
                        lastbmstimeA03 = DateTime.Now;
                    }
                    else
                    {
                        var ts = DateTime.Now - lastbmstimeA03;
                        if (ts.TotalSeconds > logtimesec)
                        {
                            addlog = true;
                            lastbmsA03 = "DD A5 03";
                            lastbmstimeA03 = DateTime.Now;
                        }
                    }
                }



                else if (hexstr.isEmpty() == false)
                {
                    if (lastbms.Equals(hexstr) == false)
                    {
                        lastbms = hexstr;
                        lastbmstime = DateTime.Now;
                        addlog = true;
                    }
                    else
                    {
                        var ts = DateTime.Now - lastbmstime;
                        if (ts.TotalSeconds > logtimesec)
                        {
                            addlog = true;
                            lastbms = hexstr;
                            lastbmstime = DateTime.Now;
                        }
                    }
                }

                if (addlog)
                {
                    //if (e.MsgType == arDev.arRS232.MessageType.Recv)
                    //    PUB.logbms.Add("RX", e.Data.GetHexString());
                    //else if (e.MsgType == arDev.arRS232.MessageType.Send)
                    //    PUB.logbms.Add("TX", e.Data.GetHexString());
                    //else
                    {
                        PUB.logbms.Add(e.MsgType.ToString(),e.Message);
                    }
                        
                }



            }
        }
        private void BMS_ChargeDetect(object sender, arDev.ChargetDetectArgs e)
        {
            //자동충전중이아니고 멈춰있다면 수동 충전으로 전환한다
            VAR.TIME[eVarTime.LastRecv_BAT] = DateTime.Now;
            if (e.Detected == true)  //충전이 감지되었다.
            {
                if (VAR.BOOL[eVarBool.FLAG_AUTORUN] == false && VAR.BOOL[eVarBool.FLAG_CHARGEONM] == false)
                {
                    VAR.BOOL[eVarBool.FLAG_CHARGEONM] = true;
                    PUB.Speak(Lang.충전이감지되어수동충전으로전환합니다);
                    if (PUB.AGV.system1.agv_run == true) PUB.AGV.AGVMoveStop("수동충전감지");
                }
            }
            else PUB.logbms.AddI("Battery Charge Off");

        }

        private void Bms_BMSDataReceive(object sender, arDev.BMSInformationEventArgs e)
        {
            VAR.TIME[eVarTime.LastRecv_BAT] = DateTime.Now;

            PUB.UpdateAGVBattery(e.Data.rsoc);

            if (e.Data.rsoc <= PUB.setting.ChargeStartLevel)
            {
                //배터리 레벨이 기준보다 낮다면 경고를 활성화 한다
                if (VAR.BOOL[eVarBool.BATTERY_LOW] == false)
                {
                    VAR.BOOL[eVarBool.BATTERY_LOW] = true;
                    PUB.log.AddAT("배터리 부족 경고 활성화");
                }
            }
            else
            {
                //배터리 경고를 해제한다
                if (VAR.BOOL[eVarBool.BATTERY_LOW] == true)
                {
                    VAR.BOOL[eVarBool.BATTERY_LOW] = false;
                    PUB.log.AddAT("배터리 부족 경고 비활성화");
                }
            }
            EEMStatus.MakeBMSInformation_INFO();
        }
        private void BMS_BMSCellDataReceive(object sender, arDev.BMSCelvoltageEventArgs e)
        {
            VAR.TIME[eVarTime.LastRecv_BAT] = DateTime.Now;
            EEMStatus.MakeBMSInformation_Cell();
        }
    }
}
