/**
 * @file hv_MW_Factory.c
 * @brief Factory module driver
 * @details This module provide interfaces for Hiview-Madia-Ware.
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 20230504
 * @copyright Copyright(c),2023-5, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                         <th>Date             <th>Details
 * <tr><td>HiView SoC Software Team   <td>20230504       <td>Create and first Version.
 * </table>
 **/

#include <string.h>
#include "hv_comm_Timer.h"
#include "hv_mw_InputPortManager.h"
#include "hv_mw_Factory.h"
#include "hv_mw_PQ.h"
#include "hv_mw_PanelManager.h"

#include "hv_drv_Audio.h"
#include "hv_drv_PLL.h"
#include "hv_drv_Dma.h"
#include "hv_drv_Uart.h"
#include "hv_drv_Ktx.h"
#include "hv_drv_Mprt.h"
#include "hv_drv_PQ.h"
#include "hv_drv_Gpio.h"
#include "hv_drv_Mailbox.h"
#include "hv_cal_Dma.h"

#include "hv_app_PQ.h"
static USHORT16 s_usTickCount = 0;
static USHORT16 s_usTimeMin = 0;

UCHAR8 Hv_Mw_Factory_GetContrast(VOID)
{
    return HV_COMMON_DATABASE_GET(UserData, ucContrast);
}

/* 有参接口 */
UCHAR8 Hv_Mw_Factory_GetSN(UCHAR8 *pucString)
{
    strncpy(pucString, Hv_Common_Db_GetFactoryDataDBRegionPtr()->acProductSn, PRODUCT_SN_LENTH);
    return strlen(pucString);
}

VOID Hv_Mw_Factory_SetEdidWp(BOOL bEn)
{
    Hv_Drv_Gpio_SetPinLevel(HV_BOARD_CONFIG_GPIO_E2_WP, bEn);
    HV_COMMON_DATABASE_SET(SystemData, bEdidWp, bEn);
    return;
}

BOOL Hv_Mw_Factory_GetEdidWp(VOID)
{
    BOOL bWpEn = Hv_Drv_Gpio_GetPinLevel(HV_BOARD_CONFIG_GPIO_E2_WP);
    HV_COMMON_DATABASE_SET(SystemData, bEdidWp, bWpEn);
    return bWpEn;
}

VOID Hv_Mw_Factory_GetEdidInfo(LinkPortIndex enPortIndex, HvFactoryEdidInfo *pstFactEdidDetail)
{
    HV_ASSERT_SUCCESS_VOID(Hv_Mw_InputPort_GetFactoryEdidInfo(enPortIndex, pstFactEdidDetail));
    return;
}

VOID Hv_Mw_Factory_GetEdidInfoToOsd(LinkPortIndex enPortIndex, HvEdidInfoToOsd *pstFactEdidInfoToOsd)
{
    HV_ASSERT_SUCCESS_VOID(Hv_Mw_InputPort_GetFactoryEdidInfoToOsd(enPortIndex, pstFactEdidInfoToOsd));
    return;
}

VOID Hv_Mw_Factory_SetEnableUart(BOOL bEnble)
{
    Hv_Drv_EnableUart(bEnble);
    HV_COMMON_DATABASE_SET(FactoryData, bUartEnable, bEnble);
    return;
}

BOOL Hv_Mw_Factory_GetEnableUart(VOID)
{
    return HV_COMMON_DATABASE_GET(FactoryData, bUartEnable);
}


VOID Hv_Mw_Factory_SetFactoryDefaultBacklight(UCHAR8 ucVal)
{
    //TODO:将数值映射到0-100的占空比
    USHORT16 usDrvValue = 0;
    usDrvValue  = Hv_App_PQ_GetRealValue(ucVal, PQ_BRIGHTNESS);
    UINT32 uiLow = 0; 
    UINT32 uiHigh = 0; 
    uiHigh = usDrvValue;
    uiLow = HV_PROJECT_CONFIG_MPRT_HIGHT_FREQ_PWM_TOTAL - uiHigh;

    Hv_Drv_MPRT_SetOutHighFreqPwmDuty(HV_PROJECT_CONFIG_MPRT_HIGHT_PORT, uiLow, uiHigh);//low-high
    
    HV_COMMON_DATABASE_SET(FactoryData, ucFactoryBrightness, ucVal);
    return;
}

VOID Hv_Mw_Factory_SetFactoryEnergy(UCHAR8 ucVal)
{
    //TODO:将数值映射到0-100的占空比
    USHORT16 usDrvValue = 0;
    usDrvValue  = Hv_App_PQ_GetRealValue(ucVal, PQ_BRIGHTNESS);
    UINT32 uiLow = 0;
    UINT32 uiHigh = 0;
    uiHigh = usDrvValue;
    uiLow = HV_PROJECT_CONFIG_MPRT_HIGHT_FREQ_PWM_TOTAL - uiHigh;

    Hv_Drv_MPRT_SetOutHighFreqPwmDuty(HV_PROJECT_CONFIG_MPRT_HIGHT_PORT, uiLow, uiHigh);//low-high 
    HV_COMMON_DATABASE_SET(FactoryData, ucFactoryEnergy, ucVal);
    return;
}

VOID Hv_Mw_Factory_WriteProductSN(const CHAR8 *pcTargetProductSN)
{
    FactoryDataDBRegion *pstFactoryData = Hv_Common_Db_GetFactoryDataDBRegionPtr();
    HV_MEMSET(pstFactoryData->acProductSn, 0, PRODUCT_SN_LENTH);                       // 将acProductSn全部置为0
    strncpy(pstFactoryData->acProductSn, pcTargetProductSN, PRODUCT_SN_LENTH - 1);     // 将pcTargetProductSN拷贝到acProductSn中
    pstFactoryData->acProductSn[PRODUCT_SN_LENTH - 1] = '\0';                          // 确保acProductSn以0作为结束符
    Hv_Mw_DataBase_Sync();
    return;
}

VOID Hv_Mw_Factory_SetPanelSscEable(BOOL bEn)
{
#ifdef  CONFIG_USER_PANEL_TYPE_VBOTX
    Hv_Drv_Sscg_Enalbe(SSCG_VBO, bEn);
#endif
#ifdef  CONFIG_USER_PANEL_TYPE_EDPTX
    Hv_Drv_Sscg_Enalbe(SSCG_EDP, bEn);
#endif
    return;
}

VOID Hv_Mw_Factory_SetPanelSscFreq(UCHAR8 ucFreq)
{
#ifdef  CONFIG_USER_PANEL_TYPE_VBOTX
    Hv_Drv_Sscg_SetSscg(SSCG_VBO, Hv_Mw_Factory_GetPanelSscSetp(), ucFreq);
#endif
#ifdef  CONFIG_USER_PANEL_TYPE_EDPTX
    Hv_Drv_Sscg_SetSscg(SSCG_EDP, Hv_Mw_Factory_GetPanelSscSetp(), ucFreq);
#endif
    HV_COMMON_DATABASE_SET(FactoryData, ucFactoryPanelSscFreq, ucFreq);
    return;
}

UCHAR8 Hv_Mw_Factory_GetPanelSscFreq(VOID)
{
    return HV_COMMON_DATABASE_GET(FactoryData, ucFactoryPanelSscFreq);
}

VOID Hv_Mw_Factory_SetPanelSscStep(UCHAR8 ucStep)
{
#ifdef  CONFIG_USER_PANEL_TYPE_VBOTX
    Hv_Drv_Sscg_SetSscg(SSCG_VBO, ucStep, Hv_Mw_Factory_GetPanelSscFreq());
#endif
#ifdef  CONFIG_USER_PANEL_TYPE_EDPTX
    Hv_Drv_Sscg_SetSscg(SSCG_EDP, ucStep, Hv_Mw_Factory_GetPanelSscFreq());
#endif
    HV_COMMON_DATABASE_SET(FactoryData, ucFactoryPanelSsSetp, ucStep);
    return;
}

UCHAR8 Hv_Mw_Factory_GetPanelSscSetp(VOID)
{
    return HV_COMMON_DATABASE_GET(FactoryData, ucFactoryPanelSsSetp);
}

VOID Hv_Mw_Factory_SetDdrSscEable(BOOL bEn)
{
    Hv_Drv_Sscg_Enalbe(SSCG_DDR, bEn);
    return;
}

VOID Hv_Mw_Factory_SetDdrSscFreq(UCHAR8 ucFreq)
{
    Hv_Drv_Sscg_SetSscg(SSCG_DDR, Hv_Mw_Factory_GetDdrSscSetp(), ucFreq);
    HV_COMMON_DATABASE_SET(FactoryData, ucFactoryDdrSscFreq, ucFreq);
    return;
}

UCHAR8 Hv_Mw_Factory_GetDdrSscFreq(VOID)
{
    return HV_COMMON_DATABASE_GET(FactoryData, ucFactoryDdrSscFreq);
}

VOID Hv_Mw_Factory_SetDdrSscStep(UCHAR8 ucStep)
{
    Hv_Drv_Sscg_SetSscg(SSCG_DDR, ucStep, Hv_Mw_Factory_GetDdrSscFreq());
    HV_COMMON_DATABASE_SET(FactoryData, ucFactoryDdrSsSetp, ucStep);
    return;
}

UCHAR8 Hv_Mw_Factory_GetDdrSscSetp(VOID)
{
    return HV_COMMON_DATABASE_GET(FactoryData, ucFactoryDdrSsSetp);
}

VOID Hv_Mw_Factory_SetColorTemperature(ColorTemperatureType ucColorTempType, ColorTemperature stColTemp)
{
    HV_LOG_INFO(DEFAULT, "FAC Colr temp %d %d %d", stColTemp.ucColorTempRGain, stColTemp.ucColorTempGGain, stColTemp.ucColorTempBGain);
    Hv_App_PQ_SetRGBGainFac(stColTemp.ucColorTempRGain, stColTemp.ucColorTempGGain, stColTemp.ucColorTempBGain);
    
    switch (ucColorTempType)
    {
        case FACTORY_COLOR_TEMPER_STANDARD:
        {
            HV_COMMON_DATABASE_SET(FactoryData, stColTempStandard, stColTemp);
            break;
        }
        case FACTORY_COLOR_TEMPER_WARM:
        {
            HV_COMMON_DATABASE_SET(FactoryData, stColTempWarm, stColTemp);
            break;
        }
        case FACTORY_COLOR_TEMPER_COOL:
        {
            HV_COMMON_DATABASE_SET(FactoryData, stColTempCool, stColTemp);
            break;
        }
        case FACTORY_COLOR_TEMPER_CUSTOM:
        {
            HV_COMMON_DATABASE_SET(FactoryData, stColTempCustom, stColTemp);
            break;
        }
        case FACTORY_COLOR_TEMPER_EYESHIELD:
        {
            HV_COMMON_DATABASE_SET(FactoryData, stColTempEyeCare, stColTemp);
            break;
        }
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_COLORtEMP_TEMP)
        case FACTORY_COLOR_TEMPER_SRGB:
        {
            HV_COMMON_DATABASE_SET(FactoryData, stColTempSrgb, stColTemp);
            break;
        }
        case FACTORY_COLOR_TEMPER_BLUISH:
        {
            HV_COMMON_DATABASE_SET(FactoryData, stColTempBlush, stColTemp);
            break;
        }
#endif
        default:
        {
            break;
        }
    }
    return;
}

ColorTemperature Hv_Mw_Factory_GetColorTemperature(ColorTemperatureType ucColorTempType)
{
    ColorTemperature stColTemp = {0};
    switch (ucColorTempType)
    {
        case FACTORY_COLOR_TEMPER_STANDARD:
        {
            stColTemp = HV_COMMON_DATABASE_GET(FactoryData, stColTempStandard);
            break;
        }
        case FACTORY_COLOR_TEMPER_WARM:
        {
            stColTemp = HV_COMMON_DATABASE_GET(FactoryData, stColTempWarm);
            break;
        }
        case FACTORY_COLOR_TEMPER_COOL:
        {
            stColTemp = HV_COMMON_DATABASE_GET(FactoryData, stColTempCool);
            break;
        }
        case FACTORY_COLOR_TEMPER_CUSTOM:
        {
            stColTemp = HV_COMMON_DATABASE_GET(FactoryData, stColTempCustom);
            break;
        }
        case FACTORY_COLOR_TEMPER_EYESHIELD:
        {
            stColTemp = HV_COMMON_DATABASE_GET(FactoryData, stColTempEyeCare);
            break;
        }
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_COLORtEMP_TEMP)
        case FACTORY_COLOR_TEMPER_SRGB:
        {
            stColTemp = HV_COMMON_DATABASE_GET(FactoryData, stColTempSrgb);
            break;
        }
        case FACTORY_COLOR_TEMPER_BLUISH:
        {
            stColTemp = HV_COMMON_DATABASE_GET(FactoryData, stColTempBlush);
            break;
        }
#endif
        default:
        {
            break;
        }
    }
    return stColTemp;
}

/* 工检 */
VOID Hv_Mw_Factory_ShowPanelPattern(UCHAR8 ucKtxPattSwitch, UCHAR8 ucPatternMode)
{
    Hv_Drv_Ktx_SetKtxPatt(ucKtxPattSwitch, ucPatternMode);
    return;
}

/* 无参接口 */
VOID Hv_Mw_Factory_ResetPanelOnCountTime(VOID)
{
    HV_COMMON_DATABASE_SET(TimeRec, usPanelOnTimeHour, 0);
    HV_COMMON_DATABASE_SET(TimeRec, usPanelOnTimeMin, 0);
    /*立即保存database数据!!!*/
    Hv_Mw_DataBase_Sync();
    s_usTickCount = 0;
    s_usTimeMin = 0;
    return;
}

VOID Hv_Mw_Factory_FactoryReset(VOID)
{
    Hv_Mw_DataBase_ResumeSystemData();
    Hv_Mw_DataBase_ResumeUserData();
    return;
}

VOID Hv_Mw_Factory_SetFactoryModeEnable(BOOL bEn)
{
    HV_COMMON_DATABASE_SET(SystemData, bFactoryMode, bEn);
    return;
}

UCHAR8 Hv_Mw_Factory_GetFactoryModeEnable()
{
    return HV_COMMON_DATABASE_GET(SystemData, bFactoryMode);
}



VOID Hv_Mw_Factory_SetBurnInEnalbe(BOOL bEn)
{
    HV_COMMON_DATABASE_SET(SystemData, bBurnInEnable, bEn);
    return;
}

VOID Hv_Mw_Factory_SavePanelOnCountTime(BOOL bSaveTimeImme)
{
    USHORT16 usPanelOnTimeHour = 0;
    USHORT16 usPanelOnTimeMin = 0;

    s_usTickCount = s_usTickCount + 1;

    if (s_usTickCount * DATABASE_PERIOD_SYNC_GLOBAL_TO_FLASH_TIMER_LENGTH >= 60 * 1000)
    {
        s_usTimeMin++;
        s_usTickCount = 0;
    }

    if ((HV_PROJECT_CONFIG_FACTORY_PANEL_TIME_COUNT_PERIOD > 60) || (HV_PROJECT_CONFIG_FACTORY_PANEL_TIME_COUNT_PERIOD < 10))
    {
        HV_LOG_ERROR(PANEL, "HV_PROJECT_CONFIG_FACTORY_PANEL_TIME_COUNT_PERIOD valid range is 10-60, please review check the setting!");
        return;
    }

    /*每HV_PROJECT_CONFIG_FACTORY_PANEL_TIME_COUNT_PERIOD分钟保存一次时间数据, typc 充电时关屏，不计时*/
    const PanelManager* pstPanelManager = Hv_Mw_Panel_GetManagerPtr();
    if (((s_usTimeMin >= HV_PROJECT_CONFIG_FACTORY_PANEL_TIME_COUNT_PERIOD) || (HV_TRUE == bSaveTimeImme))
    && (PANEL_STATE_BLK_ON == pstPanelManager->enCurrentPanelState))
    {
        usPanelOnTimeMin = HV_COMMON_DATABASE_GET(TimeRec, usPanelOnTimeMin);
        usPanelOnTimeHour = HV_COMMON_DATABASE_GET(TimeRec, usPanelOnTimeHour);

        if ((usPanelOnTimeMin + s_usTimeMin) >= 60)
        {
            if (usPanelOnTimeHour < 65535)
            {
                usPanelOnTimeHour++;
            }
            usPanelOnTimeMin = (usPanelOnTimeMin + s_usTimeMin) % 60;
            if (usPanelOnTimeHour <= 65535)
            {
                HV_COMMON_DATABASE_SET(TimeRec, usPanelOnTimeHour, usPanelOnTimeHour);
            }
            HV_COMMON_DATABASE_SET(TimeRec, usPanelOnTimeMin, usPanelOnTimeMin);
        }
        else
        {
            usPanelOnTimeMin = usPanelOnTimeMin + s_usTimeMin;
            HV_COMMON_DATABASE_SET(TimeRec, usPanelOnTimeMin, usPanelOnTimeMin);
        }
        s_usTimeMin = 0;
    }

    return;
}

VOID Hv_Mw_Factory_GetPanelOnTime(USHORT16 *usPanelOnTimeHour, USHORT16 *usPanelOnTimeMin)
{
    *usPanelOnTimeHour = HV_COMMON_DATABASE_GET(TimeRec, usPanelOnTimeHour);
    *usPanelOnTimeMin = HV_COMMON_DATABASE_GET(TimeRec, usPanelOnTimeMin);

    if ((*usPanelOnTimeMin + s_usTimeMin) >= 60)
    {
        *usPanelOnTimeHour = *usPanelOnTimeHour + 1;
        *usPanelOnTimeMin = (*usPanelOnTimeMin + s_usTimeMin) % 60;
    }
    else
    {
        *usPanelOnTimeMin = *usPanelOnTimeMin + s_usTimeMin;
    }
    return;
}

VOID Hv_Mw_FactoryModeToPm(BOOL bEn)
{
    MailboxMsg stMailboxMsg = { 0 };
    HV_LOG_DEBUG(SYSTEM, "Set Fac mode = %d", bEn);
    stMailboxMsg.stMsgHeader.u8MsgType = bEn ? E_MAILBOX_CMD_RISC_TO_PM_FACTORY_OFF : E_MAILBOX_CMD_RISC_TO_PM_FACTORY_ON;
    Hv_Drv_MailboxSendMsg(&stMailboxMsg);

    return;
}

VOID Hv_Mw_Factory_SetDynamicPanelEn(BOOL bEn)
{
    HV_COMMON_DATABASE_SET(FactoryData, bDynamicPanelEn, bEn);
    return;
}

VOID Hv_Mw_Factory_SetDynamicPanelIndex(UCHAR8 ucIndex)
{
    HV_COMMON_DATABASE_SET(FactoryData, ucDynamicPanelIndex, ucIndex);
    return;
}

VOID Hv_Mw_Factory_GetDynamicPanelInfo(BOOL *pbDynamicPanelEn, UCHAR8 *pucDynamicPanelIndex, UCHAR8 *pucPanelNum)
{
    HV_ASSERT_VALID_PTR_VOID(pbDynamicPanelEn);
    HV_ASSERT_VALID_PTR_VOID(pucDynamicPanelIndex);
    *pbDynamicPanelEn = HV_COMMON_DATABASE_GET(FactoryData, bDynamicPanelEn);
    *pucDynamicPanelIndex = HV_COMMON_DATABASE_GET(FactoryData, ucDynamicPanelIndex);
    *pucPanelNum = HV_COMMON_DATABASE_GET(PanelData, ucPanelNumber);
    return;
}

VOID Hv_Mw_Factory_Init(VOID)
{

    Hv_Mw_Factory_SavePanelOnCountTime(HV_FALSE);


    return;
}
