#include "DisplaySch_user.h"
#include "Common_Interface.h"
#include "DispSch.h"
#include "Popup_Scheduler.h"
#include "Popup_List.h"
#include "Key.h"
#include "CAN_Lib.h"
#include "GUI.h"
#include "Services_ODO_User.h" /*清空总计里程函数*/
#include "Menu.h"
#include "jcua.h"
#include "kwp2000_service.h"

uint32_t EnterTimer[10];
uint8_t UE_EN[Display_MAX];
#pragma alignvar(8)
#pragma ghs section bss = default
uint8_t Mem[32];
#pragma ghs section bss = default

#pragma ghs section bss = ".myNonInitArea"
UE_CtrlInfo_Struct_t UE_CtrlInfo;
uint8_t BG_display_flag;
#pragma ghs section bss = default

uint16_t HMI_StartAnimationFrameCnt;
uint8_t backgroundOFFFlag;

static void switch_branch(void);

uint8_t Get_Pwr(void)
{
    return Common_Get_IG_Sts();
}
uint32_t Get_IG_ON(void)
{
    return Common_GetIgnOnTime();
}
uint32_t Get_IG_Off(void)
{
    return Common_GetIgnOffTime();
}

static const DispSch_Attr_st loc[] =
{
    {&UE_EN[Display_StartAnimation], UE_IGN_ON , Display_StartAnimation, ((void *)0), Display_StartAnimationEnter, Display_StartAnimationService, Display_StartAnimationExit},
    {&UE_EN[Display_Menu]          , UE_IGN_ON , Display_Menu          , ((void *)0), Display_MenuEnter          , Display_MenuService          , ((void *)0)},
    {&UE_EN[Display_Alarm]         , UE_IGN_ON , Display_Alarm         , ((void *)0), Display_AlarmEnter         , Display_AlarmService         , ((void *)0)},
    {&UE_EN[Display_Nothing]       , UE_IGN_OFF, Display_Nothing       , ((void *)0), Display_NothingEnter       , Display_NothingService       , ((void *)0)},
};

void DispSch_TotalInit(void)
{
    uint8_t i;
    DispSch_Ext_st m_st;
    m_st.Get_Power = Get_Pwr;
    m_st.Get_IG_ON_Timer = Get_IG_ON;
    m_st.Get_IG_OFF_Timer = Get_IG_Off;
    m_st.Get_IG_OFF_Timer = Get_IG_Off;
    m_st.TotalSize = Display_MAX;
    DispSch_Init(Mem, loc, &m_st, 0u);

    for (i = 0u; i < Display_MAX; i++)
    {
        UE_EN[i] = UE_Disable;
    }

    if (BG_display_flag == 0u)
    {
        BG_display_flag = 1u;
        MenuFlag.Menu_Popup_Flag = 0u ;

        MenuInit();
    }
}

void DisplayServices(void)
{
    uint32_t PowerOnTimer;
    uint32_t PowerOFFTimer;
    uint8_t PowerStatus;
    uint32_t Get_Menu_Off_Time = 0u;
    uint16_t popupID = 0xFFFFu;

    PowerStatus = Common_Get_IG_Sts();
    PowerOnTimer = Common_GetIgnOnTime();
    PowerOFFTimer = Common_GetIgnOffTime();
    Get_Menu_Off_Time = Coom_Get_Menu_Off_Time();

    if ((PowerOnTimer <= StartAnimationEnd) && (PowerOnTimer >= StartAnimationStart))
    {
        UE_EN[Display_StartAnimation] = UE_Enable;
        UE_EN[Display_Menu] = UE_Disable;
        UE_EN[Display_Alarm] = UE_Disable;
        UE_EN[Display_Nothing] = UE_Disable;
    }
    else
    {
        /*获取四种模式*/
        Meter_Mode_conversion();

        if ((K_Line_Set.K_Line_LID51 == 0x01u) && (MenuFlag.Menu_Test != 1u))
        {
            popupID = Popup_Get_Current_Message();
        }

        if ((popupID != 0xffffu) && (MenuFlag.Key_Menu_Popup_time == 0x00u))
        {
            UE_EN[Display_Alarm] = UE_Enable;

            if (PowerStatus == COMMON_POWER_ON)
            {
                ; /*不处理*/
            }
            else
            {
                UE_EN[Display_Nothing] = UE_Disable;
            }
        }
        else
        {
            if (MenuFlag.Key_Menu_Popup_time != 0x00u)
            {
                MenuFlag.Key_Menu_Popup_time--;
            }
            else
            {
                ; /*Do Nothing!*/
            }

            UE_EN[Display_Alarm] = UE_Disable;

            if (PowerStatus == COMMON_POWER_ON)
            {
                UE_EN[Display_Menu] = UE_Enable;
            }
            else
            {
                if ((State_Flag.Menu_Key_Static == 1u) && (Get_Menu_Off_Time < 7500))
                {
                    UE_EN[Display_Menu] = UE_Enable;
                    UE_EN[Display_Nothing] = UE_Disable;
                }
                else
                {
                    UE_EN[Display_Menu] = UE_Disable;
                    UE_EN[Display_Nothing] = UE_Enable;
                }
            }
        }
    }

    DispSch_xxxMs_Logic(10u);

    /*上短*/
    if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_UP == KEY_EVENT_SHORT_PRESS)
    {
        HMI_Clear_KEY_UP();
    }
    /*上长*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_UP == KEY_EVENT_LONG_PRESS)
    {
        HMI_Clear_KEY_UP();
    }
    /*下短*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_DOWN == KEY_EVENT_SHORT_PRESS)
    {
        HMI_Clear_KEY_DOWN();
    }
    /*下长*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_DOWN == KEY_EVENT_LONG_PRESS)
    {
        HMI_Clear_KEY_DOWN();
    }
    /*确认短*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_CONFIRM == KEY_EVENT_SHORT_PRESS)
    {
        HMI_Clear_KEY_CONFIRM();
    }
    /*确认长*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_CONFIRM == KEY_EVENT_LONG_PRESS)
    {
        HMI_Clear_KEY_CONFIRM();
    }
    /*确认超长 5 S*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_CONFIRM == KEY_EVENT_SUPER_LONG_PRESS)
    {
        HMI_Clear_KEY_CONFIRM();

        /*TYW总计清零*/
        TYW_Data_ODO_Clear();
    }
    /*返回短*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_CANCEL == KEY_EVENT_SHORT_PRESS)
    {
        HMI_Clear_KEY_CANCEL();
    }
    /*返回长*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_CANCEL == KEY_EVENT_LONG_PRESS)
    {
        HMI_Clear_KEY_CANCEL();
    }
    /*MENU短*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_MENU == KEY_EVENT_SHORT_PRESS)
    {
        HMI_Clear_KEY_MENU();
    }
    /*MENU长*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_MENU == KEY_EVENT_LONG_PRESS)
    {
        HMI_Clear_KEY_MENU();
    }
    else
    {
        ; /*do nothing*/
    }

    /*ODOTrip短*/
    if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_ODOTRIP == KEY_EVENT_SHORT_PRESS)
    {
        HMI_Clear_KEY_ODOTRIP();
    }
    /*ODOTrip长*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_ODOTRIP == KEY_EVENT_LONG_PRESS)
    {
        HMI_Clear_KEY_ODOTRIP();
    }
    /*ODO off-to-on*/
    else if (UE_CtrlInfo.HMI_KEYInfo.u8KEY_ODOTRIP == KEY_EVENT_OFF_TO_ON)
    {
        HMI_Clear_KEY_ODOTRIP();
    }
    else
    {
        ; /*do nothing*/
    }
}

/*----开机动画----*/
void Display_StartAnimationEnter(void)
{
    HMI_StartAnimationFrameCnt = 0u;
    backgroundOFFFlag = 0;
    MenuFlag.Menu_Popup_Flag = 0u ;

    /*硬清空所有缓存数据*/
    loc_free_mem(MENU_Sprite, MENU_Sprite);

    /*使能底图窗口*/
    JCUA_Enable_window();
}
void Display_StartAnimationService(void)
{
    uint16_t m16_FrameCnt;

    HMI_StartAnimationFrameCnt++;
    m16_FrameCnt = HMI_StartAnimationFrameCnt;

    if ((m16_FrameCnt % 4) == 0u)
    {
        GUI_Display_Animation_Frame(m16_FrameCnt / 4u);
    }
}
void Display_StartAnimationExit(void)
{
}

uint16_t Common_GetHMI_StartAnimationFrameCnt(void)
{
    return HMI_StartAnimationFrameCnt;
}

/*----菜单----*/
void Display_MenuEnter (void)
{
    /*开机动画播放完毕使能底图窗口 二次使能窗口*/
    JCUA_Enable_window();

    /*使能PNG窗口*/
    loc_Enable_Window_App();

    /*刷底图*/
    GUI_Display_background();
}
void Display_MenuService(void)
{
}
/*----报警----*/
void Display_AlarmEnter (void)
{
}
void Display_AlarmService(void)
{
}

/*----Nothing----*/
void Display_NothingEnter(void)
{
    JCUA_Disable_window();
    loc_Disable_Window_App( );
    loc_free_mem(WET_Sprite, MENU_Sprite);
}
void Display_NothingService(void)
{

}

void GUI_DisplayServices(void)
{
    uint16_t DispVSpeed = 0u;
    uint16_t DispESpeed = 0u;
    uint32_t PowerStatus = 0u;
    UElib_uint16_t HMI_MENU_Sta = 0u;

    HMI_MENU_Sta = Get_Cur_HMI();
    PowerStatus = Common_Get_IG_Sts();
    DispVSpeed = Common_Get_Disp_V_Speed();

    if (HMI_MENU_Sta == Display_Alarm)
    {
        loc_Del_Sprite(MENU_Sprite, MENU_Sprite);

        if (Common_Get_window_Sta() != 1u)
        {
            JCUA_Enable_window();
        }

        if (MenuFlag.Menu_Popup_Flag == 0u)
        {
            MenuFlag.Menu_Popup_Flag = 1u;

            if (bCurMenuIDX < _MN_DSP_Menu_OFF)
            {
                MenuFlag.Meun_Test_Dealer = bCurMenuIDX ;
            }
            else
            {
                MenuFlag.Meun_Test_Dealer = 0u;
            }

            NextMenu(_MN_DSP_LEVEPOPUP);
            bCurMenuIDX = _MN_DSP_LEVEPOPUP;
        }
    }
    else if ((HMI_MENU_Sta == Display_Menu) || (HMI_MENU_Sta == Display_Nothing))
    {
        if (PowerStatus == COMMON_POWER_ON)
        {
            if (MenuFlag.Menu_Factory == 1u)
            {
                MenuFlag.Menu_Factory = 0u;
                MenuFlag.Menu_Popup_Flag = 2u;

                NextMenu( _FAC_DSP_LEVEL0MENU_1 );
                bCurMenuIDX = _FAC_DSP_LEVEL0MENU_1;
            }
            else if (MenuFlag.Menu_Dealer == 1u)
            {
                MenuFlag.Menu_Dealer = 0u;
                MenuFlag.Menu_Popup_Flag = 3u;

                NextMenu( _DEA_DSP_LEVEL0MENU_1 );
                bCurMenuIDX = _DEA_DSP_LEVEL0MENU_1;
            }
            else if (MenuFlag.Menu_Test == 1u)
            {
                MenuFlag.Menu_Test = 0;
                MenuFlag.Menu_Popup_Flag = 4u;

                NextMenu( _MN_DSP_Menu_Test );
                bCurMenuIDX = _MN_DSP_Menu_Test;
            }
            else  if (MenuFlag.Menu_Popup_Flag == 1u)
            {
                MenuFlag.Menu_Popup_Flag = 0u;

                if (DispVSpeed > DISPSPEEDNULLMAX)
                {
                    NextMenu(MenuFlag.Menu_Flag);
                    bCurMenuIDX = MenuFlag.Menu_Flag;
                }
                else
                {
                    NextMenu(MenuFlag.Meun_Test_Dealer);
                    bCurMenuIDX = MenuFlag.Meun_Test_Dealer;
                }
            }

            if (((bCurMenuIDX > _MN_DSP_LEVEPOPUP) && (bCurMenuIDX < _MN_DSP_LEVEL3_5_MENU_1)) ||
                    (bCurMenuIDX >= _FAC_DSP_LEVEL0MENU_1))
            {
                loc_Del_Sprite(ADAS_Sprite, Gear_Sprite);
                loc_Del_Sprite(MENU_Sprite, MENU_Sprite);
                if (Common_Get_window_Sta() != 0u)
                {
                    JCUA_Disable_window();
                }
            }
            else
            {
                loc_Del_Sprite(MENU_Sprite, MENU_Sprite);
                if (Common_Get_window_Sta() != 1u)
                {
                    JCUA_Enable_window();
                }
            }
        }
        else
        {
            ;/*Do Nothing!*/
            //BG_display_flag = 0u;
            //NextMenu(_MN_DSP_Menu_OFF);
        }
    }
    if (HMI_MENU_Sta != Display_StartAnimation)
    {
        if (PowerStatus == COMMON_POWER_ON)
        {
            MenuManage();
        }
        else
        {
            if (backgroundOFFFlag == 0u)
            {
                loc_Enable_Window_App();
                GUI_Display_backgroundOFF();

                loc_Del_Sprite(MENU_Sprite, MENU_Sprite);
                backgroundOFFFlag = 1u;
            }

            GUI_Time_OilTemp_OFF_Display(GUI_DISP_MODE_NORMAL);
            BG_display_flag = 0u;
        }
    }
    else
    {
        ; /*Do  Nothing!*/
    }
}

void HMI_Set_KEY_UP(uint8_t u8KeyVal)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_UP = u8KeyVal;
}
void HMI_Set_KEY_DOWN(uint8_t u8KeyVal)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_DOWN = u8KeyVal;
}
void HMI_Set_KEY_CONFIRM(uint8_t u8KeyVal)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_CONFIRM = u8KeyVal;
}
void HMI_Set_KEY_CANCEL(uint8_t u8KeyVal)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_CANCEL = u8KeyVal;
}
void HMI_Set_KEY_MENU(uint8_t u8KeyVal)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_MENU = u8KeyVal;
}
void HMI_Set_KEY_ODOTRIP(uint8_t u8KeyVal)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_ODOTRIP = u8KeyVal;
}

void HMI_Clear_KEY_UP(void)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_UP = 0u;
}
void HMI_Clear_KEY_DOWN(void)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_DOWN = 0u;
}
void HMI_Clear_KEY_CONFIRM(void)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_CONFIRM = 0u;
}
void HMI_Clear_KEY_CANCEL(void)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_CANCEL = 0u;
}
void HMI_Clear_KEY_MENU(void)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_MENU = 0u;
}
void HMI_Clear_KEY_ODOTRIP(void)
{
    UE_CtrlInfo.HMI_KEYInfo.u8KEY_ODOTRIP = 0u;
}

void Meter_Mode_conversion(void)
{
    uint16_t popupID = 0xFFFFu;
    uint16_t DispVSpeed = 0u;
    uint16_t DispESpeed = 0u;

    DispVSpeed = Common_Get_Disp_V_Speed();
    DispESpeed = Common_Get_Act_E_Speed();

    /*工厂模式*/
    if (MenuFlag.Menu_Popup_Flag == 2u)
    {
        if (K_Line_Set.K_Line_LID51 == 0x01u)
        {
            popupID = Popup_Get_Current_Message();
            if ((popupID != 0xffffu) && (MenuFlag.Key_Menu_Popup_time == 0x00u))
            {
                MenuFlag.Menu_Popup_Flag = 0u;
            }
            else
            {
                MenuFlag.Menu_Popup_Flag = 1u;
            }
        }
        else if (K_Line_Set.K_Line_LID51 == 0x02u)
        {
            MenuFlag.Menu_Dealer = 1u;
        }
        else
        {
            ;/*Do Nothing!*/
        }
    }
    /*经销商模式*/
    else if (MenuFlag.Menu_Popup_Flag == 3u)
    {
        if (K_Line_Set.K_Line_LID51 == 0x01u)
        {
            popupID = Popup_Get_Current_Message();
            if ((popupID != 0xffffu) && (MenuFlag.Key_Menu_Popup_time == 0x00u))
            {
                MenuFlag.Menu_Popup_Flag = 0u;
            }
            else
            {
                MenuFlag.Menu_Popup_Flag = 1u;
            }
        }
        else if (K_Line_Set.K_Line_LID51 == 0x00u)
        {
            MenuFlag.Menu_Factory = 1u;
        }
        else
        {
            ;/*Do Nothing!*/
        }
    }
    /*自检模式*/
    else if (MenuFlag.Menu_Popup_Flag == 4u)
    {
        if ((DispVSpeed > DISPSPEEDNULLMAX) || (DispESpeed >= 330u))
        {
            popupID = Popup_Get_Current_Message();
            if ((popupID != 0xffffu) && (MenuFlag.Key_Menu_Popup_time == 0x00u))
            {
                MenuFlag.Menu_Popup_Flag = 0u;
            }
            else
            {
                MenuFlag.Menu_Popup_Flag = 1u;
            }
        }
        else if (K_Line_Set.K_Line_LID51 == 0x02u)
        {
            MenuFlag.Menu_Dealer = 1u;
        }
        else
        {
            ;/*Do Nothing!*/
        }
    }
    /*通常模式*/
    else
    {
        /*进入自检模式*/
        if ((MenuFlag.Key_MenuTest_Long == 1u) && (MenuFlag.Key_MenuTest_ShortNum >= 3u))
            //if (MenuFlag.Key_MenuTest_ShortNum >= 3u)
        {
            MenuFlag.Menu_Test = 1u;
            MenuFlag.Key_MenuTest_Long = 0u;
            MenuFlag.Key_MenuTest_ShortNum = 0u;
        }
        else if (K_Line_Set.K_Line_LID51 == 0x00u)
        {
            MenuFlag.Menu_Factory = 1u;
        }
        else if (K_Line_Set.K_Line_LID51 == 0x02u)
        {
            MenuFlag.Menu_Dealer = 1u;
        }
        else
        {
            ;/*Do Nothing!*/
        }
    }
}