#include "config.h"

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

#include "../../../subprojects/inih_dep/ini.h"

config_t Config = {
#define SECTION(s, comment) .s = {
#define CFG_str(s, n, default, comment) .n = default,
#define CFG_bool(s, n, default, comment) .n = default,
#define CFG_int(s, n, default, comment) .n = default,
#define CFG_hex(s, n, precision, default, comment) .n = 0x##default,
#define CFG_ipv4(s, n, a, b, c, d, comment) .n = (a << 24) | (b << 16) | (c << 8) | d,
#define ENDSECTION(s) \
    }                 \
    ,
#include "config.def"
    ._keep_linter_happy = true
};

void fprintf_prefix(FILE *file, const char *prefix, const char *text) {
    char *copy = (char *)malloc(strlen(text) + 1);
    memcpy_s(copy, strlen(text) + 1, text, strlen(text) + 1);

    char *next_token;
    char *token = strtok_s(copy, "\n", &next_token);
    while (token != NULL) {
        fprintf(file, "%s%s\n", prefix, token);
        token = strtok_s(NULL, "\n", &next_token);
    }

    free(copy);
}

static void make_default_config() {
    FILE *config_file;
    fopen_s(&config_file, CONFIG_PATH, "w");
    if (config_file == NULL) {
        puts("Failed to create config file!");
        return;
    };
    int first_section = true;

#define CFG_str(s, n, default, comment)                                   \
    if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
    fprintf(config_file, "%s = %s\n", #n, default);

#define CFG_bool(s, n, default, comment)                                  \
    if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
    fprintf(config_file, "%s = %s\n", #n, default ? "true" : "false");

#define CFG_int(s, n, default, comment)                                   \
    if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
    fprintf(config_file, "%s = %d\n", #n, default);

#define CFG_hex(s, n, precision, default, comment)                                              \
    if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment);                       \
    fprintf(config_file, "%s = %.*X\n", #n, precision, 0x##default, precision, \
            precision == 1 ? "" : "s");

#define CFG_ipv4(s, n, a, b, c, d, comment)                               \
    if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
    fprintf(config_file, "%s = %hhu.%hhu.%hhu.%hhu\n", #n, a, b, c, d);

#define SECTION(s, comment)                                               \
    if (!first_section) fprintf(config_file, "\n");                       \
    first_section = false;                                                \
    if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment); \
    fprintf(config_file, "[%s]\n", #s);

#define COMMENT(comment) \
    if (strlen(comment) != 0) fprintf_prefix(config_file, "; ", comment);

#include "config.def"

    fclose(config_file);
}

static int handler(void *user, const char *section, const char *name, const char *value) {
    config_t *cfg = (config_t *)user;

    char *end;

    if (false)
        ;
#define CFG_str(s, n, default, comment) \
    else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) cfg->s.n = _strdup(value);
#define CFG_bool(s, n, default, comment)                                       \
    else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) cfg->s.n = \
        strcmp(value, "true") == 0;
#define CFG_int(s, n, default, comment)                                          \
    else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) {            \
        cfg->s.n = strtol(value, &end, 10);                                      \
        if (end == value || *end != '\0' || errno == ERANGE) cfg->s.n = default; \
    }
#define CFG_hex(s, n, precision, default, comment)                                   \
    else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) {                \
        cfg->s.n = strtol(value, &end, 16);                                          \
        if (end == value || *end != '\0' || errno == ERANGE) cfg->s.n = 0x##default; \
    }
#define CFG_ipv4(s, n, a, b, c, d, comment)                                          \
    else if (_stricmp(section, #s) == 0 && _stricmp(name, #n) == 0) {                \
        cfg->s.n = strtol(value, &end, 16);                                          \
        unsigned char ip_a, ip_b, ip_c, ip_d;                                        \
        if (sscanf_s(value, "%hhu.%hhu.%hhu.%hhu", &ip_a, &ip_b, &ip_c, &ip_d) == 4) \
            cfg->s.n = (ip_a << 24) | (ip_b << 16) | (ip_c << 8) | ip_d;             \
        else                                                                         \
            cfg->s.n = (a << 24) | (b << 16) | (c << 8) | d;                         \
    }

#include "config.def"

    return 1;
}

void load_micemaster_config() {
    if (ini_parse(CONFIG_PATH, handler, &Config) < 0) {
        make_default_config();
        printf("Can't load '%s', using defaults\n", CONFIG_PATH);
    }
}
