/**
 * @file hv_cal_OsdIndex.c
 * @brief Osd Cal Index interface file.
 * @details This file provides the following functions: \n
 *          (1) Osd CAL layer initialization functions \n
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-31
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author         <th>Date        <th>Change Description
 * <tr><td>OSD Group      <td>2022-08-31   <td>create
 * </table>
*/

#include "hv_drv_Osd.h"
#include "hv_cal_Osd.h"

VOID Hv_Cal_OsdIndex_SetRowAttr(OsdIndexRow *pstRow, BOOL bIsNormalRow)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);
    INDEXROW(pstRow)->rowAttr = bIsNormalRow ? 0 : 1;
}

BOOL Hv_Cal_OsdIndex_GetRowEnable(OsdIndexRow *pstRow)
{
    return INDEXROW(pstRow)->rowDisable == 1 ? HV_FALSE : HV_TRUE;
}

VOID Hv_Cal_OsdIndex_SetRowEnable(OsdIndexRow *pstRow, BOOL bEnable)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);
    INDEXROW(pstRow)->rowDisable = bEnable ? 0 : 1;
}

VOID Hv_Cal_OsdIndex_SetRowType(OsdIndexRow *pstRow, OsdRowType enType)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);
    switch (enType)
    {
        case OSD_ROW_FONT:
        {
            INDEXROW(pstRow)->rowType = 0;
            break;
        }
        case OSD_ROW_BMP:
        {
            INDEXROW(pstRow)->rowType = 1;
            break;
        }
        default:
        {
            break;
        }
    }
}

UCHAR8 Hv_Cal_OsdIndex_GetRowLen(OsdIndexRow *pstRow)
{
    HV_ASSERT_TRUE_RET(pstRow != NULL, 0);
    return INDEXROW(pstRow)->rowLen;
}

VOID Hv_Cal_OsdIndex_SetRowLen(OsdIndexRow *pstRow, UCHAR8 ucRowLen)
{
    HV_ASSERT_TRUE_VOID(ucRowLen <= 0xfe);
    HV_ASSERT_TRUE_VOID(pstRow != NULL);

    INDEXROW(pstRow)->rowLen = ucRowLen;
}

VOID Hv_Cal_OsdIndex_SetRowBlinkEnable(OsdIndexRow *pstRow, BOOL bEnable)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);

    INDEXROW(pstRow)->blinkEnable = bEnable;
}

VOID Hv_Cal_OsdIndex_SetRowFontLibSel(OsdIndexRow *pstRow, BOOL bSelect)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);

    INDEXROW(pstRow)->fontCurIdx = bSelect;
}

VOID Hv_Cal_OsdIndex_SetRowHSpace(OsdIndexRow *pstRow, UCHAR8 ucNum)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);
    HV_ASSERT_TRUE_VOID(ucNum <= 0x0f);

    INDEXROW(pstRow)->curHspcNum = ucNum;
}

VOID Hv_Cal_OsdIndex_SetRowVSpace(OsdIndexRow *pstRow, UCHAR8 ucNum)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);
    HV_ASSERT_TRUE_VOID(ucNum <= 0x0f);

    INDEXROW(pstRow)->curVspcNum = ucNum;
}

VOID Hv_Cal_OsdIndex_SetRowFontType(OsdIndexRow *pstRow, OsdResourceType enType)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);

    INDEXROW(pstRow)->fontType = enType;
}

VOID Hv_Cal_OsdIndex_SetRowFontLibIndex(OsdIndexRow *pstRow, UCHAR8 ucIndex)
{
    HV_ASSERT_TRUE_VOID(pstRow != NULL);
    HV_ASSERT_TRUE_VOID(ucIndex < 8);

    INDEXROW(pstRow)->attrHsz = ucIndex;

    //OSD_DRV_LOG_INFO("Set bank %d", ucIndex);
}

USHORT16 Hv_Cal_OsdIndex_GetColIndex(OsdIndexColumn *pstCol)
{
    HV_ASSERT_TRUE_RET(pstCol != NULL, 0);

    return INDEXCOLUMN(pstCol)->fontCurIdx;
}

VOID Hv_Cal_OsdIndex_SetColIndex(OsdIndexColumn *pstCol, USHORT16 usIndex)
{
    HV_ASSERT_TRUE_VOID(pstCol != NULL);
    HV_ASSERT_TRUE_VOID(usIndex <= 0x1ff);

    INDEXCOLUMN(pstCol)->fontCurIdx = usIndex;
}

BOOL Hv_Cal_OsdIndex_IsBlankColumn(OsdIndexColumn *pstCol)
{
    HV_ASSERT_TRUE_RET(pstCol != NULL, HV_TRUE);

    return INDEXCOLUMN(pstCol)->blankFontEn == 1;
}

OsdColumnType Hv_Cal_OsdIndex_GetColType(OsdIndexColumn *pstCol)
{
    HV_ASSERT_TRUE_RET(pstCol != NULL, 0);
    UCHAR8 ucDelta = 0;

    if (INDEXCOLUMN(pstCol)->blankFontEn)
    {
        return OSD_COLUMN_BLANK;
    }

    ucDelta = Hv_Cal_OsdIndex_GetColDelta(pstCol);
    if (ucDelta == 0x1f)
    {
        return OSD_COLUMN_BMP;
    }
    else
    {
        return OSD_COLUMN_FONT;
    }
}

VOID Hv_Cal_OsdIndex_SetColType(OsdIndexColumn *pstCol, OsdColumnType enType)
{
    HV_ASSERT_TRUE_VOID(pstCol != NULL);

    switch (enType)
    {
        case OSD_COLUMN_BLANK:
        {
            INDEXCOLUMN(pstCol)->blankFontEn = 1;
            break;
        }
        case OSD_COLUMN_FONT:
        {
            INDEXCOLUMN(pstCol)->blankFontEn = 0;
            break;
        }
        case OSD_COLUMN_BMP:
        {
            INDEXCOLUMN(pstCol)->blankFontEn = 0;
            Hv_Cal_OsdIndex_SetColDelta(pstCol, 1, 0x1f);

            break;
        }
        default:
        {
            break;
        }
    }

    //OSD_DRV_LOG_DEBUG("column 0x%x set blank font en %d", pstCol, INDEXCOLUMN(pstCol)->blankFontEn);
}

VOID Hv_Cal_OsdIndex_SetFontColColorIndex(OsdIndexColumn *pstCol, USHORT16 usColorIndex)
{
    HV_ASSERT_TRUE_VOID(pstCol != NULL);
    HV_ASSERT_TRUE_VOID(usColorIndex <= 0xff);

    INDEXCOLUMN(pstCol)->fgcIdx = usColorIndex;
}

USHORT16 Hv_Cal_OsdIndex_GetBMPColColorIndex(OsdIndexColumn *pstCol)
{
    USHORT16 usOffset = 0;
    USHORT16 usColorIndex = 0;

    if (pstCol->fgcIdx & 0x01)
    {
        usOffset = 256;
    }

    usColorIndex = INDEXCOLUMN(pstCol)->fgcIdx;

    return (usColorIndex & (~0x01)) + usOffset;
}

VOID Hv_Cal_OsdIndex_SetBMPColColorIndex(OsdIndexColumn *pstCol, USHORT16 usColorIndex)
{
    BOOL bUseFontPalette = HV_TRUE;

    HV_ASSERT_TRUE_VOID(pstCol != NULL);

    if (usColorIndex >= 256)
    {
        usColorIndex -= 256;
        bUseFontPalette = HV_FALSE;
    }

    INDEXCOLUMN(pstCol)->fgcIdx = usColorIndex;
    if (bUseFontPalette)
    {
        Hv_Cal_OsdIndex_SetBmpstColPaletteSel(pstCol, OSD_PALETTE_TYPE_FONT);
    }
    else
    {
        Hv_Cal_OsdIndex_SetBmpstColPaletteSel(pstCol, OSD_PALETTE_TYPE_BMP);
    }
}

VOID Hv_Cal_OsdIndex_SetColBlankWidth(OsdIndexColumn *pstCol, USHORT16 usBlankWidth)
{
    HV_ASSERT_TRUE_VOID(pstCol != NULL);
    HV_ASSERT_TRUE_VOID(usBlankWidth <= 0xff);
    HV_ASSERT_TRUE_VOID(usBlankWidth >= 0x1);

    INDEXCOLUMN(pstCol)->fgcIdx = usBlankWidth - 1;
}

VOID Hv_Cal_OsdIndex_SetBmpstColPaletteSel(OsdIndexColumn *pstCol, OsdPaletteType enType)
{
    if (OSD_PALETTE_TYPE_FONT == enType)
    {
        pstCol->fgcIdx &= ~0x01;
    }
    else
    {
        pstCol->fgcIdx |= 0x01;
    }
}

BOOL Hv_Cal_OsdIndex_DeltaIsIncrease(const OsdIndexColumn *pstCol)
{
    HV_ASSERT_TRUE_RET(pstCol != NULL, 0);

    return pstCol->deltaDir == 1;
}

UCHAR8 Hv_Cal_OsdIndex_GetColDelta(const OsdIndexColumn *pstCol)
{
    HV_ASSERT_TRUE_RET(pstCol != NULL, 0);

    return pstCol->deltaValue;
}

VOID Hv_Cal_OsdIndex_SetColDelta(OsdIndexColumn *pstCol, BOOL bIsIncrease,  UCHAR8 ucDelta)
{
    HV_ASSERT_TRUE_VOID(pstCol != NULL);
    HV_ASSERT_TRUE_VOID(ucDelta <= 0x1f);

    INDEXCOLUMN(pstCol)->deltaDir = bIsIncrease;
    INDEXCOLUMN(pstCol)->deltaValue = ucDelta;
}
