#include <time.h>
#include <sys/time.h>
#include <esp_sntp.h>
#include "esp_log.h"

#include "sntp.h"

static const char *TAG = "smart-oil-heater-control-system-sntp";
static eSntpState sntpState = SYNC_NOT_STARTED;
void time_sync_notification_cb(struct timeval *tv);

void initSntp(void)
{
    esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
    esp_sntp_setservername(0, CONFIG_SNTP_SERVER_IP_ADDR);

    sntp_set_time_sync_notification_cb(time_sync_notification_cb);
    esp_sntp_init();
}

eSntpState getSntpState(void)
{
    return sntpState;
}

void time_sync_notification_cb(struct timeval *tv)
{
    ESP_LOGI(TAG, "SNTP synchronization! Unix Time: %lld", tv->tv_sec);
    sntpState = SYNC_SUCCESSFUL;
}