/**
 * @file hv_mw_OsdComm.c
 * @brief osd common c file.
 * @details This file provides the following functions: \n
 *          (1) Osd common functions \n
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-09-01
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author       <th>Date            <th>Change Description
 * <tr><td>shanpeng     <td>2022-09-01      <td>create
 * </table>
 */

#include "hv_res_OsdMoeRes.h"
#include "hv_mw_OsdComm.h"
#include "hv_mw_OsdLog.h"
#include "hv_mw_PanelManager.h"

#define DEFAULT_NAME_LEN    (32)
static CHAR8 s_acDefaultName[DEFAULT_NAME_LEN];

#ifdef OSD_DEBUG_PRINT_NAME
extern const CHAR8* g_apcItemNameTbl[];
extern const CHAR8* g_apcSceneNameTbl[];
#endif

static const CHAR8* s_apcCpNameTbl[] = {
    [OSD_CP_TYPE_NONE] =            "CP_NONE",
    [OSD_CP_TYPE_TEXT] =            "Text",
    [OSD_CP_TYPE_TEXT_LIST] =       "TextList",
    [OSD_CP_TYPE_NUMBER] =          "Number",
    [OSD_CP_TYPE_INTACT_NUMBER] =   "IntactNumber",
    [OSD_CP_TYPE_ASCII_STRING] =    "AsciiString",
    [OSD_CP_TYPE_ICON] =            "Icon",
    [OSD_CP_TYPE_ICON_LIST] =       "IconList",
    [OSD_CP_TYPE_BMP] =             "Bmp",
    [OSD_CP_TYPE_BMP_LIST] =        "BmpList",
#if (HV_PROJECT_CONFIG_OSD_CUSTOM_GROUP_SUPPORT == HV_CONFIG_ON)
    [OSD_CP_TYPE_CUSTOM_GROUP] =    "CustomGroup",
#endif
    [OSD_CP_TYPE_RECT] =            "Rect",
    [OSD_CP_TYPE_RECTGRID] =        "RectGrid",
    [OSD_CP_TYPE_LINEGRID] =        "LineGrid",
    [OSD_CP_TYPE_LINE] =            "Line",
    [OSD_CP_TYPE_GRAPHIC] =         "Graphic",
};

const CHAR8 *Hv_Mw_OsdGetComponentName(OsdComponent stComponent)
{
    if (stComponent.enType < OSD_CP_TYPE_MAX)
    {
        HV_SNPRINTF(s_acDefaultName, DEFAULT_NAME_LEN, "%s_%d", s_apcCpNameTbl[stComponent.enType], stComponent.usIndex);
    }
    else
    {
        HV_SNPRINTF(s_acDefaultName, DEFAULT_NAME_LEN, "CP_NONE");
    }
    return s_acDefaultName;
}

const CHAR8 *Hv_Mw_OsdGetItemName(USHORT16 usItemId)
{
#ifdef OSD_DEBUG_PRINT_NAME
    return usItemId < ITEM_ID_MAX ? g_apcItemNameTbl[usItemId] : g_apcItemNameTbl[ITEM_ID_MAX];
#else
    if (usItemId < ITEM_ID_MAX)
    {
        HV_SNPRINTF(s_acDefaultName, DEFAULT_NAME_LEN, "ITEM_%d", usItemId);
    }
    else
    {
        HV_SNPRINTF(s_acDefaultName, DEFAULT_NAME_LEN, "ITEM_NONE");
    }
    return s_acDefaultName;
#endif
}

const CHAR8 *Hv_Mw_OsdGetSceneName(UCHAR8 ucSceneId)
{
#ifdef OSD_DEBUG_PRINT_NAME
    return ucSceneId < SCENE_ID_MAX ? g_apcSceneNameTbl[ucSceneId] : g_apcSceneNameTbl[SCENE_ID_MAX];
#else
    if (ucSceneId < SCENE_ID_MAX)
    {
        HV_SNPRINTF(s_acDefaultName, DEFAULT_NAME_LEN, "SCENE_%d", ucSceneId);
    }
    else
    {
        HV_SNPRINTF(s_acDefaultName, DEFAULT_NAME_LEN, "SCENE_NONE");
    }
    return s_acDefaultName;
#endif
}

UINT32 Hv_Mw_OsdGetComponentPropSize(OsdComponentType enType)
{
    HV_ASSERT_TRUE_RET(enType < OSD_CP_TYPE_MAX, 0);
    static const UINT32 s_auiCpSize[OSD_CP_TYPE_MAX] = {
        [OSD_CP_TYPE_NONE] =            0,
        [OSD_CP_TYPE_TEXT] =            sizeof(OsdText),
        [OSD_CP_TYPE_TEXT_LIST] =       sizeof(OsdTextList),
        [OSD_CP_TYPE_NUMBER] =          sizeof(OsdNumber),
        [OSD_CP_TYPE_INTACT_NUMBER] =   0, // TODO
        [OSD_CP_TYPE_ASCII_STRING] =    sizeof(OsdAsciiString),
        [OSD_CP_TYPE_ICON] =            sizeof(OsdIcon),
        [OSD_CP_TYPE_ICON_LIST] =       sizeof(OsdIconList),
        [OSD_CP_TYPE_BMP] =             sizeof(OsdBmp),
        [OSD_CP_TYPE_BMP_LIST] =        sizeof(OsdBmpList),
#if (HV_PROJECT_CONFIG_OSD_CUSTOM_GROUP_SUPPORT == HV_CONFIG_ON)
        [OSD_CP_TYPE_CUSTOM_GROUP] =    sizeof(OsdCustomGroup),
#endif
        [OSD_CP_TYPE_RECT] =            sizeof(OsdRect),
        [OSD_CP_TYPE_RECTGRID] =        sizeof(OsdRectGrid),
        [OSD_CP_TYPE_LINEGRID] =        sizeof(OsdLineGrid),
        [OSD_CP_TYPE_LINE] =            sizeof(OsdLine),
        [OSD_CP_TYPE_GRAPHIC] =         sizeof(OsdGraphic),
    };

    return s_auiCpSize[enType];
}

UINT32 Hv_Mw_OsdGetComponentPropAddr(OsdComponent stCp)
{
    HV_ASSERT_TRUE_RET(stCp.enType < OSD_CP_TYPE_MAX, 0);

    static const UINT32 s_auiCpAddr[OSD_CP_TYPE_MAX] = {
        [OSD_CP_TYPE_NONE] =            (UINT32)NULL,
        [OSD_CP_TYPE_TEXT] =            (UINT32)g_astTextPropArray,
        [OSD_CP_TYPE_TEXT_LIST] =       (UINT32)g_astTextListPropArray,
        [OSD_CP_TYPE_NUMBER] =          (UINT32)g_astNumberPropArray,
        [OSD_CP_TYPE_INTACT_NUMBER] =   (UINT32)NULL, // TODO
        [OSD_CP_TYPE_ASCII_STRING] =    (UINT32)g_astAsciiStringPropArray,
        [OSD_CP_TYPE_ICON] =            (UINT32)g_astIconPropArray,
        [OSD_CP_TYPE_ICON_LIST] =       (UINT32)g_astIconListPropArray,
        [OSD_CP_TYPE_BMP] =             (UINT32)g_astBmpPropArray,
        [OSD_CP_TYPE_BMP_LIST] =        (UINT32)g_astBmpListPropArray,
#if (HV_PROJECT_CONFIG_OSD_CUSTOM_GROUP_SUPPORT == HV_CONFIG_ON)
        [OSD_CP_TYPE_CUSTOM_GROUP] =    (UINT32)g_astCustomGroupPropArray,
#endif
        [OSD_CP_TYPE_RECT] =            (UINT32)g_astRectPropArray,
        [OSD_CP_TYPE_RECTGRID] =        (UINT32)g_astRectGridPropArray,
        [OSD_CP_TYPE_LINEGRID] =        (UINT32)g_astLineGridPropArray,
        [OSD_CP_TYPE_LINE] =            (UINT32)g_astLinePropArray,
        [OSD_CP_TYPE_GRAPHIC] =         (UINT32)g_astGraphicPropArray,
    };

    UINT32 uiCpSize = Hv_Mw_OsdGetComponentPropSize(stCp.enType);

    return s_auiCpAddr[stCp.enType] + (uiCpSize * stCp.usIndex);
}

UCHAR8 Hv_Mw_OsdGetDynamicStateColor(UCHAR8 ucColorChoose, UCHAR8 ucState)
{
    if (ucColorChoose >= OSD_DYN_STATE_COLOR_NUM)
    {
        return 0;
    }

    switch (ucState)
    {
        case OSD_ITEM_NORMAL:
        {
            return astOsdDynamicStateColor[ucColorChoose].ucNormalColor;
        }
        case OSD_ITEM_FOCUS:
        {
            return astOsdDynamicStateColor[ucColorChoose].ucFocusColor;
        }
        case OSD_ITEM_STAY:
        {
            return astOsdDynamicStateColor[ucColorChoose].ucStayColor;
        }
        case OSD_ITEM_SELECT:
        {
            return astOsdDynamicStateColor[ucColorChoose].ucSelectColor;
        }
        case OSD_ITEM_SELECT_FOCUS:
        {
            return astOsdDynamicStateColor[ucColorChoose].ucSelectFocusColor;
        }
        case OSD_ITEM_DISABLED:
        {
            return astOsdDynamicStateColor[ucColorChoose].ucDisableColor;
        }
        default:
        {
            return 0;
        }
    }
}

/* Function Define Begin */
const OsdComponent* Hv_Mw_OsdGetCursorStyle(UCHAR8 ucCursorStyleId, OsdItemState enState)
{
    if (ucCursorStyleId >= CURSOR_STYLE_NUM)
    {
        return NULL;
    }

    switch (enState)
    {
        case OSD_ITEM_NORMAL:
        {
            return g_astCursorStyleList[ucCursorStyleId].pstNormal;
        }
        case OSD_ITEM_FOCUS:
        {
            return g_astCursorStyleList[ucCursorStyleId].pstFocus;
        }
        case OSD_ITEM_STAY:
        {
            return g_astCursorStyleList[ucCursorStyleId].pstStay;
        }
        case OSD_ITEM_SELECT:
        {
            return g_astCursorStyleList[ucCursorStyleId].pstSelect;
        }
        case OSD_ITEM_SELECT_FOCUS:
        {
            return g_astCursorStyleList[ucCursorStyleId].pstSelectFocus;
        }
        case OSD_ITEM_DISABLED:
        {
            return g_astCursorStyleList[ucCursorStyleId].pstDisable;
        }
        default:
        {
            return NULL;
        }
    }
}

const OsdComponent* Hv_Mw_OsdGetItemStyle(USHORT16 usItemId, OsdItemState enState)
{
    if (usItemId >= ITEM_ID_MAX)
    {
        return NULL;
    }

    switch (enState)
    {
        case OSD_ITEM_NORMAL:
        {
            return g_astItemStyleList[usItemId].pstNormal;
        }
        case OSD_ITEM_FOCUS:
        {
            return g_astItemStyleList[usItemId].pstFocus;
        }
        case OSD_ITEM_STAY:
        {
            return g_astItemStyleList[usItemId].pstStay;
        }
        case OSD_ITEM_SELECT:
        {
            return g_astItemStyleList[usItemId].pstSelect;
        }
        case OSD_ITEM_SELECT_FOCUS:
        {
            return g_astItemStyleList[usItemId].pstSelectFocus;
        }
        case OSD_ITEM_DISABLED:
        {
            return g_astItemStyleList[usItemId].pstDisable;
        }
        default:
        {
            return NULL;
        }
    }
}

const OsdRectStyle* Hv_Mw_OsdGetRectStyle(UCHAR8 ucStyleIndex)
{
    HV_ASSERT_TRUE_RET(ucStyleIndex < COMPONET_RECT_STYLE_NUM_MAX, NULL);

    return &g_astRectStyleArray[ucStyleIndex];
}

const OsdRectHighlight* Hv_Mw_OsdGetRectHl(UCHAR8 ucHlOptionIndex)
{
    HV_ASSERT_TRUE_RET(ucHlOptionIndex < COMPONET_RECT_HIGHLIGHT_NUM_MAX, NULL);

    return &g_astRectHighlightArray[ucHlOptionIndex];
}

const OsdLineHighlight* Hv_Mw_OsdGetLineHl(UCHAR8 ucHlOptionIndex)
{
    HV_ASSERT_TRUE_RET(ucHlOptionIndex < COMPONET_LINE_HIGHLIGHT_NUM_MAX, NULL);

    return &g_astLineHighlightArray[ucHlOptionIndex];
}

USHORT16 Hv_Mw_OsdGetPanelHActive(VOID)
{
    return Hv_Mw_Panel_GetPanelHactive();
}

USHORT16 Hv_Mw_OsdGetPanelVActive(VOID)
{
    return Hv_Mw_Panel_GetPanelVactive();
}

/* Function Define End */
