#include <Wire.h>
#include <RtcDS3231.h>
#include <stdlib.h>
#include <ESP8266HTTPClient.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
#include <Timezone.h>


static const String TIMEURL = "http://api.itsblue.de/time.php";
static const String MONTH_STR[12] = {"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez"};

class led_clock_time
{
private:
    RtcDS3231<TwoWire> * Rtc;
    bool timedebug = 0;
    bool rtc_ready = false;
    uint32_t int_second = 0;
    uint32_t int_minute = 0;
    uint32_t int_hour = 0;
    uint16_t int_year = 0;
    bool timeisvalid = false;

    uint32_t lastwrittentortc = 0;
    static const uint32_t WAITBEFOREWRITERTCAGAIN_MS = 100;

    static const uint32_t NTP_SYNC_CYCLE_SECS = 600; 
    static const uint16_t NTP_SYNC_MAX_TRIES = 10;  
    time_t timelastsync_ntptime = 0;    /// last time got form the sync time server
    time_t epoch = 0;                   /// epoch time got from the server - or adjusted with millis()
    time_t timelastsync = 0;            /// last time sync in processor time (millis())
    bool timesynced = false;            /// set to true if time was synced
    time_t timestamp = 0;

    HTTPClient * http;

    WiFiManager wifiManager;
    long captivepotal_timeout = 60;    /// in seconds
    bool isconnected = false;
    bool wasalreadyconnected = false;

    bool rtc_init();
    bool rtc_settime(RtcDateTime newtime);
    bool rtc_gettime(void);
    void dbg_gettime(void);
    bool connectWifi();

    bool syncTime();
  
public:
    void init(bool debug = false);
    bool gettime(uint32_t *hour, uint32_t *minute, uint32_t *second);
    bool isConnectedToWifi();
    bool isTimenSynced();
};