#include "ins_ut_mmc.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "ins_mw_mmc_event.h"
#include "ins_mw_media.h"
#include "ins_mw_device_rx.h"
#include "ins_mw_device_tx.h"
#include "ins_comm_log.h"
#include <app/record/record.h>
#include <app/live/live.h>
#include <app/applink/ambalink.h>
#include "ins_pal_memory.h"
#include "ins_pal_system.h"
#include "ins_dev_interface.h"
#include "ins_mw_ambalink.h"
#include "ins_mw_rpmsg.h"
#include "ins_app_factory.h"
#include "ins_app_extra.h"

static int32_t InsUT_MMC_LaunchModule(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    //test code dengyanbing 2021/12/07
    //InsPal_Memory_Init();
    //InsComm_Log_Init();

    InsMw_MMC_Init();
    InsMw_Media_Init();
    InsApp_Extra_Init();
    InsApp_Record_Init();
    //InsApp_Live_Init();
    InsMw_DeviceRx_Init();
    InsMw_DeviceTx_Init();
    InsApp_Factory_Init();
    return 0;
}

static int32_t InsUT_MMC_ExitModule(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    //test code dengyanbing 2021/12/07
    InsMw_DeviceRx_Deinit();
    InsMw_Media_Deinit();
    InsApp_Extra_DeInit();
    InsApp_Live_Deinit();
    InsApp_Record_Deinit();
    InsMw_MMC_Deinit();
    return 0;
}

static int32_t InsUT_MMC_ShowRegisterModule(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsMw_MMC_ShowRegisterModule();
    return 0;
}

static int32_t InsUT_MMC_ShowSubscribeEventAndModule(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsMw_MMC_ShowSubscribeEventAndModule();
    return 0;
}

static int32_t InsUT_MMC_ShowMemoryInfo(int32_t argc, char **argv)
{
    INSPAL_UNUSED(&argc);
    INSPAL_UNUSED(&argv);
    InsMw_MMC_ShowMemoryInfo();
    return 0;
}

static int32_t InsUT_MMC_SendEventToMMC(int32_t argc, char **argv)
{
    int16_t event_id = HAL_EVENT_BUTTON;
    int16_t event_num = 1;
    int32_t result = 0;
    int32_t i = 0;
    char  pstring[64]= {0};

    if ((argc != 2 || NULL == argv))
    {
        return INSUT_UNITTEST_USAGE;
    }

    event_id = atoi(argv[0]);
    event_num = atoi(argv[1]);

    for (i = 0;  i < event_num; i++)
    {
        sprintf(pstring, "%s:%d", "qwertyuiopasdfghjklzxcvbnm", (int)i);
        result = InsMw_MMC_PublishEventWithParameter(event_id, pstring, strlen(pstring), MODULE_HAL);
    }
    return result;
}

static int32_t InsUT_MMC_SubscribeEvent(int32_t argc, char **argv)
{
    int16_t event_id = HAL_EVENT_BUTTON;
    int8_t module_id = 1;

    int32_t result = 0;

    if ((argc != 2 || NULL == argv))
    {
        return INSUT_UNITTEST_USAGE;
    }

    module_id = atoi(argv[0]);
    event_id = atoi(argv[1]);

    result = InsMw_MMC_SubscribeEvent(module_id, event_id);
    return result;
}

static int32_t InsUT_MMC_SendButtonEvent(int32_t argc, char **argv)
{
    int16_t button_id = HAL_EVENT_BUTTON;
    int8_t button_status = 1;
    int32_t result = 0;
    InsDev_Event_Button_t st_button;
    if ((argc != 2 || NULL == argv))
    {
        return INSUT_UNITTEST_USAGE;
    }
    button_id = atoi(argv[0]);
    button_status = atoi(argv[1]);
    st_button.button_id = button_id;
    st_button.button_status = button_status;

#if 0 //comment by longtz
    result = InsDev_Device_EventPublish(EN_DEV_EVENT_TYPE_BUTTON, (void *)&st_button, sizeof(InsDev_Event_Button_t));
#else
    (void)st_button;
#endif
    return result;
}

#define INSUT_CMDCNT_MMC_DEBUG      (8)
InsUT_Unittest_CmdInfo_t g_ut_cmd_mmc_debug[INSUT_CMDCNT_MMC_DEBUG] =
{
    {InsUT_MMC_LaunchModule,                0,  NULL,   "launch_module",    "No Param"},
    {InsUT_MMC_ExitModule,                  0,  NULL,   "exit_module",      "No Param"},
    {InsUT_MMC_ShowRegisterModule,          0,  NULL,   "show_module",      "No Param"},
    {InsUT_MMC_ShowSubscribeEventAndModule, 0,  NULL,   "show_event",       "No Param"},
    {InsUT_MMC_ShowMemoryInfo,              0,  NULL,   "show_memory",      "No Param"},
    {InsUT_MMC_SendEventToMMC,              0,  NULL,   "send_event",       "[event_id] [event_num]"},
    {InsUT_MMC_SubscribeEvent,              0,  NULL,   "subscribe_event",  "[module_id] [event_id]"},
    {InsUT_MMC_SendButtonEvent,             0,  NULL,   "button_event",     "[button_id] [button_status]"},
    //{InsUT_GUI_DebugToast,            0,  NULL,   "toast",        "[text]"},
};

InsUT_Unittest_CmdInfo_t g_ut_cmd_mmc[INSUT_CMDCNT_MMC] =
{
    {NULL,  INSUT_CMDCNT_MMC_DEBUG,     g_ut_cmd_mmc_debug,     "debug",        "MMC Debug"},
};

