/**
 * @file hv_comm_shell_osd_debug.c
 * @brief osd-shell-debug c file.
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2023-02-14
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                        <th>Date            <th>Change Description
 * <tr><td>HiView SoC Software Team      <td>2023-09-14      <td>init
 * </table>
 */

#include "hv_comm_shell_hvconfig.h"
#include "hv_comm_DataBase.h"
#include "hv_mw_OsdEvent.h"
#include "hv_mw_OsdOperation.h"
#include "hv_mw_OsdDraw.h"
#include "hv_drv_Osd.h"
#if (HV_OSD_CONFIG_AUTO_TEST == HV_CONFIG_ON)
#include "hv_app_OsdTest.h"
#endif


extern Status hv_mw_EventHub_SendOsdEvent(USHORT16 usEventId, USHORT16 usValue);
extern VOID Hv_Mw_Osd_PrintDebugEventQueue(VOID);

int Hv_Shell_SetOsdCmd(int argc, char* argv[])
{
    USHORT16 usEventId = 0;
    static USHORT16 s_usPrevValue = 0;
    static BOOL bNoLatch = HV_FALSE;
    USHORT16 usValue = 0;

    if (3 == argc)
    {
        if (0 == strcmp("p", argv[1]))
        {
            usEventId = OSD_EVENT_KEY_PRESS;
            if (0 == strcmp("u", argv[2]))
                usValue = OSDEVT_KEY_UP;
            else if (0 == strcmp("d", argv[2]))
                usValue = OSDEVT_KEY_DOWN;
            else if (0 == strcmp("l", argv[2]))
                usValue = OSDEVT_KEY_LEFT;
            else if (0 == strcmp("r", argv[2]))
                usValue = OSDEVT_KEY_RIGHT;
            else if (0 == strcmp("e", argv[2]))
                usValue = OSDEVT_KEY_ENTER;
            else if (0 == strcmp("x", argv[2]))
                usValue = OSDEVT_KEY_EXIT;
            else if (0 == strcmp("p", argv[2]))
                usValue = OSDEVT_KEY_POWER;
            else if (0 == strcmp("h1", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY1;
            else if (0 == strcmp("h2", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY2;
            else if (0 == strcmp("h3", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY3;
            else if (0 == strcmp("c1", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM1;
            else if (0 == strcmp("c2", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM2;
            else if (0 == strcmp("c3", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM3;
            else if (0 == strcmp("c4", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM4;
            else if (0 == strcmp("c5", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM5;
            else
                goto OSD_CMD_ERROR;
            s_usPrevValue = usValue;
        }
        else if (0 == strcmp("h", argv[1]))
        {
            usEventId = OSD_EVENT_KEY_HOLD;
            if (0 == strcmp("u", argv[2]))
                usValue = OSDEVT_KEY_UP;
            else if (0 == strcmp("d", argv[2]))
                usValue = OSDEVT_KEY_DOWN;
            else if (0 == strcmp("l", argv[2]))
                usValue = OSDEVT_KEY_LEFT;
            else if (0 == strcmp("r", argv[2]))
                usValue = OSDEVT_KEY_RIGHT;
            else if (0 == strcmp("e", argv[2]))
                usValue = OSDEVT_KEY_ENTER;
            else if (0 == strcmp("x", argv[2]))
                usValue = OSDEVT_KEY_EXIT;
            else if (0 == strcmp("p", argv[2]))
                usValue = OSDEVT_KEY_POWER;
            else if (0 == strcmp("h1", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY1;
            else if (0 == strcmp("h2", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY2;
            else if (0 == strcmp("h3", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY3;
            else if (0 == strcmp("c1", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM1;
            else if (0 == strcmp("c2", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM2;
            else if (0 == strcmp("c3", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM3;
            else if (0 == strcmp("c4", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM4;
            else if (0 == strcmp("c5", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM5;
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("r", argv[1]))
        {
            usEventId = OSD_EVENT_KEY_RELEASE;
            if (0 == strcmp("u", argv[2]))
                usValue = OSDEVT_KEY_UP;
            else if (0 == strcmp("d", argv[2]))
                usValue = OSDEVT_KEY_DOWN;
            else if (0 == strcmp("l", argv[2]))
                usValue = OSDEVT_KEY_LEFT;
            else if (0 == strcmp("r", argv[2]))
                usValue = OSDEVT_KEY_RIGHT;
            else if (0 == strcmp("e", argv[2]))
                usValue = OSDEVT_KEY_ENTER;
            else if (0 == strcmp("x", argv[2]))
                usValue = OSDEVT_KEY_EXIT;
            else if (0 == strcmp("p", argv[2]))
                usValue = OSDEVT_KEY_POWER;
            else if (0 == strcmp("h1", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY1;
            else if (0 == strcmp("h2", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY2;
            else if (0 == strcmp("h3", argv[2]))
                usValue = OSDEVT_KEY_HOTKEY3;
            else if (0 == strcmp("c1", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM1;
            else if (0 == strcmp("c2", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM2;
            else if (0 == strcmp("c3", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM3;
            else if (0 == strcmp("c4", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM4;
            else if (0 == strcmp("c5", argv[2]))
                usValue = OSDEVT_KEY_CUSTOM5;
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("f", argv[1]))
        {
            if (0 == strcmp("e", argv[2]))
            {
                usEventId = OSD_EVENT_FACTORY_ENTER;
                usValue = 0;
            }
            else if (0 == strcmp("q", argv[2]))
            {
                usEventId = OSD_EVENT_FACTORY_EXIT;
                usValue = 0;
            }
            else if (0 == strcmp("a", argv[2]))
            {
                usEventId = OSD_EVENT_FACTORY_AGING_REFRESH;
                usValue = 0;
            }
            else if (0 == strcmp("x", argv[2]))
            {
                usEventId = OSD_EVENT_FACTORY_AGING_EXIT;
                usValue = 0;
            }
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("rotate", argv[1]))
        {
            if (0 == strcmp("0", argv[2]))
            {
                Hv_Mw_OsdSetRotateType(0);
                return 0;
            }
            else if (0 == strcmp("90", argv[2]))
            {
                Hv_Mw_OsdSetRotateType(1);
                return 0;
            }
            else if (0 == strcmp("180", argv[2]))
            {
                Hv_Mw_OsdSetRotateType(2);
                return 0;
            }
            else if (0 == strcmp("270", argv[2]))
            {
                Hv_Mw_OsdSetRotateType(3);
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("px", argv[1]))
        {
            USHORT16 usPos = atoi(argv[2]);
            usPos = MIN(100, usPos);
            UCHAR8 ucScene = Hv_Mw_OsdSceneGetShowing(0);
            if (SCENE_ID_MAX != ucScene)
            {
                Hv_Mw_OsdSceneSetPositionRatio(ucScene, HV_TRUE, usPos, 0, INVALID_PARAM_USHORT16);
            }
            else
            {
                HV_LOG_RAW("No scene is showing\r\n");
            }
            return 0;
        }
        else if (0 == strcmp("py", argv[1]))
        {
            USHORT16 usPos = atoi(argv[2]);
            usPos = MIN(100, usPos);
            UCHAR8 ucScene = Hv_Mw_OsdSceneGetShowing(0);
            if (SCENE_ID_MAX != ucScene)
            {
                Hv_Mw_OsdSceneSetPositionRatio(ucScene, HV_FALSE, usPos, 0, INVALID_PARAM_USHORT16);
            }
            else
            {
                HV_LOG_RAW("No scene is showing\r\n");
            }
            return 0;
        }
        else if (0 == strcmp("reg", argv[1]))
        {
            if (0 == strcmp("latch", argv[2]))
            {
                if (bNoLatch)
                {
                    HV_W32_FIELD(DISP_TOP_KOSD_TOP_00, reg_en_no_latch, 0);
                    bNoLatch = HV_FALSE;
                    HV_LOG_RAW("reg_en_no_latch = 0\r\n");
                }
                else
                {
                    HV_W32_FIELD(DISP_TOP_KOSD_TOP_00, reg_en_no_latch, 1);
                    bNoLatch = HV_TRUE;
                    HV_LOG_RAW("reg_en_no_latch = 1\r\n");
                }
                return 0;
            }
            else if (0 == strcmp("en", argv[2]))
            {
                HV_W32_FIELD(DISP_TOP_KOSD_TOP_00, reg_en, 1);
                HV_LOG_RAW("set reg_en\r\n");
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("b", argv[1]))
        {
            if (0 == strcmp("b", argv[2]))
            {
                Hv_Mw_OsdSceneDebugStat();
                return 0;
            }
            else if (0 == strcmp("r", argv[2]))
            {
                Hv_Mw_OsdResourceDebugStat();
                return 0;
            }
            else if (0 == strcmp("clear", argv[2]))
            {
                Hv_Mw_OsdDbgDrawClear();
                return 0;
            }
            else if (0 == strcmp("u", argv[2]))
            {
                Hv_Mw_OsdUserdataDebugStat();
                return 0;
            }
            else if (0 == strcmp("e", argv[2]))
            {
                Hv_Mw_Osd_PrintDebugEventQueue();
                return 0;
            }
            else if (0 == strcmp("d", argv[2]))
            {
                Hv_Drv_OsdDMA_DebugPrintState();
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("logo", argv[1]))
        {
            if (0 == strcmp("s", argv[2]))
            {
                Hv_Mw_OsdSetLogoState(OSD_LOGO_WAITING_SHOW);
                usEventId = OSD_EVENT_SHOW_LOGO;
                usValue = 1;
            }
            else if (0 == strcmp("h", argv[2]))
            {
                usEventId = OSD_EVENT_TIMER_LOGO_HIDE;
            }
            else if (0 == strcmp("e", argv[2]))
            {
                BOOL bShowLogo = HV_COMMON_DATABASE_GET(SystemData, bShowLogoFlag);
                HV_COMMON_DATABASE_SET(SystemData, bShowLogoFlag, !bShowLogo);
                HV_LOG_RAW("set bShowLogoFlag = %d\r\n", !bShowLogo);
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("lang", argv[1]))
        {
            USHORT16 usLang = atoi(argv[2]);
            Hv_Mw_OsdSetLanguage(MIN(usLang, OSD_LANGUAGE_COUNT - 1));
            HV_LOG_RAW("set lang = %d\r\n", MIN(usLang, OSD_LANGUAGE_COUNT - 1));
            return 0;
        }
        else if (0 == strcmp("timer", argv[1]))
        {
            if (0 == strcmp("0", argv[2]))
            {
                Hv_Mw_OsdSetAutoCloseTimerEnable(HV_FALSE);
                return 0;
            }
            else if (0 == strcmp("1", argv[2]))
            {
                Hv_Mw_OsdSetAutoCloseTimerEnable(HV_TRUE);
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("t", argv[1]))
        {
            if (0 == strcmp("p", argv[2]))
            {
#if (HV_PROJECT_CONFIG_OSD_MARQUEE == HV_CONFIG_ON)
                Hv_Drv_OsdFont_MarqueeDebug();
                //Hv_Drv_OsdFont_ResumeAll(0, 0);
                //while (Hv_Drv_OsdFont_NeedMarquee())
                //{
                //    Hv_Cal_Osd_TimeDelayMs(50);
                //    //HV_WAIT_KEY();
                //    //OSD_STUB("step. ");
                //    Hv_Drv_OsdFont_MarqueeStep();

                //    Hv_Drv_OsdFrame_DrawPost();
                //    Hv_Drv_OsdFrame_SetSync();
                //}
#endif

                return 0;
            }
            else if (0 == strcmp("n", argv[2]))
            {
#if (HV_PROJECT_CONFIG_OSD_MARQUEE == HV_CONFIG_ON)
                //Hv_Drv_OsdFont_ResumeAll(1, 1);
                //while (Hv_Drv_OsdFont_NeedMarquee())
                //{
                //    Hv_Cal_Osd_TimeDelayMs(50);
                    //HV_WAIT_KEY();
                    //OSD_STUB("step. ");
                    Hv_Drv_OsdFont_MarqueeStep();

                    Hv_Drv_OsdFrame_DrawPost();
                    Hv_Drv_OsdFrame_SetSync();
                //}
#endif

                return 0;
            }
            else if (0 == strcmp("r", argv[2]))
            {
#if (HV_PROJECT_CONFIG_OSD_MARQUEE == HV_CONFIG_ON)
                Hv_Drv_OsdFont_ResetAll();
                    //HV_WAIT_KEY();
                    //OSD_STUB("step. ");

                    Hv_Drv_OsdFrame_DrawPost();
                    Hv_Drv_OsdFrame_SetSync();
#endif

                return 0;
            }
            else if (0 == strcmp("2", argv[2]))
            {
                //Hv_Drv_OsdLogo_ShowLogo();
#if (HV_PROJECT_CONFIG_OSD_GRAPHIC == HV_CONFIG_ON)
                OsdGraphic stGraphic;
                OsdGraphicAddition stGraphicAddition = {0};
                HV_MEMSET(&stGraphic, 0, sizeof(stGraphic));
                stGraphic.usX = 100;
                stGraphic.usY = 100;
                stGraphic.usWidth = 640;
                stGraphic.usHeight = 640;
                stGraphic.ucFormat = 5;
                stGraphic.ucCompressed = 1;
                stGraphic.ucAlpha = 255;
                Hv_Drv_OsdGraphic_DrawGraphic(&stGraphic, &stGraphicAddition);
                Hv_Drv_OsdFrame_SetSync();
#endif
                return 0;
            }
            else if (0 == strcmp("3", argv[2]))
            {
                Hv_Drv_OsdTest();
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("a", argv[1]))
        {
#if (HV_OSD_CONFIG_AUTO_TEST == HV_CONFIG_ON)
            usEventId = OSD_EVENT_TEST;
            if (0 == strcmp("all", argv[2]))
            {
                Hv_App_OsdTestSceneAllStart();
                return 0;
            }
            else if (0 == strcmp("s", argv[2]))
            {
                Hv_App_OsdTestSceneStart();
                return 0;
            }
            else if (0 == strcmp("pos", argv[2]))
            {
                Hv_App_OsdTestPosition();
                return 0;
            }
            else if (0 == strcmp("r", argv[2]))
            {
                usValue = OSDEVT_TEST_ROTATION;
            }
            else if (0 == strcmp("a", argv[2]))
            {
                usValue = OSDEVT_TEST_ALPHA;
            }
            else if (0 == strcmp("cr", argv[2]))
            {
                usValue = OSDEVT_TEST_CROSSHARE;
            }
            else if (0 == strcmp("p", argv[2]))
            {
                Hv_App_OsdTestPauseContinue();
                return 0;
            }
            else if (0 == strcmp("t", argv[2]))
            {
                usValue = OSDEVT_TEST_TEXT;
            }
            else if (0 == strcmp("tn", argv[2]))  //text next
            {
                Hv_App_OsdTestTextNextPage();
                return 0;
            }
            else if (0 == strcmp("tr", argv[2]))  //text right
            {
                Hv_App_OsdTestTextNextLanguage();
                return 0;
            }
            else if (0 == strcmp("tl", argv[2])) //text left
            {
                Hv_App_OsdTestTextPrevLanguage();
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
#endif
        }
        else
        {
            goto OSD_CMD_ERROR;
        }
    }
    else if (2 == argc)
    {
        if (0 == strcmp("h", argv[1]))
        {
            usEventId = OSD_EVENT_KEY_HOLD;
            usValue = s_usPrevValue;
        }
        else if (0 == strcmp("r", argv[1]))
        {
            usEventId = OSD_EVENT_KEY_RELEASE;
            usValue = s_usPrevValue;
        }
        else if (0 == strcmp("s", argv[1]))
        {
            usEventId = OSD_EVENT_SIGNAL_STATE_CHANGE;
            usValue = 0;
        }
        else if (0 == strcmp("u", argv[1]))
        {
            usEventId = OSD_EVENT_SIGNAL_TIMING_NOT_SUPPORT;
            usValue = 0;
        }
        else if (0 == strcmp("k", argv[1]))
        {
            Hv_Mw_OsdSetAllKeyEnable(HV_TRUE);
            return 0;
        }
        else if (0 == strcmp("i", argv[1]))
        {
            usEventId = OSD_EVENT_SIGNAL_PLUG_IN;
            usValue = 0;
        }
        else if (0 == strcmp("o", argv[1]))
        {
            usEventId = OSD_EVENT_SIGNAL_PLUG_OUT;
            usValue = 0;
        }
        else if (0 == strcmp("x", argv[1]))
        {
            usEventId = OSD_EVENT_HIDE_ALL;
            usValue = 0;
        }
        else if (0 == strcmp("sync", argv[1]))
        {
            usEventId = OSD_EVENT_SYNC;
        }
        else if (0 == strcmp("m", argv[1]))
        {
#ifdef HV_SCALER_DEBUG_VERSION
            if (Hv_Mw_OsdIsMonkeyRunning())
            {
                Hv_Mw_OsdMonkeyTest(HV_FALSE);
            }
            else
            {
                Hv_Mw_OsdMonkeyTest(HV_TRUE);
            }
#endif
            return 0;
        }
        else
            goto OSD_CMD_ERROR;
    }
    else if (4 == argc)
    {
        if (0 == strcmp("e", argv[1]))
        {
            usEventId = atoi(argv[2]);
            usValue = atoi(argv[3]);
        }
#if (HV_OSD_CONFIG_AUTO_TEST == HV_CONFIG_ON)
        else if (0 == strcmp("a", argv[1]))
        {
            if (0 == strcmp("time", argv[2]))
            {
                Hv_App_OsdTestSetDelayTime(atoi(argv[3]));
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
        }
        else if (0 == strcmp("t", argv[1]))
        {
            if (0 == strcmp("w", argv[2]))
            {
                Hv_App_OsdTestTextSetWinWidth(atoi(argv[3]));
                return 0;
            }
            else if (0 == strcmp("h", argv[2]))
            {
                Hv_App_OsdTestTextSetWinHeight(atoi(argv[3]));
                return 0;
            }
            else if (0 == strcmp("cols", argv[2]))
            {
                Hv_App_OsdTestTextSetColumns(atoi(argv[3]));
                return 0;
            }
            else if (0 == strcmp("color", argv[2]))
            {
                Hv_App_OsdTestTextSetColor(atoi(argv[3]));
                return 0;
            }
            else if (0 == strcmp("lang", argv[2]))
            {
                Hv_App_OsdTestTextSetSartLang(atoi(argv[3]));
                return 0;
            }
            else
                goto OSD_CMD_ERROR;
        }

#endif
        else
            goto OSD_CMD_ERROR;
    }
    else
    {
        goto OSD_CMD_ERROR;
    }

    HV_LOG_RAW("Send osd Event-%d-%d\r\n", usEventId, usValue);
    hv_mw_EventHub_SendOsdEvent(usEventId, usValue);
    return 0;

OSD_CMD_ERROR:
    HV_LOG_RAW("************************************************\r\n");
    HV_LOG_RAW("* KEY PRESS:   osd p <KeyValue>\r\n");
    HV_LOG_RAW("*     <KeyValue>: u-up, d-down,, l-left, r-right, e-enter, x-exit\r\n");
    HV_LOG_RAW("* KEY HOLD:    osd h\r\n");
    HV_LOG_RAW("* KEY RELEASE: osd r\r\n");
    HV_LOG_RAW("* Enable all key: osd k\r\n");
    HV_LOG_RAW("* Signal Change:   osd s\r\n");
    HV_LOG_RAW("* Signal unsupport:   osd u\r\n");
    HV_LOG_RAW("* Plug in:   osd i\r\n");
    HV_LOG_RAW("* Plug out:   osd o\r\n");
    HV_LOG_RAW("* Hide all:   osd x\r\n");
    HV_LOG_RAW("* Show Logo:   osd logo s\r\n");
    HV_LOG_RAW("* Hide Logo:   osd logo h\r\n");
    HV_LOG_RAW("* Enable/disable Logo:   osd logo e\r\n");
    HV_LOG_RAW("* Enter Factory Mode:   osd f e\r\n");
    HV_LOG_RAW("* Exit Factory Mode:   osd f q\r\n");
    HV_LOG_RAW("* Refresh factoy aging scene:   osd f a\r\n");
    HV_LOG_RAW("* Exit factoy aging:   osd f x\r\n");
    HV_LOG_RAW("* Set language:   osd lang <value>\r\n");
    HV_LOG_RAW("* Send sync:   osd sync\r\n");
    HV_LOG_RAW("* Set reg_en:   osd reg en\r\n");
    HV_LOG_RAW("* Set reg_en_latch:   osd reg latch\r\n");
    HV_LOG_RAW("* Monkey test switch:   osd m\r\n");
    HV_LOG_RAW("* Osd rotate:   osd rotate <RotateType>\r\n");
    HV_LOG_RAW("*     <RotateType>: 0, 90, 180, 270\r\n");
    HV_LOG_RAW("* Auto close timer enable:   osd timer <0/1>\r\n");
    HV_LOG_RAW("* Osd position X:   osd px <0~100>\r\n");
    HV_LOG_RAW("* Osd position Y:   osd py <0~100>\r\n");
    HV_LOG_RAW("* Print osd scene debug info:   osd b b\r\n");
    HV_LOG_RAW("* Print osd resource debug info:   osd b r\r\n");
    HV_LOG_RAW("* Print userdata value:   osd b u\r\n");
    HV_LOG_RAW("* Print debug event queue:   osd b e\r\n");
    HV_LOG_RAW("* Print debug DMA:   osd b d\r\n");
    HV_LOG_RAW("* test:   osd t <value>\r\n");
    HV_LOG_RAW("* Auto test:   osd a <TestValue>\r\n");
    HV_LOG_RAW("*     <TestValue>: all-scene, pos-position, r-rotation, a-alpha, cr-crosshair, p-pause_continue,t-text,n-text next page\r\n");
    HV_LOG_RAW("* Send any event to OSD:   osd e <EventId> <Value>\r\n");
    HV_LOG_RAW("************************************************\r\n");
    return -1;
}

