#include <stdlib.h>
#include <string.h>
#include "command/command.h"
#include "wm_include.h"

extern int shell_printf(const char *format, ...);

int _task_list(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
  tls_os_disp_task_stat_info();
  return 0;
}

int _reset(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
  tls_sys_reset();
  return 0;
}

int _soft_ap(const shell_cmd_t *pcmd, int argc, char *const argv[])
{
  if(argc > 1)
  {
    if(strcmp(argv[1], "state") == 0)
    {
      shell_printf("SOFT AP state : %u\n", tls_wifi_softap_get_state());
    }
    else if(strcmp(argv[1], "create") == 0)
    {
      struct tls_softap_info_t ap_info;
      struct tls_ip_info_t ip_info;

      tls_wifi_disconnect();

      strncpy((char *)ap_info.ssid, argv[2], 32);
      ap_info.ssid[32] = '\0';
      ap_info.encrypt = IEEE80211_ENCRYT_TKIP_WPA2;
      ap_info.channel = 5;

      ap_info.keyinfo.format = 1;
      ap_info.keyinfo.index = 1;
      ap_info.keyinfo.key_len = strlen(argv[3]);
      strncpy((char *)ap_info.keyinfo.key, argv[3], 63);

      ip_info.ip_addr[0] = 192;
      ip_info.ip_addr[1] = 168;
      ip_info.ip_addr[2] = 1;
      ip_info.ip_addr[3] = 1;
      ip_info.netmask[0] = 255;
      ip_info.netmask[1] = 255;
      ip_info.netmask[2] = 255;
      ip_info.netmask[3] = 0;
      ip_info.dnsname[0] = '\0';
      
      int result = tls_wifi_softap_create(&ap_info, &ip_info);

      shell_printf("Create AP with SSID : %s, key(%d) : %s -> %d\n", ap_info.ssid, ap_info.keyinfo.key_len, ap_info.keyinfo.key, result);
    }
  }
  else
  {
    shell_printf("Unknown SOFT AP command\n");
  }
  return 0;
}

NANO_SHELL_ADD_CMD(task_list,
                   _task_list,
                   "List all tasks",
                   "  Use this command to list all defined tasks\r\n");
NANO_SHELL_ADD_CMD(reset,
                   _reset,
                   "Reset the system",
                   "  Use this command reset the system\r\n");
NANO_SHELL_ADD_CMD(soft_ap,
                   _soft_ap,
                   "Command to control SOFT AP",
                   "  Use this command to control the SOFT AP subsystem\r\n");
