#include "esp_log.h"
#include "obtain_time.h"
#include <time.h>
#ifdef CONFIG_IDF_TARGET_LINUX
#else
#include "esp_netif_sntp.h"
#include "esp_sntp.h"
#endif

static const char *TAG = "sntp";

char *days[7]={"Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi"};
char *months[12]={"Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"};

#ifdef CONFIG_IDF_TARGET_LINUX
void updateTime(void *pvParameter)
{
    while (1)
    {
        char strftime_buf[64];

        // Set timezone to Eastern Standard Time and print local time
        time_t now;
        struct tm timeinfo;
        time(&now);
        localtime_r(&now, &timeinfo);
        // strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
       snprintf(strftime_buf,sizeof(strftime_buf), "%s %d %s %02d:%02d", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon], timeinfo.tm_hour, timeinfo.tm_min);
        localtime_r(&now, &timeinfo);
        ESP_LOGE(TAG, "%s", strftime_buf);
        send_event(EVT_TIME_SETTED, &timeinfo);
        vTaskDelay(10000 / portTICK_PERIOD_MS);
    }
}
#else
void time_sync_notification_cb(struct timeval *tv)
{
    ESP_LOGI(TAG, "Notification of a time synchronization event");
    char strftime_buf[64];

    // Set timezone to Eastern Standard Time and print local time
    time_t now;
    struct tm timeinfo;
    time(&now);
    localtime_r(&now, &timeinfo);
    // strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
    sprintf(strftime_buf, "%s %d %s %02d:%02d", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon], timeinfo.tm_hour, timeinfo.tm_min);
    localtime_r(&now, &timeinfo);
    send_event(EVT_TIME_SETTED, &strftime_buf);
}

void updateTime(void *pvParameter)
{

    // Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
    // number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above)
    EventBits_t bits = xEventGroupWaitBits(domotic_event_group,
                                           WIFI_CONNECTED_BIT,
                                           pdFALSE,
                                           pdFALSE,
                                           portMAX_DELAY);
    if (bits & WIFI_CONNECTED_BIT)
    {
        ESP_LOGI(TAG, "Initializing and starting SNTP");
        esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
        config.sync_cb = time_sync_notification_cb; // Note: This is only needed if we want
        esp_netif_sntp_init(&config);
    }

    char strftime_buf[64];
    time_t now = 0;
    while (1)
    {
        time(&now);
        setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
        tzset();
        struct tm timeinfo = {0};
        localtime_r(&now, &timeinfo);
        sprintf(strftime_buf, "%s %d %s %02d:%02d (%lli mn depuis reboot)", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon], timeinfo.tm_hour, timeinfo.tm_min, (esp_timer_get_time() / 1000 / 1000 / 60));
        send_event(EVT_TIME_SETTED, &strftime_buf);
        vTaskDelay(60000 / portTICK_PERIOD_MS);
    }
}
#endif
