#include "GUI_Display\GUI_Display.h"
#include "SEG_DISPLAY\SEG_DISPLAY.h"
#include "Application.h"

uint16_t poweroff_time = 0;

uint8_t SOC_Count_Time = 0;

// void Gauge_Gear_Display(void)
// {
//     // uint8_t MBMS_StatBattWorkState = Get_CAN_CH0_ID_18203220_Sig_MBMS_StatBattWorkState();
//     if (Common_Get_IG_Sts() == COMMON_POWER_ON)
//     {
//         SEG_SET_GEAR(1,);
//     }
//     else
//     {
        
//     }
// }

void Gauge_Power_SOC_pull_Display(void)
{
    // uint8_t MBMS_StatBattWorkState = Get_CAN_CH0_ID_18203220_Sig_MBMS_StatBattWorkState();
    if (Common_Get_IG_Sts() == COMMON_POWER_ON)
    {
        if (CAN_MSG_Status(&CAN_CH0_CanMsgOp, Co_Can_ConvertSubID_CAN_CH0(CAN_CH0_ID_CAN_0x18200A20_Msg)) == CAN_ERR_OK || CAN_MSG_Status(&CAN_CH0_CanMsgOp, Co_Can_ConvertSubID_CAN_CH0(CAN_CH0_ID_CAN_0x18200A21_Msg)) == CAN_ERR_OK)
        {
            uint8_t SOC_1_Count = Get_CAN_CH0_ID_18200A20_Sig_MBMS_Soc();
            uint8_t SOC_2_Count = Get_CAN_CH0_ID_18200A21_Sig_SBMS_Soc();
            SEG_SET_EleDial_1SOC(1, SOC_1_Count);
            SEG_SET_EleDial_2SOC(1, SOC_2_Count);
        }
        else
        {
            SEG_SET_EleDial_1SOC(0, 0);
            SEG_SET_EleDial_2SOC(0, 0);
        }
    }
    else
    {
        SEG_SET_EleDial_1SOC(0, 0);
        SEG_SET_EleDial_2SOC(0, 0);
    }
}

void Gauge_Power_SOC_CHAGING_Display(void)
{
    uint8_t MBMS_TOTALSigAcc = Get_CAN_CH0_ID_18202922_Sig_MBMS_TOTALSigAcc();
    uint8_t MBMS_TOTALSigCharge = Get_CAN_CH0_ID_18202922_Sig_MBMS_TOTALSigCharge();
    uint8_t MBMS_StatBattWorkState = Get_CAN_CH0_ID_18203220_Sig_MBMS_StatBattWorkState();
    static uint8_t SOC1_Count = 0;
    static uint8_t SOC2_Count = 0;
    // uint8_t MBMS_StatBattWorkState = Get_CAN_CH0_ID_18203220_Sig_MBMS_StatBattWorkState();
    if (Common_Get_IG_Sts() == COMMON_POWER_ON)
    {
        if (CAN_MSG_Status(&CAN_CH0_CanMsgOp, Co_Can_ConvertSubID_CAN_CH0(CAN_CH0_ID_CAN_0x18200A20_Msg)) == CAN_ERR_OK || CAN_MSG_Status(&CAN_CH0_CanMsgOp, Co_Can_ConvertSubID_CAN_CH0(CAN_CH0_ID_CAN_0x18200A21_Msg)) == CAN_ERR_OK)
        {
            uint8_t SOC_1_Count = Get_CAN_CH0_ID_18200A20_Sig_MBMS_Soc();
            uint8_t SOC_2_Count = Get_CAN_CH0_ID_18200A21_Sig_SBMS_Soc();
            if ((MBMS_TOTALSigAcc == 0x1 && MBMS_TOTALSigCharge == 0x1) || (MBMS_TOTALSigAcc == 0x0 && MBMS_TOTALSigCharge == 0x1)) // 上电充电
            {
                if (MBMS_StatBattWorkState == 0x1) // 主电池
                {
                    if (++SOC_Count_Time > 10)
                    {
                        SOC_Count_Time = 0;
                        
                        SEG_SET_EleDial_1SOC(1, SOC1_Count);
                        SOC1_Count++;
                        if (SOC1_Count > SOC_1_Count)
                        {
                            SOC1_Count = 0;
                        }
                    }
                    SEG_SET_EleDial_2SOC(1, SOC_2_Count);
                }
                else if (MBMS_StatBattWorkState == 0x4) // 副电池
                {
                    if (++SOC_Count_Time > 10)
                    {
                        SOC_Count_Time = 0;
                        
                        SEG_SET_EleDial_2SOC(1, SOC2_Count);
                        SOC2_Count++;
                        if (SOC2_Count > SOC_2_Count)
                        {
                            SOC2_Count = 0;
                        }
                    }
                    SEG_SET_EleDial_1SOC(1,SOC_1_Count);
                }
                else if (MBMS_StatBattWorkState == 0x7) // 主副电池
                {
                    if (++SOC_Count_Time > 10)
                    {
                        SOC_Count_Time = 0;
                        
                        SEG_SET_EleDial_1SOC(1, SOC1_Count);
                        SEG_SET_EleDial_2SOC(1, SOC2_Count);
                        SOC1_Count++;
                        SOC2_Count++;
                        if (SOC_1_Count > SOC_2_Count)
                        {
                            if (SOC2_Count > SOC_2_Count)
                            {
                                SOC2_Count--;
                                if (SOC1_Count > SOC_1_Count)
                                {
                                    SOC1_Count = 0;
                                    SOC2_Count = 0;
                                }
                            }
                        }
                        else
                        {
                            if (SOC1_Count > SOC_1_Count)
                            {
                                SOC1_Count--;
                                if (SOC2_Count > SOC_2_Count)
                                {
                                    SOC1_Count = 0;
                                    SOC2_Count = 0;
                                }
                            }
                        }
                    }
                }
            }
            if (MBMS_TOTALSigAcc == 0x0 && MBMS_TOTALSigCharge == 0x1) // 下电充电
            {
                poweroff_time++;
            }
            else
            {
                poweroff_time = 0;
            }
        }
        else
        {
            SEG_SET_EleDial_1SOC(0, 0);
            SEG_SET_EleDial_2SOC(0, 0);
        }
    }
    else
    {
        SEG_SET_EleDial_1SOC(0, 0);
        SEG_SET_EleDial_2SOC(0, 0);
    }
}

void Gauge_EleDial_AllSOC_Display(void)
{
    uint8_t AllSOC_DIS = 0;
    if (Common_Get_IG_Sts() == COMMON_POWER_ON)
    {
        if (CAN_MSG_Status(&CAN_CH0_CanMsgOp, Co_Can_ConvertSubID_CAN_CH0(CAN_CH0_ID_CAN_0x18203020_Msg)) == CAN_ERR_OK)
        {
            AllSOC_DIS = Get_CAN_CH0_ID_18203020_Sig_MBMS_StatSoc();
            SEG_SET_EleDial_AllSOC(1, AllSOC_DIS);
        }
        else
        {
            SEG_SET_EleDial_AllSOC(0, 0);
        }
    }
    else
    {
        SEG_SET_EleDial_AllSOC(0, 0);
    }
}

void Gauge_Clock_Display(void)
{
    uint32_t Clock_all = 0;
    static uint8_t Clock_seconds = 0;
    static uint8_t Clock_hours = 0;
    if (Common_Get_IG_Sts() == COMMON_POWER_ON)
    {
        if (CAN_MSG_Status(&CAN_CH0_CanMsgOp, Co_Can_ConvertSubID_CAN_CH0(CAN_CH0_ID_CAN_0x339_Msg)) == CAN_ERR_OK)
        {
            Clock_all = Get_CAN_CH0_ID_339_Sig_MMCU_SingleWorkingHours();
            Clock_hours = Clock_all / 60;
            Clock_seconds = Clock_all % 60;
            SEG_SET_Clock(Clock_hours, Clock_seconds, FLASH_SYNC_1Hz, 1, 1);
        }
        else
        {
            SEG_SET_Clock(Clock_hours, Clock_seconds, 1, 1, 1);
        }
    }
    else
    {
        SEG_SET_Clock(0, 0, 0, 0, 0);
    }
}
static uint16_t EEPROM_Write_Time = 0;
void Data_CumulativeWorkHours_EEPROM_Write(void)
{
    uint32_t u32Data[1] = {0};
    uint32_t eeprom_back[1] = {0};
    if (CAN_MSG_Status(&CAN_CH0_CanMsgOp, Co_Can_ConvertSubID_CAN_CH0(CAN_CH0_ID_CAN_0x339_Msg)) == CAN_ERR_OK)
    {
        u32Data[0] = Get_CAN_CH0_ID_339_Sig_MMCU_AccumulatedWorkHours();
        eeprom_ReadRecord(EEPROM_BLOCK_00, (uint8_t *)eeprom_back, 4);
        if (EEPROM_Write_Time == 0)
        {
            if (u32Data[0] >= eeprom_back[0])
            {
                eeprom_WriteRecord(EEPROM_BLOCK_00, (uint8_t *)u32Data, 4);
            }
        }
        if (++EEPROM_Write_Time > 6000) // 一分钟存一次
        {
            EEPROM_Write_Time = 0;
        }
    }
    else
    {
        EEPROM_Write_Time = 0;
    }
}

void Gauge_Clock_All_Display(void)
{
    uint32_t u32Data[1] = {0};
    eeprom_ReadRecord(EEPROM_BLOCK_00, (uint8_t *)u32Data, 4);
    if (Common_Get_IG_Sts() == COMMON_POWER_ON)
    {
        SEG_SET_Clock_ALL(1, u32Data[0]);
    }
    else
    {
        SEG_SET_Clock_ALL(0, 0);
    }
}

void Gauge_Frame_Display(void)
{
    if (Common_Get_IG_Sts() == COMMON_POWER_ON)
    {
        SEG_SET_Frame(1);
    }
    else
    {
        SEG_SET_Frame(0);
    }
}

void Clear_Bu98(void)
{
    uint8_t i = 0;
    for (i = 0; i < BU98R10_DDRAM_SIZE; i++)
    {
        BU98R10Chip0DDRAM.Byte[i] = 0;
        BU98R10Chip1DDRAM.Byte[i] = 0;
    }
}

void Gauge_Service(void)
{
    uint8_t i = 0;
    Gauge_Frame_Display();
    if (0)
    {
        if (Common_Get_IG_Sts() == COMMON_POWER_ON)
        {
            Check_SEG_Display();
        }
        else
        {
            Clear_Bu98();
        }
    }
    else
    {
        if (Common_Get_IG_Sts() == COMMON_POWER_ON && ADC_Read_Signal(ADC_CH_KL30_VOLTAGE) <= 16000 && ADC_Read_Signal(ADC_CH_KL30_VOLTAGE) >= 9000)
        {
            if (Get_CAN_Power_State() != PKEY_ON)
            {
                if (Common_GetIgnOnTime() >= 1500)
                {
                    SOC_charge_seg_Flag = 0;
                    Checkself_Init();
                    if (Get_CAN_Power_State() == KEY_ACC)
                    {
                        interact_PWM_Low_Duty = 0;
                        Gauge_Clock_Display();
                        Gauge_Clock_All_Display();
                        Gauge_EleDial_AllSOC_Display();
                        SEG_SET_GEAR(1, 1);
                        SEG_SET_Cutterhead(1, 2);
                        Gauge_Frame_Display();
                        Gauge_Power_SOC_pull_Display();
                    }
                    else if (Get_CAN_Power_State() == READY)
                    {
                        interact_PWM_Low_Duty = 30;
                        Gauge_Clock_Display();
                        Gauge_Clock_All_Display();
                        Gauge_EleDial_AllSOC_Display();
                        SEG_SET_GEAR(1, 1);
                        SEG_SET_Cutterhead(1, 2);
                        Gauge_Frame_Display();
                        Gauge_Power_SOC_pull_Display();
                    }
                    else if (Get_CAN_Power_State() == CHRGING)
                    {
                        SOC_charge_seg_Flag = 1;
                        if (poweroff_time < 3600)
                        {
                            Gauge_Power_SOC_CHAGING_Display();
                            if (poweroff_time == 0)
                            {
                                Gauge_Clock_Display();
                                Gauge_Clock_All_Display();
                                Gauge_EleDial_AllSOC_Display();
                                SEG_SET_GEAR(1, 1);
                                SEG_SET_Cutterhead(1, 2);
                                Gauge_Frame_Display();
                            }
                        }
                        else
                        {
                           
                        }
                    }
                }
                else
                {
                    Checkself_SEG_Display();
                }
            }
            else
            {
                // Clear_Bu98();
                // for (i = 0; i < LampCh0_MAX; i++)
                // {
                //     LED_Driver_Channel_Set(LampChannel_0, i, LED_OFF);
                // }
                interact_PWM_Low_Duty = 50;
                // 蜂鸣器
            }
        }
        else
        {
            Clear_Bu98();
            for (i = 0; i < LampCh0_MAX; i++)
            {
                LED_Driver_Channel_Set(LampChannel_0, i, LED_OFF);
            }
        }
    }
}
