/**
 *  @file ins_ut_config.c
 *  @details config unit test
 *
 */

#include <stdlib.h>
#include <string.h>
#include "unittest.h"
#include "unittest/config/ins_ut_config.h"
#include "ins_mw_config.h"
#include "ins_mw_configmgr.h"
#include "ins_mw_config_capability.h"
#include "ins_pal_error_code.h"
#include "ins_comm_log.h"

static int32_t InsUT_Config_Init(int32_t argc, char **argv)
{
    (void)argc;
    (void)argv;
    return (int32_t)InsMw_Cfg_Init();
}

static int32_t InsUT_Config_Save(int32_t argc, char **argv)
{
    int32_t part = 0;

    if(argc < 1)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    part = atoi(argv[0]);
    return (int32_t)InsMw_Cfg_Save(part);
}

static int32_t InsUT_Config_Print(int32_t argc, char **argv)
{
    int32_t id = 0;
    if(argc <= 0)
    {
        return InsMw_Cfg_PrintAll();
    }
    id = atoi(argv[0]);

    return (int32_t)InsMw_Cfg_PrintId(id);
}

static int32_t InsUT_Config_Set(int32_t argc, char **argv)
{
    int32_t id = 0;
    int32_t value[64] = {0};

    if(argc < 2)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    id = atoi(argv[0]);
    value[0] = atoi(argv[1]);
    return (int32_t)InsMw_Cfg_SetConfig((uint32_t)id, (void *)value, sizeof(int)+64);
}

static int32_t InsUT_Config_Get(int32_t argc, char **argv)
{
    int32_t id = 0;
    int32_t value = 0;
    int32_t ret = 0;

    if(argc < 2)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    id = atoi(argv[0]);
    value = atoi(argv[1]);
    ret = (int32_t)InsMw_Cfg_GetConfig((uint32_t)id, (void *)&value, sizeof(value));
    if(INSERR_OK == ret)
    {
        INSLOG_I("Get id[%d] success, data:%d", id, value);
        InsMw_Cfg_PrintId(id);
    }
    else
    {
        INSLOG_E("Get id[%d] failed", id);
    }
    return ret;
}

static int32_t InsUT_Config_Reset(int32_t argc, char **argv)
{
    int32_t part = 0;

    if(argc < 1)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    part = atoi(argv[0]);
    return (int32_t)InsMw_Cfg_LoadDefault(part);
}

static int32_t InsUT_Config_Destroy(int32_t argc, char **argv)
{
    int32_t part = 0;
    int32_t region = 0;

    if(argc < 2)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    part = atoi(argv[0]);
    region = atoi(argv[1]);
    return (int32_t)InsMw_Cfg_Destroy(part, region);
}

InsUT_Unittest_CmdInfo_t g_ut_cmd_config_base[INSUT_CMDCNT_CONFIG_BASE] =
{
    {InsUT_Config_Init,     0, NULL, "init",        "No Param: Init config"},
    {InsUT_Config_Print,    0, NULL, "print",       "No Param: Print all; [ID]: Print sigle id"},
    {InsUT_Config_Save,     0, NULL, "save",        "[partition]: Save sigle partition"},
    {InsUT_Config_Reset,    0, NULL, "reset",       "[partition]: Reset sigle partition"},
    {InsUT_Config_Set,      0, NULL, "set",         "[id] [value]: Set sigle id(only support int type)"},
    {InsUT_Config_Get,      0, NULL, "get",         "[id] [value]: Get sigle id"},
    {InsUT_Config_Destroy,  0, NULL, "destroy",     "[part] [region]: Destroy cfg data"},
};

static int32_t InsUT_ConfigMgr_Init(int32_t argc, char **argv)
{
    (void)argc;
    (void)argv;
    return (int32_t)InsMw_CfgMgr_Init();
}

static int32_t InsUT_ConfigMgr_Switch(int32_t argc, char **argv)
{
    InsMw_CfgProfileKey_t key;

    if(argc < 2)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    key.sensor_id = atoi(argv[0]);
    key.submode = atoi(argv[1]);
    return (int32_t)InsMw_CfgMgr_ProfileSwitch(key);
}

static int32_t InsUT_ConfigMgr_SetName(int32_t argc, char **argv)
{
    InsMw_CfgProfileKey_t key;

    if(argc < 3)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    key.sensor_id = atoi(argv[0]);
    key.submode = atoi(argv[1]);
    return (int32_t)InsMw_CfgMgr_ProfileNameSet(key, argv[2]);
}

static int32_t InsUT_ConfigMgr_GetName(int32_t argc, char **argv)
{
    InsMw_CfgProfileKey_t key;
    char name[64] = {0};

    if(argc < 2)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    key.sensor_id = atoi(argv[0]);
    key.submode = atoi(argv[1]);
    if(INSERR_OK == InsMw_CfgMgr_ProfileNameGet(key, name))
    {
        INSLOG_I("Get name: %s", name);
    }
    return INSERR_OK;
}

static int32_t InsUT_ConfigMgr_Reset(int32_t argc, char **argv)
{
    (void)argc;
    (void)argv;
    return (int32_t)InsMw_CfgMgr_ProfileResetDefault();
}

static int32_t InsUT_ConfigMgr_Destroy(int32_t argc, char **argv)
{
    int32_t region = 0;

    if(argc < 1)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    region = atoi(argv[0]);
    return (int32_t)InsMw_CfgMgr_Destroy(region);
}

InsUT_Unittest_CmdInfo_t g_ut_cmd_config_mgr[INSUT_CMDCNT_CONFIG_MGR] =
{
    {InsUT_ConfigMgr_Init,     0, NULL, "init",        "No Param: Init config mgr"},
    {InsUT_ConfigMgr_Switch,   0, NULL, "switch",      "[sensor_id] [sub_mode]: Switch key"},
    {InsUT_ConfigMgr_SetName,  0, NULL, "set_name",    "[sensor_id] [sub_mode] [name]: Set name"},
    {InsUT_ConfigMgr_GetName,  0, NULL, "get_name",    "[sensor_id] [sub_mode]: Get name"},
    {InsUT_ConfigMgr_Reset,    0, NULL, "reset",       "No Param: Reset config mgr"},
    {InsUT_ConfigMgr_Destroy,  0, NULL, "destroy",     "[region]: Destroy config mgr data"},
};

static int32_t InsUT_ConfigCap_Init(int32_t argc, char **argv)
{
    (void)argc;
    (void)argv;
    return (int32_t)InsMw_CfgCAP_Init();
}

static int32_t InsUT_ConfigCap_GetVideoList(int32_t argc, char **argv)
{
    uint32_t list[64] = {0};
    uint32_t size = 0;
    uint32_t eis = 0;
    uint32_t ret = INSERR_OK;

    if(argc < 3)
    {
        INSLOG_W("Input para not enough!");
        return INSERR_NG;
    }
    if(strcmp(argv[0], "res") == 0)
    {
        if(strcmp(argv[1], "eis") == 0)
        {
            eis = atoi(argv[2]);
            INSLOG_I("Get video res list by eis %d", eis);
            ret = InsMw_CfgCAP_GetVideoResListByEis(list, &size, INSMW_CFG_CAM_SUBMODE_VID_TIMELAPSE, eis);
            if(ret != INSERR_OK)
            {
                INSLOG_E("Get video res list by eis failed!");
                return ret;
            }
            for(uint32_t i=0; i<size; i++)
            {
                INSLOG_I("Get video res %d", list[i]);
            }
        }
    }

    return (int32_t)ret;
}

InsUT_Unittest_CmdInfo_t g_ut_cmd_config_capability[INSUT_CMDCNT_CONFIG_CAPABILITY] =
{
    {InsUT_ConfigCap_Init,          0, NULL, "init",        "No Param: Init config capbility"},
    {InsUT_ConfigCap_GetVideoList,  0, NULL, "get_video",   "Get video config capbility"},
};

InsUT_Unittest_CmdInfo_t g_ut_cmd_config[INSUT_CMDCNT_CONFIG] =
{
    {NULL, INSUT_CMDCNT_CONFIG_MGR,  g_ut_cmd_config_mgr,  "mgr",        "config mgr test"},
    {NULL, INSUT_CMDCNT_CONFIG_BASE, g_ut_cmd_config_base, "base",       "config base test"},
    {NULL, INSUT_CMDCNT_CONFIG_CAPABILITY, g_ut_cmd_config_capability, "cap", "config capability test"}
};
