#include "settings.h"

// Uptime
struct sysUptimeStruct
{
  unsigned int day = 0;
  unsigned int hour = 0;
  unsigned int min = 0;
  unsigned int sec = 0;
  unsigned int highMillis = 0;
  unsigned int rollover = 0;
  char uptimeStr[16];
} sysUptime;

unsigned long system_restartAtMillis = 0;
bool system_pendingRestart = false;
bool system_doRestart = false;
unsigned long system_pendingRestart_lastMillis = 0;


bool mqtt_tempDisabled_credentialError = false;

char mqtt_topic_in_cmd[61];
char mqtt_topic_in_setTemp[61];
char mqtt_topic_in_setMode[61];
char mqtt_topic_in_setPreset[61];
char mqtt_topic_in_setPause[61];



// Sensors - global
unsigned long sensors_maxMeasurementAge = MAX_MEASUREMENT_AGE;
unsigned long sensors_maxMeasurementAge_outside = MAX_MEASUREMENT_AGE_OUT;

// DHT Sensor
float sensor_DHT_currTemp;                     // last reading from DHT sensor with smoothing
float sensor_DHT_currTemp_raw;                 // last reading from DHT sensor (raw)
uint16_t sensor_DHT_currHum;                   // last reading from DHT sensor with smoothing
uint16_t sensor_DHT_currHum_raw;               // last reading from DHT sensor (raw)
float sensor_DHT_tmpHum, sensor_DHT_tmpTemp;
int sensor_DHT_readRetries = 2;
unsigned long sensor_DHT_lastUpdate = 0;    // last update time of DHT reading

// OneWire Sensors
int8_t owTemp_feed, owTemp_return, owTemp_out;


// 
float outTemp;                      // outside temp (via MQTT if enabled and in-topic configured)
uint16_t outHum;                    // outside temp (via MQTT if enabled and in-topic configured)
unsigned long outTempHumLastUpdate; // last reading from out temp/hum source
char outTemp_newValue[6];
bool outTemp_parseNewValue;
char outHum_newValue[4];
bool outHum_parseNewValue;









unsigned long lastMeasure = 0;       // millis of last temp/hum measurement


//char msg[101];                       // buffer MQTT in payload
//char topic[70];                      // buffer MQTT in topic

bool PIRSensorOn = false;


//bool config_was_changed = false;
bool configChangedRestartRequired = false;
bool configChangedMqttConnResetRequired = false;




char cmdPayload[101];    // buffer for commands
bool cmdInQueue = false; // command is queued and will be processed next loop() run

// not used any more, now triggered directly
//bool saveConfigToFlash = false;  // conf is saved in next loop() run
//bool saveConfig2ToFlash = false; // conf2 is saved in next loop() run
// ----


unsigned long lastConfigChange;
bool lastConfigChangeNoteAlreadyDone = false;


unsigned long lastRun = 0;
uint16_t count100ms = 0;
uint16_t countSeconds = 0;
uint16_t countMeasureInterval = 0;

unsigned long mqttLastReconnectAttempt = 0;
uint16_t mqttReconnectAttempts = 0;
uint16_t mqtt_reconnects = 0;
bool mqtt_connected = false;
unsigned long mqtt_lastHeartbeat;
bool mqttInTopicSubscribed = false;
char mqtt_topic_log[50];


// Display
uint16_t display_countInterval = 0;
uint16_t display_overlayMsgTimeout = 2;
bool display_showFullscreenMsg = false;
bool display_updateImmediately = false;
bool display_showRow2OverlayMsg = false;
//uint16_t displayInterval;
uint16_t display_overrideInterval;
unsigned long display_lastDisplayUpdate = 0; // millis of last display update
unsigned long display_lastDisplayToggle = 0; // millis of last display toggle
bool display_active = false; // gets true when button is pressed. display light
                            // gets switched on until timeout. button actions
                            // are only performed while display is active



// MQTT publishing
unsigned int outPubInterval_states_count = 0;
unsigned int outPubInterval_sensors_count = 0;


// if command "debugmode 1" is called...
bool debug = false;
bool debug2 = false;
bool sysInfoEverySecond = false;


// global buffer variable for use with sendLog function
char logBuf[LOG_BUFFER_SIZE];

bool WifiInApMode = false;
unsigned long WifiApModeStartedAt;

#ifdef ENABLE_FEATURE_SECRETS_ENCRYPTION
char encKey[] = SECRETS_ENCRYPTION_KEY;
#endif
bool confEncryptionEnabled = false;

#ifdef ENABLE_FEATURE_NTP_TIME
struct tm lt; // http://www.cplusplus.com/reference/ctime/tm/
//const char *const PROGMEM dayNames[] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
//const char *const PROGMEM dayShortNames[] = {"So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"};
//const char *const PROGMEM monthNames[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
//const char *const PROGMEM monthShortNames[] = {"Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"};
#endif

#ifdef ENABLE_FEATURE_WEB_CONSOLE
char webLogBuffer[WEB_CONSOLE_BUFFER_SIZE];
unsigned int webLogBuffer_index = 0;
#endif

char mqttCurrentStateName[25];

char httpServer_returnPage[21];

