#include <cmdHelper.h>
#include <logger.h>

#include <eeprom.h>
#include <config.h>


static bool showConfigCmd(uint8_t argc, char **args) {
    bool retCode = true;

    t_configBlock configBlock;
    eepromReadConfigBlock(&configBlock);
    sendFormatString("configMagic:         %lx\n\r", configBlock.configMagic);
    sendFormatString("deviceName:          %s\n\r", configBlock.deviceName);
    sendFormatString("MAC address:         %02x:%02x:%02x:%02x:%02x:%02x\n\r", configBlock.macAddress[0], 
                                                                           configBlock.macAddress[1], 
                                                                           configBlock.macAddress[2], 
                                                                           configBlock.macAddress[3], 
                                                                           configBlock.macAddress[4], 
                                                                           configBlock.macAddress[5]);
    sendFormatString("frontend threshold:  %ld\n\r", configBlock.frontendThreshold);
    sendFormatString("broker:              %s\n\r", configBlock.brokerName);
    sendFormatString("watchdogTopic:       %s\n\r", configBlock.watchdogTopic);
    sendFormatString("startupTopic:        %s\n\r", configBlock.startupTopic);
    sendFormatString("statusTopic:         %s\n\r", configBlock.statusTopic);
    sendFormatString("mbusDataTopic:       %s\n\r", configBlock.mbusDataTopic);
    sendFormatString("syslog server:       %s\n\r", configBlock.syslogServerName);
    sendFormatString("device block cnt:    %d\n\r", configBlock.numOfDeviceBlocks);

    for (uint8_t i = 0; i < configBlock.numOfDeviceBlocks; i++) {
        t_deviceBlock tmpDeviceBlock;
        eepromReadDeviceBlock(i, &tmpDeviceBlock);
        if (tmpDeviceBlock.deviceMagic == DEVICE_MAGIC) {
            sendFormatString("device %d: \n\r", i);
            sendFormatString("  Name: %s, Address: %d, Period: %d\n\r", 
                       tmpDeviceBlock.deviceName, tmpDeviceBlock.address, tmpDeviceBlock.period);
            sendFormatString("  Considered Fields: %d %d %d %d\n\r",
                       tmpDeviceBlock.consideredField[0],
                       tmpDeviceBlock.consideredField[1],
                       tmpDeviceBlock.consideredField[2],
                       tmpDeviceBlock.consideredField[3]);
            if (tmpDeviceBlock.deviceMagic != DEVICE_MAGIC) {
                sendString("  DEVICE MAGIC DOES NOT MATCH\n\r");
            }
        }
    }

    return retCode;
}

static bool set(uint8_t argc, char **args) {
    bool retCode = false;

    if (argc >= 1) {
        if (0 == strcmp("?", args[0])) {
            sendString("You can set the following parameters:\n\r");
            retCode = true;
        }
    }

    return retCode;
}

static bool restartCmd(uint8_t argc, char **args) {
    HAL_NVIC_SystemReset();
    // you won't come here ...
    return true;
}



const cmd_t CONFIG_COMMANDS[] = {
    { .name = "show", .cmdFunc = showConfigCmd,
     .help = \
        "show ................................. Show the configuration\n\r"
    },
    { .name = "restart", .cmdFunc = restartCmd,
      .help = \
        "restart .............................. Restart the system,\n\r" \
        "                                       Required to reload config\n\r"
    },
    { .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
};



const cmd_t *getConfigCommands() {
    return CONFIG_COMMANDS;
}