#include <string.h>
#include <ctype.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_err.h"

#include "board_config.h"

static const char *TAG = "board_config";

board_config_t board_config;

bool atob(const char *value)
{
    return value[0] == 'y';
}

#define SET_CONFIG_VALUE(name, prop, convert_fn) \
    if (!strcmp(key, name))                      \
    {                                            \
        board_config.prop = convert_fn(value);   \
        continue;                                \
    }

#define SET_CONFIG_VALUE_STR(name, prop)  \
    if (!strcmp(key, name))               \
    {                                     \
        strcpy(board_config.prop, value); \
        continue;                         \
    }

void board_config_load()
{
    memset(&board_config, 0, sizeof(board_config_t));

    FILE *file = fopen("/cfg/board.cfg", "r");
    if (file == NULL)
    {
        ESP_ERROR_CHECK(ESP_ERR_NOT_FOUND);
    }

    char buffer[256];

    while (fgets(buffer, 256, file))
    {
        int buf_length = strlen(buffer);
        int buf_start = 0;
        while (buf_start < buf_length && isspace((unsigned char)buffer[buf_start]))
        {
            buf_start++;
        }
        int buf_end = buf_length;
        while (buf_end > 0 && !isgraph((unsigned char)buffer[buf_end - 1]))
        {
            buf_end--;
        }

        buffer[buf_end] = '\0';
        char *line = &buffer[buf_start];

        if (line[0] != '#')
        {
            char *saveptr;
            char *key = strtok_r(line, "=", &saveptr);
            if (key != NULL)
            {
                char *value = strtok_r(NULL, "=", &saveptr);
                if (value != NULL)
                {
                    SET_CONFIG_VALUE_STR("DEVICE_NAME", device_name);
                    SET_CONFIG_VALUE("led_blue", led_blue, atob);
                    SET_CONFIG_VALUE("led_blue_GPIO", led_blue_gpio, atoi);
                    SET_CONFIG_VALUE("led_red", led_red, atob);
                    SET_CONFIG_VALUE("led_red_GPIO", led_red_gpio, atoi);
                    SET_CONFIG_VALUE("led_green", led_green, atob);
                    SET_CONFIG_VALUE("led_green_GPIO", led_green_gpio, atoi);
                    SET_CONFIG_VALUE("BUZZER", buzzer, atob);
                    SET_CONFIG_VALUE("BUZZER_GPIO", buzzer_gpio, atoi);
                    SET_CONFIG_VALUE("BUTTON_WIFI_GPIO", button_wifi_gpio, atoi);
                    SET_CONFIG_VALUE("PILOT_PWM_GPIO", pilot_pwm_gpio, atoi);
                    SET_CONFIG_VALUE("PILOT_ADC_CHANNEL", pilot_adc_channel, atoi);
                    SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_12", pilot_down_threshold_12, atoi);
                    SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_9", pilot_down_threshold_9, atoi);
                    SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_6", pilot_down_threshold_6, atoi);
                    SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_3", pilot_down_threshold_3, atoi);
                    SET_CONFIG_VALUE("PILOT_DOWN_THRESHOLD_N12", pilot_down_threshold_n12, atoi);
                    SET_CONFIG_VALUE("PROXIMITY", proximity, atob);
                    SET_CONFIG_VALUE("PROXIMITY_ADC_CHANNEL", proximity_adc_channel, atoi);
                    SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_8", proximity_down_threshold_8, atoi);
                    SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_10", proximity_down_threshold_10, atoi);
                    SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_13", proximity_down_threshold_13, atoi);
                    SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_20", proximity_down_threshold_20, atoi);
                    SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_25", proximity_down_threshold_25, atoi);
                    SET_CONFIG_VALUE("PROXIMITY_DOWN_THRESHOLD_32", proximity_down_threshold_32, atoi);
                    SET_CONFIG_VALUE("AC_RELAY_GPIO", ac_relay_gpio, atoi);
                    SET_CONFIG_VALUE("SOCKET_LOCK", socket_lock, atob);
                    SET_CONFIG_VALUE("SOCKET_LOCK_A_GPIO", socket_lock_a_gpio, atoi);
                    SET_CONFIG_VALUE("SOCKET_LOCK_B_GPIO", socket_lock_b_gpio, atoi);
                    SET_CONFIG_VALUE("SOCKET_LOCK_DETECTION_GPIO", socket_lock_detection_gpio, atoi);
                    SET_CONFIG_VALUE("SOCKET_LOCK_DETECTION_DELAY", socket_lock_detection_delay, atoi);
                    SET_CONFIG_VALUE("SOCKET_LOCK_MIN_BREAK_TIME", socket_lock_min_break_time, atoi);
                    SET_CONFIG_VALUE("RCM", rcm, atob);
                    SET_CONFIG_VALUE("RCM_GPIO", rcm_gpio, atoi);
                    SET_CONFIG_VALUE("RCM_TEST_GPIO", rcm_test_gpio, atoi);
                }
            }
        }
    }

    fclose(file);
}
