#include "configreader.h"
#include <libconfig.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

config_t cfg;
const config_setting_t *pins;
const config_setting_t *compareIDs;

void cfginit(void) {
    
    config_init(&cfg);
    if (!config_read_file(&cfg, "/etc/humiditypi/settings.cfg")) {
        fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
        config_destroy(&cfg);
        //return(EXIT_FAILURE);
    }
}

void cfgdestroy(void) {
    //free ram
    config_destroy(&cfg);
}

void cfgreinit(void) {
    cfgdestroy();
    cfginit();
}

int cfgreadpinnumber(int element) {
    pins = config_lookup(&cfg, "pins");
    int length = config_setting_length(pins);
    if (element < length) {
        return config_setting_get_int_elem(pins, element);
    } else {
        printf("No pins configuration item found.\n");
        return 7;
    }
    
}

int cfgreadpinamount(void) {
    pins = config_lookup(&cfg, "pins");
    return config_setting_length(pins);
} 

int cfgreadsensornodes(sensornode *nodes, int nodecount) {
    sensornode *currentNode = nodes;
    
    config_setting_t *sensornodes = config_lookup(&cfg, "sensornodes");
    if (sensornodes != NULL) {
        int count = nodecount = config_setting_length(sensornodes); //Read amount of sensornodes and save it in nodecount
        int i;
        
        for(i = 0; i < count; ++i) {
            config_setting_t *sensornodesetting = config_setting_get_elem(sensornodes, i);
            
            /* Only output the record if all of the expected fields are present. */
            const char *ip;
            int outside;
            if (!(config_setting_lookup_string(sensornodesetting, "ip", &ip) && config_setting_lookup_int(sensornodesetting, "outside", &outside)))
                continue;
            
            strcpy(currentNode->ip, ip);
            currentNode->isoutside = outside;
            //if (i < count - 1) { //stop at last item
                currentNode->next = malloc(sizeof(sensornode));
                currentNode = currentNode->next;
            //}
            
        }
        return 1;
        
    }
    return 0;
}

int cfgreadcompareidamount(void) {
    compareIDs = config_lookup(&cfg, "compare_ids");
    return config_setting_length(compareIDs);
}

int cfgreadcompareidvalue(int element) {
    compareIDs = config_lookup(&cfg, "compare_ids");
    int length = config_setting_length(compareIDs);
    if (element < length) {
        return config_setting_get_int_elem(compareIDs, element);
    } else {
        printf("No compare configuration item found.\n");
        return -1;
    }
}

const char *cfgreadDatabaseUser(void) {
    const char *databaseuser;
    if (!config_lookup_string(&cfg, "databaseuser", &databaseuser)) {
        databaseuser = "dhtuser";
    }
    return databaseuser;
}
    
int cfgreadNodeRefreshRate(void) {
    int refreshRate;
    if (!config_lookup_int(&cfg, "refreshRate", &refreshRate)) {
            refreshRate = 60; //Default value
    }
    return refreshRate;
}
