/**
 * @file hv_app_OsdDynamicUpdate.c
 * @brief osd dynamic component update c file.
 * @details This file provides the following functions: \n
 *          (1) Osd dynamic component update 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_app_OsdDynamicUpdate.h"
#include "hv_mw_OsdDraw.h"
#include "hv_mw_OsdOperation.h"
#include "hv_mw_OsdLog.h"

VOID Hv_App_Cb_OsdUpdateTextByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdText *pstText = (OsdText *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstText);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);
}

VOID Hv_App_Cb_OsdUpdateTextCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdText *pstText = (OsdText *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstText);
}

VOID Hv_App_Cb_OsdUpdateTextListByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdTextList *pstTextList = (OsdTextList *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstTextList);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);

    pstTextList->ucStringCurID = MIN((USHORT16)pstUserdata->sOsdValue, pstTextList->ucStringNum - 1);
}

VOID Hv_App_Cb_OsdUpdateTextListCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdTextList *pstTextList = (OsdTextList *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstTextList);

    switch (usId)
    {
        case TEXTLIST_FREESYNC_SCENE_MAIN_NORMAL:
        {
            if ((UD_VIDEOSOURCE_V_HDMI1 == Hv_App_GetOsdDataValue(E_USERDATA_VIDEOSOURCE)) ||
                (UD_VIDEOSOURCE_V_HDMI2 == Hv_App_GetOsdDataValue(E_USERDATA_VIDEOSOURCE)))
            {
                /* FreeSync */
                pstTextList->ucStringCurID = 0;
            }
            else
            {
                /* AdaptiveSync */
                pstTextList->ucStringCurID = 1;
            }
        }

        default:
        {
            break;
        }
    }
}

VOID Hv_App_Cb_OsdUpdateNumberByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdNumber *pstNumber = (OsdNumber *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstNumber);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);

    pstNumber->iValue = pstUserdata->sOsdValue;
}

VOID Hv_App_Cb_OsdUpdateNumberCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdNumber *pstNumber = (OsdNumber *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstNumber);
}

VOID Hv_App_Cb_OsdUpdateAsciiStringByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdAsciiString *pstAsciiString = (OsdAsciiString *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstAsciiString);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);
}

VOID Hv_App_Cb_OsdUpdateAsciiStringCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdAsciiString *pstAsciiString = (OsdAsciiString *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstAsciiString);
}

VOID Hv_App_Cb_OsdUpdateIconByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdIcon *pstIcon = (OsdIcon *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstIcon);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);
    const OsdIcon *pstIconProp = OSD_GET_ICON_PROP(usId);
    HV_ASSERT_VALID_PTR_VOID(pstIconProp);
    UINT32 uiDynamicFlag = pstIconProp->uiDynamicFlag;

    if (uiDynamicFlag & DYN_X)
    {
        pstIcon->usX = pstIconProp->usX + (pstUserdata->sOsdValue * 3);

        uiDynamicFlag &= ~DYN_X;
        if (uiDynamicFlag == 0)
        {
            return;
        }
    }
}

VOID Hv_App_Cb_OsdUpdateIconCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdIcon *pstIcon = (OsdIcon *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstIcon);
    const OsdIcon *pstIconProp = OSD_GET_ICON_PROP(usId);
    HV_ASSERT_VALID_PTR_VOID(pstIconProp);
    UINT32 uiDynamicFlag = pstIconProp->uiDynamicFlag;

    if (uiDynamicFlag & DYN_COLOR)
    {
        UINT32 uiCrosshairColor = 0xffffff;
        if (UD_DYN_CROSSHAIR_V_OFF != Hv_App_GetOsdDataValue(E_USERDATA_DYN_CROSSHAIR))
        {
            uiCrosshairColor = Hv_Mw_OsdCalculateRevertColor();
        }
        Hv_Mw_OsdSetComponentColor(COMPONENT(OSD_CP_TYPE_ICON, usId), uiCrosshairColor);
        return;
    }
}

VOID Hv_App_Cb_OsdUpdateIconListByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdIconList *pstIconList = (OsdIconList *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstIconList);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);

    pstIconList->ucIconCurID = MIN((USHORT16)pstUserdata->sOsdValue, pstIconList->ucIconNum - 1);
}

VOID Hv_App_Cb_OsdUpdateIconListCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdIconList *pstIconList = (OsdIconList *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstIconList);
}

VOID Hv_App_Cb_OsdUpdateBmpByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdBmp *pstBmp = (OsdBmp *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstBmp);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);
}

VOID Hv_App_Cb_OsdUpdateBmpCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdBmp *pstBmp = (OsdBmp *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstBmp);
}

VOID Hv_App_Cb_OsdUpdateBmpListByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdBmpList *pstBmpList = (OsdBmpList *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstBmpList);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);

    pstBmpList->ucBmpCurID = MIN((USHORT16)pstUserdata->sOsdValue, pstBmpList->ucBmpNum - 1);
}

VOID Hv_App_Cb_OsdUpdateBmpListCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdBmpList *pstBmpList = (OsdBmpList *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstBmpList);
}

VOID Hv_App_Cb_OsdUpdateRectByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdRect *pstRect = (OsdRect *)pstCpProp;
    const OsdRect *pstRectProp = OSD_GET_RECT_PROP(usId);
    HV_ASSERT_VALID_PTR_VOID(pstRectProp);
    HV_ASSERT_VALID_PTR_VOID(pstRect);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);
    UINT32 uiDynamicFlag = pstRectProp->uiDynamicFlag;

    /* for position slider bar */
    if ((uiDynamicFlag & DYN_X) && (uiDynamicFlag & DYN_WIDTH))
    {
        if (pstUserdata->sMaxValue == pstUserdata->sMinValue)
        {
            pstRect->usWidth = 0;
        }
        else
        {
            OSD_LOG_DEBUG("Dynamic rect(%d): x = %d, width = %d, ", usId, pstRect->usX, pstRect->usWidth);
            if (pstUserdata->sOsdValue > 50)
            {
                pstRect->usX = pstRectProp->usX + pstRectProp->usWidth / 2 - 2;
                pstRect->usWidth = (pstUserdata->sOsdValue - 50) * 3 + 3;
            }
            else if (pstUserdata->sOsdValue < 50)
            {
                pstRect->usX = pstRectProp->usX + pstUserdata->sOsdValue * 3;
                pstRect->usWidth = pstRectProp->usWidth / 2 - (pstRect->usX - pstRectProp->usX) + 4;
            }
            else
            {
                pstRect->usX = pstRectProp->usX + pstRectProp->usWidth / 2 - 2;
                pstRect->usWidth = 5;
            }
            OSD_LOG_DEBUG("Update dynamic rect(%d): x = %d, width = %d, ", usId, pstRect->usX, pstRect->usWidth);
        }

        uiDynamicFlag &= ~(DYN_X | DYN_WIDTH);
        if (uiDynamicFlag == 0)
        {
            return;
        }
    }

    /* for v slider bar */
    if ((uiDynamicFlag & DYN_Y) && (uiDynamicFlag & DYN_HEIGHT))
    {
        if (pstUserdata->sMaxValue == pstUserdata->sMinValue)
        {
            pstRect->usHeight = 0;
        }
        else
        {
            OSD_LOG_DEBUG("Dynamic rect(%d): y = %d, height = %d, ", usId, pstRect->usY, pstRect->usHeight);
            pstRect->usHeight = (USHORT16)(((UINT32)pstRectProp->usHeight * (UINT32)(pstUserdata->sOsdValue - pstUserdata->sMinValue))
                / (UINT32)(pstUserdata->sMaxValue - pstUserdata->sMinValue));
            pstRect->usY = pstRectProp->usY + pstRectProp->usHeight - pstRect->usHeight;
            OSD_LOG_DEBUG("Update dynamic rect(%d): y = %d, height = %d, ", usId, pstRect->usY, pstRect->usHeight);
        }

        uiDynamicFlag &= ~(DYN_Y | DYN_HEIGHT);
        if (uiDynamicFlag == 0)
        {
            return;
        }
    }

    /* for h slider bar */
    if (uiDynamicFlag & DYN_WIDTH)
    {
        if (pstUserdata->sMaxValue == pstUserdata->sMinValue)
        {
            pstRect->usWidth = 0;
        }
        else
        {
            OSD_LOG_DEBUG("Dynamic rect(%d): width = %d", usId, pstRect->usWidth);
            pstRect->usWidth = (USHORT16)(((UINT32)pstRectProp->usWidth * (UINT32)(pstUserdata->sOsdValue - pstUserdata->sMinValue))
                / (UINT32)(pstUserdata->sMaxValue - pstUserdata->sMinValue));
            OSD_LOG_DEBUG("Update dynamic rect(%d): width = %d", usId, pstRect->usWidth);
        }

        uiDynamicFlag &= ~DYN_WIDTH;
        if (uiDynamicFlag == 0)
        {
            return;
        }
    }

    /* for v slider bar oppsite */
    if (uiDynamicFlag & DYN_HEIGHT)
    {
        if (pstUserdata->sMaxValue == pstUserdata->sMinValue)
        {
            pstRect->usHeight = 0;
        }
        else
        {
            pstRect->usHeight = (USHORT16)(((UINT32)pstRectProp->usHeight * (UINT32)(pstUserdata->sMaxValue - pstUserdata->sOsdValue))
                / (UINT32)(pstUserdata->sMaxValue - pstUserdata->sMinValue));

        }

        uiDynamicFlag &= ~DYN_HEIGHT;
        if (uiDynamicFlag == 0)
        {
            return;
        }
    }
}

VOID Hv_App_Cb_OsdUpdateRectCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdRect *pstRect = (OsdRect *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstRect);
}

VOID Hv_App_Cb_OsdUpdateRectGridByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdRectGrid *pstRectGrid = (OsdRectGrid *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstRectGrid);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);
}

VOID Hv_App_Cb_OsdUpdateRectGridCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdRectGrid *pstRectGrid = (OsdRectGrid *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstRectGrid);
}

VOID Hv_App_Cb_OsdUpdateLineGridByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdLineGrid *pstLineGrid = (OsdLineGrid *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstLineGrid);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);
}

VOID Hv_App_Cb_OsdUpdateLineGridCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdLineGrid *pstLineGrid = (OsdLineGrid *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstLineGrid);
}

VOID Hv_App_Cb_OsdUpdateLineByUserdata(VOID *pstCpProp, USHORT16 usId, UserdataProp *pstUserdata)
{
    OsdLine *pstLine = (OsdLine *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstLine);
    HV_ASSERT_VALID_PTR_VOID(pstUserdata);
}

VOID Hv_App_Cb_OsdUpdateLineCustom(VOID *pstCpProp, USHORT16 usId)
{
    OsdLine *pstLine = (OsdLine *)pstCpProp;
    HV_ASSERT_VALID_PTR_VOID(pstLine);
}
