#include "lib_shell.h"
#include "shell/sh.h"
#include "shell/sh_vset.h"
#include "spbelib_interface.h"
#include "config/app_config.h"

#include <string.h>

#undef CONFIG_SYS_LOG_LEVEL
#define CONFIG_SYS_LOG_LEVEL SYS_LOG_LEVEL_INF
#include "sys_log.h"

static void _print_event(void);
SH_CMD_FN(_set_adapter_fcport_off);  // set adapter fcport-off
SH_CMD_FN(_set_adapter_fcport_uart); // set adapter fcport-uart
SH_CMD_FN(_set_adapter_fcport_otg);  // set adapter fcport-otg
SH_CMD_FN(_set_adapter_rf_off);      // set adapter rf-off
SH_CMD_FN(_set_adapter_rf_ble);      // set adapter rf-ble
SH_CMD_FN(_set_adapter_rf_ap);       // set adapter rf-ap
SH_CMD_FN(_set_adapter_rf_sta);      // set adapter rf-sta
SH_CMD_FN(_set_ble_name);            // set ble name <...>
SH_CMD_FN(_set_ble_passwd);          // set ble passwd <...>
SH_CMD_FN(_set_wifi_ap_ssid);        // set wifi ap-ssid <...>
SH_CMD_FN(_set_wifi_ap_passwd);      // set wifi ap-passwd <...>
SH_CMD_FN(_set_wifi_sta_ssid);       // set wifi sta-ssid <...>
SH_CMD_FN(_set_wifi_sta_passwd);     // set wifi sta-passwd <...>
SH_CMD_FN(_get_adapter_rf_status);   // get adapter adapter rf-status
SH_CMD_FN(_get_ble_name);            // get ble name
SH_CMD_FN(_get_ble_passwd);          // get ble passwd
SH_CMD_FN(_get_wifi_ap_ssid);        // get wifi ap-ssid
SH_CMD_FN(_get_wifi_ap_passwd);      // get wifi ap-passwd
SH_CMD_FN(_get_wifi_sta_ssid);       // get wifi sta-ssid
SH_CMD_FN(_get_wifi_sta_passwd);     // get wifi sta-passwd

SH_DEF_SUB_CMD(
    _sub_set_adapter,
    SH_SETUP_CMD("fcport-off", "关闭飞控数据接口", _set_adapter_fcport_off, NULL),     // set adapter fcport-off
    SH_SETUP_CMD("fcport-uart", "使用 UART 连接飞控", _set_adapter_fcport_uart, NULL), // set adapter fcport-uart
    SH_SETUP_CMD("fcport-otg", "使用 OTG 连接飞控", _set_adapter_fcport_otg, NULL),    // set adapter fcport-otg

    SH_SETUP_CMD("rf-off", "关闭无线功能", _set_adapter_rf_off, NULL),  // set adapter rf-off
    SH_SETUP_CMD("rf-ble", "打开蓝牙功能", _set_adapter_rf_ble, NULL),  // set adapter rf-ble
    SH_SETUP_CMD("rf-ap", "打开 AP 功能", _set_adapter_rf_ap, NULL),    // set adapter rf-ap
    SH_SETUP_CMD("rf-sta", "打开 STA 功能", _set_adapter_rf_sta, NULL), // set adapter rf-sta
);

SH_DEF_SUB_CMD(
    _sub_set_opt,
    SH_SETUP_CMD("adapter", "设置小黄砖功能", NULL, _sub_set_adapter), // set adapter <...>
);

SH_DEF_SUB_CMD(
    _sub_set_ble,
    SH_SETUP_CMD("name", "设置 BLE 设备名， 1 到 19 个字符", _set_ble_name, NULL),       // set ble name <...>
    SH_SETUP_CMD("passwd", "设置 BLE 透传密码， 0 到 12 个字符", _set_ble_passwd, NULL), // set ble passwd <...>
);
SH_DEF_SUB_CMD(
    _sub_set_wifi,
    SH_SETUP_CMD("ap-ssid", "参数", _set_wifi_ap_ssid, NULL),       // set wifi ap-ssid <...>
    SH_SETUP_CMD("ap-passwd", "参数", _set_wifi_ap_passwd, NULL),   // set wifi ap-passwd <...>
    SH_SETUP_CMD("sta-ssid", "参数", _set_wifi_sta_ssid, NULL),     // set wifi sta-ssid <...>
    SH_SETUP_CMD("sta-passwd", "参数", _set_wifi_sta_passwd, NULL), // set wifi sta-passwd <...>
);

SH_DEF_SUB_CMD(
    _sub_set_param,
    SH_SETUP_CMD("ble", "参数", NULL, _sub_set_ble),         // set ble <...>
    SH_SETUP_CMD("wifi", "参数", NULL, _sub_set_wifi),       // set wifi <...>
);

SH_DEF_SUB_CMD(
    _sub_get_adapter,
    SH_SETUP_CMD("rf-status", "查询小黄砖射频状态", _get_adapter_rf_status, NULL), // get adapter rf-status
);

SH_DEF_SUB_CMD(
    _sub_get_ble,
    SH_SETUP_CMD("name", "打印 BLE 设备名", _get_ble_name, NULL),       // get ble name
    SH_SETUP_CMD("passwd", "打印 BLE 透传密码", _get_ble_passwd, NULL), // get ble passwd
);

SH_DEF_SUB_CMD(
    _sub_get_wifi,
    SH_SETUP_CMD("ap-ssid", "参数", _get_wifi_ap_ssid, NULL),       // get wifi ap-ssid
    SH_SETUP_CMD("ap-passwd", "参数", _get_wifi_ap_passwd, NULL),   // get wifi ap-passwd
    SH_SETUP_CMD("sta-ssid", "参数", _get_wifi_sta_ssid, NULL),     // get wifi sta-ssid
    SH_SETUP_CMD("sta-passwd", "参数", _get_wifi_sta_passwd, NULL), // get wifi sta-passwd
);

SH_DEF_SUB_CMD(
    _sub_get,
    SH_SETUP_CMD("adapter", "打印当前射频状态", NULL, _sub_get_adapter), // get adapter <...>
    SH_SETUP_CMD("ble", "参数", NULL, _sub_get_ble),                     // get ble <...>
    SH_SETUP_CMD("wifi", "参数", NULL, _sub_get_wifi),                   // get wifi <...>
);

SH_DEF_CMD(
    _register_cmd,
    SH_SETUP_CMD("set", "执行控制动作", NULL, _sub_set_opt),   // set <...>
    SH_SETUP_CMD("set", "执行参数设置", NULL, _sub_set_param), // set <...>
    SH_SETUP_CMD("get", "获取状态", NULL, _sub_get),           // get <...>
);

void lib_shell_register(void)
{
    SYS_LOG_INF("Register notify event");
    sblib_regist_notify_event(_print_event);
    _print_event();
    sh_register_cmd(&_register_cmd);
}

SH_CMD_FN(_set_adapter_fcport_off)
{
    return sblib_control(LIB_CONTROL_FCPORT_OFF);
}
SH_CMD_FN(_set_adapter_fcport_uart)
{
    return sblib_control(LIB_CONTROL_FCPORT_UART);
}
SH_CMD_FN(_set_adapter_fcport_otg)
{
    return sblib_control(LIB_CONTROL_FCPORT_OTG);
}

SH_CMD_FN(_set_adapter_rf_off)
{
    return sblib_control(LIB_CONTROL_RF_OFF);
}
SH_CMD_FN(_set_adapter_rf_ble)
{
    return sblib_control(LIB_CONTROL_RF_BLE);
}
SH_CMD_FN(_set_adapter_rf_ap)
{
    return sblib_control(LIB_CONTROL_RF_AP);
}
SH_CMD_FN(_set_adapter_rf_sta)
{
    return sblib_control(LIB_CONTROL_RF_STA);
}

SH_CMD_FN(_set_ble_name)
{
    int len = 0;
    int ret = -1;
    if (argc > 0)
    {
        len = strlen(argv[0]);
    }

    ret = app_cfg_set_device_name_ble(argv[0], len);
    if (ret == 0)
    {
        sblib_send_event(LIB_EVENT_BLE_NAME_SET, 0, NULL);
    }
    return ret;
}

SH_CMD_FN(_set_ble_passwd)
{
    int len = 0;
    int ret = -1;
    if (argc > 0)
    {
        len = strlen(argv[0]);
    }

    ret = app_cfg_set_psPassword(argv[0], len);
    if (ret == 0)
    {
        sblib_send_event(LIB_EVENT_BLE_PASSWD_SET, 0, NULL);
    }
    return ret;
}

SH_CMD_FN(_set_wifi_ap_ssid)
{
    int ret = app_cfg_set_wifi_ap_ssid(argv[0]);
    if (ret == 0)
    {
        sblib_send_event(LIB_EVENT_WIFI_AP_SSID_SET, 0, NULL);
    }
    return ret;
}

SH_CMD_FN(_set_wifi_ap_passwd)
{
    int ret = app_cfg_set_wifi_ap_password(argv[0]);
    if (ret == 0)
    {
        sblib_send_event(LIB_EVENT_WIFI_AP_PASSWD_SET, 0, NULL);
    }
    return ret;
}

SH_CMD_FN(_set_wifi_sta_ssid)
{
    int ret = app_cfg_set_wifi_sta_ssid(argv[0]);
    if (ret == 0)
    {
        sblib_send_event(LIB_EVENT_WIFI_STA_SSID_SET, 0, NULL);
    }
    return ret;
}

SH_CMD_FN(_set_wifi_sta_passwd)
{
    int ret = app_cfg_set_wifi_sta_password(argv[0]);
    if (ret == 0)
    {
        sblib_send_event(LIB_EVENT_WIFI_STA_PASSWD_SET, 0, NULL);
    }
    return ret;
}

SH_CMD_FN(_get_adapter_rf_status)
{
    static const char *const rf_mode_tab[] = {
        [DATA_BRIDGE_RF_MODE_OFF] = "无线已关闭",
        [DATA_BRIDGE_RF_MODE_BLE] = "蓝牙模式",
        [DATA_BRIDGE_RF_MODE_WIFI_AP] = "AP 模式",
        [DATA_BRIDGE_RF_MODE_WIFI_STA] = "STA 模式",
    };
    sh_echo(sh_hdl, "当前射频状态：%s\r\n", rf_mode_tab[g_cfg_app->rf_mode]);
    return 0;
}

SH_CMD_FN(_get_ble_name)
{
    char ble_name[__ARRAY_SIZE(g_cfg_app->device_name_ble) + 1] = {0};
    memcpy(ble_name, g_cfg_app->device_name_ble, sizeof(g_cfg_app->device_name_ble));
    sh_echo(sh_hdl, "%s\r\n", ble_name);
    return 0;
}
SH_CMD_FN(_get_ble_passwd)
{
    char passwd[__ARRAY_SIZE(g_cfg_app->psPassword) + 1] = {0};
    memcpy(passwd, g_cfg_app->psPassword, sizeof(g_cfg_app->psPassword));
    sh_echo(sh_hdl, "%s\r\n", passwd);
    return 0;
}

SH_CMD_FN(_get_wifi_ap_ssid)
{
    sh_echo(sh_hdl, "%s\r\n", g_cfg_app->app_config_wifi_para.wifi_ap_ssid);
    return 0;
}
SH_CMD_FN(_get_wifi_ap_passwd)
{
    sh_echo(sh_hdl, "%s\r\n", g_cfg_app->app_config_wifi_para.wifi_ap_password);
    return 0;
}
SH_CMD_FN(_get_wifi_sta_ssid)
{
    sh_echo(sh_hdl, "%s\r\n", g_cfg_app->app_config_wifi_para.wifi_sta_ssid);
    return 0;
}
SH_CMD_FN(_get_wifi_sta_passwd)
{
    sh_echo(sh_hdl, "%s\r\n", g_cfg_app->app_config_wifi_para.wifi_sta_password);
    return 0;
}

static void _print_event(void)
{
    lib_event_t event;

    do
    {
        static const char *const type_tab[] = {
            [LIB_EVENT_NONE] = "未新事件",

            [LIB_EVENT_FC_PORT_OFF] = "飞控数据接口关闭",
            [LIB_EVENT_FC_PORT_UART] = "飞控数据接口为 UART",
            [LIB_EVENT_FC_PORT_OTG] = "飞控数据接口为 OTG",

            [LIB_EVENT_USBH_NO_START] = "USBH 接口未启动",
            [LIB_EVENT_USBH_CDC] = "USBH 接口已连接 CDC 设备",
            [LIB_EVENT_USBH_DFU] = "USBH 接口已连接 DFU 设备",
            [LIB_EVENT_USBH_MSC] = "USBH 接口已连接 MSC 设备",
            [LIB_EVENT_USBH_UNKNOW_DEVICE] = "USBH 接口已连接未知设备",

            [LIB_EVENT_RF_OFF] = "射频已关闭",
            [LIB_EVENT_RF_CHANGE_MODE] = "已准备好切换模式",
            [LIB_EVENT_RF_BLE_IDLE] = "当前为 BLE 模式，未有设备连接",
            [LIB_EVENT_RF_BLE_CONNECTED] = "当前为 BLE 模式，已连接",
            [LIB_EVENT_RF_AP_IDLE] = "当前为 AP 模式，未有设备连接",
            [LIB_EVENT_RF_AP_CONNECTED] = "当前为 AP 模式，已连接",
            [LIB_EVENT_RF_STA_IDLE] = "当前为 STA 模式，未连接到 AP",
            [LIB_EVENT_RF_STA_CONNECTED] = "当前为 STA 模式，已连接",
            [LIB_EVENT_BLE_NAME_SET] = "BLE 名称已设置",
            [LIB_EVENT_BLE_PASSWD_SET] = "BLE 密码已设置",
            [LIB_EVENT_WIFI_AP_SSID_SET] = "WIFI AP SSID 已设置",
            [LIB_EVENT_WIFI_AP_PASSWD_SET] = "WIFI AP 密码已设置",
            [LIB_EVENT_WIFI_STA_SSID_SET] = "WIFI STA SSID 已设置",
            [LIB_EVENT_WIFI_STA_PASSWD_SET] = "WIFI STA 密码已设置",
        };
        event = sblib_get_event();
        if (event.type != LIB_EVENT_NONE)
        {
            if (event.type < __ARRAY_SIZE(type_tab) && type_tab[event.type] != NULL)
            {
                SYS_LOG_INF("******** Event: %s ********", type_tab[event.type]);
            }
            else
            {
                SYS_LOG_WRN("******** Unknown event: %d ********", event.type);
            }

            if (event.len > 0)
            {
                SYS_LOG_DUMP(event.data, event.len, 0, 0);
            }
        }

    } while (event.type != LIB_EVENT_NONE);
}
