using Jdas_Mbic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jdas_Mbic
{
    class Data_Handler
    {
        //FileDownload fileDownload;
        Update_Serial fileDownload;
        const byte Terminal_Controller_Update_Ack = 0x11;
        const byte Terminal_Controller_Update_Nak = 0x22;
        const byte Terminal_Controller_FirmwareUpdateLen = 250;
        const byte Terminal_Reset = 0x0A;
        const byte Terminal_BootStart = 0x0B;


        public Boolean HeaderCheck(byte[] data)
        {
            Boolean ret = false;

            if(data[0] == 0xbe)
            {
                ret = true;
            }
            return ret;
        }
        public bool CrcCheck(byte[] data)
        {
            bool ret = false;
            Crc16 crc = new Crc16();
            byte length,crcindex;
            try
            {
                length = data[2];
            }
            catch
            {
                return ret;
            }
            crcindex = (byte)(length + 1);
            try
            {
            ret = crc.STH30_CheckCrc(data,length,data[crcindex]);
            }
            catch(Exception e)
            {
                return ret;
            }
            return ret;
        }
        JdasMbic main_Form; //= new Main_Form();
        Serial serial;
        public void Recv_dataCheck(object serial,object main_form, object fileDownload, byte[] data)
        {
            Boolean DataCheck;
            Bluecell_BootProtocol bluecell_BootProtocol = new Bluecell_BootProtocol();
            this.serial = (Serial)serial;
            this.main_Form = (JdasMbic)main_form;
            byte seq;
            try
            {
                seq = data[1];
            }
            catch {
                return;
            }
            if (fileDownload != null)
            {
                this.fileDownload = (Update_Serial)fileDownload;
            }
            else
            {
                this.fileDownload = new Update_Serial();
            }
            if (this.fileDownload.Update_ready == true)
            {
                DataCheck = HeaderCheck(data);
                if (DataCheck == false)
                {
                    return;
                }
                DataCheck = CrcCheck(data);
                if(DataCheck == false)
                {
                    return;
                }


                switch (seq)
                {
                    case Terminal_Reset:
                    case Terminal_BootStart:
                    case Terminal_Controller_Update_Ack:
                    case Terminal_Controller_Update_Nak:
                        //int DataAckcnt = data[(int)Bluepro_t.bluecell_type + 1];
                        //this.fileDownload.UpdateFileSend(data, DataAckcnt);
                        this.fileDownload.Termianl__Operate(data);
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
                
            }
            else/* * * * * * * * * * * * * *API RECV 부분 * * * * * * * * * * * * *  */
            {
                DataCheck = HeaderCheck(data);
                if (DataCheck == false)
                {
                    return;
                }
                DataCheck = CrcCheck(data);
                if (DataCheck == false)
                {
                    return;
                }
                //bluecell_BootProtocol.Bluecell_RF_Status_Get(this.serial,this.main_Form, data);
            }
        }
    }
}
