//
// Set telnet pw
//

#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <esp_wifi.h>
#include <settings.h>

#include "console/cmd_common.h"
#include <console/cmddef.h>
#include <application.h>
#include <console/prefix_match.h>

static int cmd_clear(console_ctx_t *ctx, cmd_signature_t *reg)
{
    EMPTY_CMD_SETUP("Clear access password");
    console_printf("Access password cleared.\n");
    g_Settings.console_pw[0] = 0;
    settings_persist(SETTINGS_console_pw);
    return 0;
}

static int cmd_set(console_ctx_t *ctx, cmd_signature_t *reg)
{
    static struct {
        struct arg_str *pw;
        struct arg_end *end;
    } args;

    if (reg) {
        args.pw = arg_str1(NULL, NULL, "<password>", EXPENDABLE_STRING("New password"));
        args.end = arg_end(1);

        reg->argtable = &args;
        reg->help = EXPENDABLE_STRING("Set access password");
        return CONSOLE_OK;
    }

    strncpy(g_Settings.console_pw, args.pw->sval[0], CONSOLE_PW_LEN-1);
    console_printf("Access pw set to: \"%.*s\"\n", CONSOLE_PW_LEN, args.pw->sval[0]);
    settings_persist(SETTINGS_console_pw);
    return 0;
}

void register_cmd_pw(void)
{
    console_group_add("pw", "Access password");

    console_cmd_register(cmd_set, "pw set");
    console_cmd_register(cmd_clear, "pw clear");
}