#undef __STRICT_ANSI__

#include "ntp.h"

#include <qthing.h>

#include <freertos/FreeRTOS.h>
#include "freertos/task.h"
#include "apps/sntp/sntp.h"
#include <time.h>
#include <stdlib.h>
#include "esp_log.h"


typedef struct {
    bool started;
    std::string address;
    qthing::ntp_callback_t callback;
} ntpd_parameter_t;

static ntpd_parameter_t parameter;

void ntp_task(void *ignored) {
    time_t now = 0;
    struct tm timeinfo = { 0 };

    while (get_wlan_connection_status() != connected) vTaskDelay(100 / portTICK_PERIOD_MS);

    sntp_setoperatingmode(SNTP_OPMODE_POLL);
    sntp_setservername(0, (char *)parameter.address.c_str());
    sntp_init();

    do {
        vTaskDelay(100 / portTICK_PERIOD_MS);
        time(&now);
        localtime_r(&now, &timeinfo);
    } while(timeinfo.tm_year < 100);

    if (parameter.callback != NULL) parameter.callback();

    vTaskDelete(NULL);
}

namespace qthing {

    void enable_ntp(std::string address, std::string timezone, qthing::ntp_callback_t callback) {
        if (parameter.started) {
            ESP_LOGE("NTP", "service already enabled");
            return;
        }

        parameter = {true, address, callback};

        setenv("TZ", (char *)timezone.c_str(), 1);
        tzset();

        xTaskCreate(ntp_task, "ntp", 10000, NULL, 1, NULL);
    }
}