#include <stdlib.h>
#include <string.h>

#include <helpers.h>
#include <config.h>
#include <logger.h>
#include <confini.h>

#define CONFINI_IS_KEY(SECTION, KEY) \
    (ini_array_match(SECTION, disp->append_to, '.', disp->format) && \
     ini_string_match_ii(KEY, disp->data, disp->format))

int
helper_load_config(IniDispatch *disp, void *config_void)
{
    config_t *config = (config_t*)config_void;
    char relay_section_name[10]; // "relay-255\0" is longest name 

    if(disp->type == INI_KEY)
    {
        if(CONFINI_IS_KEY("controller", "name"))
        {
            strncpy(config->name, disp->value, MAX_NAME_LENGTH);
            config->name[MAX_NAME_LENGTH] = '\0';
            return 0;
        }
        if(CONFINI_IS_KEY("controller", "database"))
        {
            strcpy(config->database, disp->value);
            return 0;
        }
        if(CONFINI_IS_KEY("controller", "log-level"))
        {
            if(strcasecmp(disp->value, "trace") == 0)
            {
                config->log_level = LOG_LEVEL_TRACE;
                return 0;
            }
            if(strcasecmp(disp->value, "debug") == 0)
            {
                config->log_level = LOG_LEVEL_DEBUG;
                return 0;
            }
            if(strcasecmp(disp->value, "info") == 0)
            {
                config->log_level = LOG_LEVEL_INFO;
                return 0;
            }
            if(strcasecmp(disp->value, "warn") == 0)
            {
                config->log_level = LOG_LEVEL_WARN;
                return 0;
            }
            if(strcasecmp(disp->value, "error") == 0)
            {
                config->log_level = LOG_LEVEL_ERROR;
                return 0;
            }
            if(strcasecmp(disp->value, "fatal") == 0)
            {
                config->log_level = LOG_LEVEL_FATAL;
                return 0;
            }
            LOG_WARN("invalid log-level '%s'\n", disp->value);
            return 0;
        }
        if(CONFINI_IS_KEY("controller", "discovery-port"))
        {
            config->discovery_port = atoi(disp->value);
            return 0;
        }
        if(CONFINI_IS_KEY("controller", "mqtt-port"))
        {
            config->mqtt_port = atoi(disp->value);
            return 0;
        }
        if(CONFINI_IS_KEY("controller", "mqtt-host"))
        {
            strcpy(config->mqtt_host, disp->value);
            return 0;
        }
        if(CONFINI_IS_KEY("controller", "relay-count"))
        {
            config->relay_count = atoi(disp->value);
            config->relay_configs = malloc(sizeof(config_relay_t) * config->relay_count);
            for(uint8_t i = 0; i < config->relay_count; ++i)
            {
                config->relay_configs[i].driver = RELAY_DRIVER_NONE;
                config->relay_configs[i].inverted = 0;
                config->relay_configs[i].pin = 0;
                config->relay_configs[i].pulse_duration = 0;
            }
            LOG_TRACE("config relay-count set to %u\n", config->relay_count);
            return 0;
        }
        for(uint8_t i = 0; i < config->relay_count; ++i)
        {
            sprintf(relay_section_name, "relay-%u", i);
            if(CONFINI_IS_KEY(relay_section_name, "pin"))
            {
                config->relay_configs[i].pin = atoi(disp->value);
                return 0;
            }
            if(CONFINI_IS_KEY(relay_section_name, "inverted"))
            {
                config->relay_configs[i].inverted = atoi(disp->value);
                return 0;
            }
            if(CONFINI_IS_KEY(relay_section_name, "pulse-duration"))
            {
                config->relay_configs[i].pulse_duration = atoi(disp->value);
                return 0;
            }
            if(CONFINI_IS_KEY(relay_section_name, "driver"))
            {
                if(strcasecmp(disp->value, "gpio") == 0)
                {
                    config->relay_configs[i].driver = RELAY_DRIVER_GPIO;
                    return 0;
                }
                if(strcasecmp(disp->value, "piface") == 0)
                {
                    config->relay_configs[i].driver = RELAY_DRIVER_PIFACE;
                    return 0;
                }
                LOG_WARN("invalid driver '%s' in section '%s'\n", disp->value, relay_section_name);
                return 0;
            }
        }
    }
    return 0;
}