#ifdef ENABLE_LCD_I2C

// LCD
unsigned char display_customCharSun[] = {B00100, B10001, B01110, B11111, B11111, B01110, B10001, B00100};
//unsigned char display_customCharMoon[] = {B11000, B01110, B00110, B00111, B00111, B00110, B01110, B11000};
unsigned char display_customCharMoon[] = {B00011, B01110, B01100, B11100, B11100, B01100, B01110, B00011};
unsigned char display_customCharArrowRight[] = {B01000, B01100, B01110, B01111, B01110, B01100, B01000, B00000};
unsigned char display_customCharDegC[] = {B01000, B10100, B01000, B00011, B00100, B00100, B00011, B00000};
unsigned char display_customCharConn[] = {B00000, B00000, B11100, B00010, B11001, B00101, B10101, B00000};
unsigned char display_customCharDisconn[] = {B10001, B01010, B00100, B01010, B10001, B00000, B10000, B00000};
unsigned char display_customCharReduction2[] = {B00000, B11111, B01110, B00100, B00000, B11111, B01110, B00100}; //(2 arrows down)
unsigned char display_customCharFlame[] = {B00100, B00100, B01010, B01010, B10101, B10101, B10101, B01110};
unsigned char display_customCharLock[] = {B01110, B10001, B10001, B11111, B11011, B11011, B11111, B00000};
unsigned char display_customCharPause[] = {B11011, B11011, B11011, B11011, B11011, B11011, B11011, B11011};

unsigned long display_lastOnMillis = 0;



void display_enable()
{
  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();
}



void display_disable()
{
  lcd.noBacklight();
  display_active = false;
}



void display_extendTimeout() { 
  display_lastOnMillis = millis(); 
}



void display_handleTimeout()
{
  if (display_active && (millis() - display_lastOnMillis) > (confDisplay.displayTimeout * 1000)) { 
      display_disable(); 
    }
}



void display_init()
{
  lcd.init();
  lcd.createChar(0, display_customCharMoon);
  lcd.createChar(1, display_customCharSun);
  lcd.createChar(2, display_customCharArrowRight);  // replaced by > char
  lcd.createChar(3, display_customCharDegC);
  lcd.createChar(4, display_customCharConn);
  lcd.createChar(5, display_customCharDisconn);
  lcd.createChar(6, display_customCharReduction2);
  lcd.createChar(7, display_customCharFlame);
  //lcd.createChar(7, display_customCharLock);
  
  // only 8 custom chars are possible :-/

  lcd.setCursor(0, 0);

#ifdef FIRMWARE_VARIANT_THERMOSTAT
  lcd.print("WiFiThermostat  ");
#elif FIRMWARE_VARIANT_HEATCONTROL
  lcd.print("HeatControl     ");
#else
  lcd.print("ESPIoTBase      ");
#endif

  lcd.setCursor(0, 1);
  lcd.print("(c) by FloKra.at");

  //  for ( int i = 0; i < 3; i++) {
  //    lcd.backlight();
  //    delay(200);
  //    lcd.noBacklight();
  //    delay(200);
  //  }
  lcd.backlight();
  // delay(1000);
  /// lcd.clear();
  display_active = true;
  display_lastOnMillis = millis();
}



void display_showWifiConnected()
{
  lcd.setCursor(0, 0);

  lcd.print("WIFI CONNECTED: ");
  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(0, 1);
  //lcd.print(WiFi.localIP());
  lcd.print(WiFi.SSID().c_str());

  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();
  display_overrideInterval = 3;
  display_updateImmediately = false;
}

uint8_t display_nextMsgToShow = 0;

void display_showNextMessageAfterTimeout() {
  if(display_nextMsgToShow == 1) {
    display_nextMsgToShow = 0;
    display_showAPModeActive();
  }
}

void display_showAPModeActive() {
    char _buf[17];
    lcd.setCursor(0, 0);
    lcd.print("                ");
    lcd.setCursor(0, 0);
    snprintf(_buf, 16, "AP: %s", persWM.getApSsid().c_str());
    lcd.print(_buf);
    lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.setCursor(0, 1);
    if(strlen(confSecrets.WiFiAPModePassword) > 0) {
      snprintf(_buf, 16, "PW: %s", confSecrets.WiFiAPModePassword);
      lcd.print(_buf);
    }
    else {
      lcd.print("  NO PASSWORD   ");
    }
    

    display_active = true;
    display_lastOnMillis = millis();

    display_showFullscreenMsg = true;

    // set a message that will be displayed after this one
    //display_nextMsgToShow = 0;
  
    // reset countInterval and set new Timeout to 4 sec
    display_countInterval = 0;
    display_overrideInterval = 15;
    display_updateImmediately = false;
}

void display_showWifiConnectionError()
{
  lcd.setCursor(0, 0);
  lcd.print("WIFI CONNECT    ");
  lcd.setCursor(0, 1);
  lcd.print(" !!! FAILED !!! ");
  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();

  display_showFullscreenMsg = true;

  // set a message that will be displayed after this one
  display_nextMsgToShow = 1;
  
  // reset countInterval and set new Timeout to 4 sec
  display_countInterval = 0;
  display_overrideInterval = 4;
  display_updateImmediately = false;

  //delay(2000);
  //
  //lcd.setCursor(0, 0);
  //lcd.print("AP MODE ACTIVE: ");
  //lcd.setCursor(0, 1);
  //lcd.print("                ");
  //lcd.setCursor(0, 1);
  //lcd.print(persWM.getApSsid());
  //lcd.backlight();
  //display_active = true;
  //display_lastOnMillis = millis();
  //delay(3000);
  //
  //lcd.setCursor(0, 0);
  //lcd.print("AP PASSWORD:    ");
  //lcd.setCursor(0, 1);
  //lcd.print("                ");
  //lcd.setCursor(0, 1);
  //lcd.print(confSecrets.WiFiAPModePassword);
  //lcd.backlight();
  //display_active = true;
  //display_lastOnMillis = millis();
  //delay(7000);
}

void display_showMQTTConnected()
{
  lcd.setCursor(0, 0);

  if(mqttclient.state() == 0) {
    lcd.print("MQTT CONNECTED: ");
    lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.setCursor(0, 1);
    lcd.print(confMqtt.mqtt_server);
  }
  else {
    lcd.print("MQTT:           ");
    lcd.setCursor(0, 1);
    lcd.print("- DISCONNECTED -");
  }

  

  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();

  display_showFullscreenMsg = true;
  display_countInterval = 0;
  display_overrideInterval = display_overlayMsgTimeout;
}

void display_showMQTTConnectionError() {
  lcd.setCursor(0, 0);
  lcd.print("MQTT CONNECTION ");
  lcd.setCursor(0, 1);
  lcd.print(" !!! FAILED !!! ");

  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();
  display_showFullscreenMsg = true;
  display_countInterval = 0;
  display_overrideInterval = display_overlayMsgTimeout;
}

void display_showWifi() {
  if(WifiInApMode) {
    char _buf[17];
    lcd.setCursor(0, 0);
    lcd.print("                ");
    lcd.setCursor(0, 0);
    snprintf(_buf, 16, "AP: %s", persWM.getApSsid().c_str());
    lcd.print(_buf);
    lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.setCursor(0, 1);
    if(strlen(confSecrets.WiFiAPModePassword) > 0) {
      snprintf(_buf, 16, "PW: %s", confSecrets.WiFiAPModePassword);
      lcd.print(_buf);
    }
    else {
      lcd.print("  NO PASSWORD   ");
    }
    lcd.print(_buf);
  }
  else {
    if( (strlen(confSecrets.WiFiPW1) > 3 && strlen(confDevWiFi.WiFiSSID1) > 2) || \
        (strlen(confSecrets.WiFiPW2) > 3 && strlen(confDevWiFi.WiFiSSID1) > 2) ) {
      
      lcd.setCursor(0, 0);
      if(WiFi.status() == WL_CONNECTED) {
        lcd.print("WIFI CONNECTED: ");
      }
      else {
        lcd.print("WIFI:         ");
      }
      lcd.setCursor(0, 1);
      lcd.print("                ");
      lcd.setCursor(0, 1);
      if(WiFi.status() == WL_CONNECTED) {
        lcd.print(WiFi.SSID().c_str());
      }
      else {
        lcd.print("- DISCONNECTED -");
      }
    }
    else {
      lcd.setCursor(0, 0);
      lcd.print("WIFI:           ");
      lcd.setCursor(0, 1);
      //       ("                ");
      lcd.print(" NOT CONFIGURED ");
    }
    
    
  }

  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();

  display_showFullscreenMsg = true;
  display_countInterval = 0;
  display_overrideInterval = display_overlayMsgTimeout;
}

void display_showIP()
{
  lcd.setCursor(0, 0);
  lcd.print("IP:             ");
  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(0, 1);
  
  if(WifiInApMode) {
    lcd.print("192.168.4.1");
  }
  else {
    lcd.print(WiFi.localIP().toString().c_str());
  }

  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();

  display_showFullscreenMsg = true;
  display_countInterval = 0;
  display_overrideInterval = display_overlayMsgTimeout;
}

void display_showHostname()
{
  lcd.setCursor(0, 0);
  lcd.print("Hostname:       ");
  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(0, 1);
  lcd.print(confDevWiFi.hostName);

  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();

  display_showFullscreenMsg = true;
  display_countInterval = 0;
  display_overrideInterval = display_overlayMsgTimeout;
}

void display_showUptime()
{
  lcd.setCursor(0, 0);
  lcd.print("System Uptime:  ");
  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(0, 1);
  
  //lcd.print("1d 10h 45m      ");
  lcd.print(getUptimeStr());

  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();

  display_showFullscreenMsg = true;
  display_countInterval = 0;
  display_overrideInterval = display_overlayMsgTimeout;
}



#ifdef ENABLE_FEATURE_NTP_TIME
void display_showDateTime()
{
  lcd.setCursor(0, 0);
  lcd.print("Date/Time:      ");
  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(0, 1);

  char _buf[17];
  updateTime();
  if(lt.tm_year > 70) {
    strftime(_buf, sizeof(_buf), "%H:%M %d.%m.%Y", &lt);
    lcd.print(_buf);
  }
  else {
    lcd.print("unavailable");
  }

  lcd.backlight();
  display_active = true;
  display_lastOnMillis = millis();

  display_showFullscreenMsg = true;
  display_countInterval = 0;
  display_overrideInterval = display_overlayMsgTimeout;
}
#endif // ENABLE_FEATURE_NTP_TIME



void display_showRow2OverlayMessage(char *msg)
{
  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(0, 1);
  lcd.write((uint8_t)2); // arrow right symbol
  lcd.print(" ");
  lcd.print(msg);

  for (int i = strlen(msg); i < 14; i++)
  {
    lcd.print(" ");
  }

  display_showRow2OverlayMsg = true;
  display_countInterval = 0;
  display_overrideInterval = display_overlayMsgTimeout;
}


void display_addConnectionIcon()
{
  lcd.setCursor(14, 1);
  lcd.print(" ");
  if (mqtt_connected)
    lcd.write((uint8_t)4); // connected symbol
  else
    lcd.write((uint8_t)5); // disconnected symbol
}



uint8_t display_currentFullscreenMsg = 0;

void display_toggleFullscreenMessage() {
  if(!display_showFullscreenMsg || display_currentFullscreenMsg == 0) {
    display_currentFullscreenMsg++;
    display_showWifi();
    sendLog(F("DISP: showWifi"));
  }
  else if(display_currentFullscreenMsg == 1) {
    display_currentFullscreenMsg++;
    display_showIP();
    sendLog(F("DISP: showIP"));
  }
  else if(display_currentFullscreenMsg == 2) {
    display_currentFullscreenMsg++;
    display_showHostname();
    sendLog(F("DISP: showHostname"));
  }
  else if(display_currentFullscreenMsg == 3) {
    display_currentFullscreenMsg++;
    display_showMQTTConnected();
    sendLog(F("DISP: showMQTT"));
  }
  else if(display_currentFullscreenMsg == 4) {
    display_currentFullscreenMsg++;
    display_showUptime();
    sendLog(F("DISP: showUptime"));
  }
  else if(display_currentFullscreenMsg == 5) {
    display_currentFullscreenMsg = 0;
    #ifdef ENABLE_FEATURE_NTP_TIME
      if (confTime.ntpEnable) {
        display_showDateTime();
        sendLog(F("DISP: showDateTime"));
      }
    #endif
  }
  else {
    sendLog(F("DISP: show???"));
    display_currentFullscreenMsg = 0;
  }
}



#ifdef DISPLAY_USE_MINIMAL_LAYOUT

void display_update()
{
  if (display_showFullscreenMsg)
   {
       if(display_nextMsgToShow > 0) {
           display_showNextMessageAfterTimeout();
       }
       else {
           display_showFullscreenMsg = false;
           //display_overrideInterval = confDisplay.displayInterval;
           display_overrideInterval = 0;
           display_updateImmediately = true;
       }
  }
  else
  {    
    display_lastDisplayUpdate = millis();
    lcd.setCursor(0, 0);

    // LCD line 2
    // display target temperature to line 2, 8 chars length incl space at the end
    lcd.setCursor(0, 1);

    if (system_doRestart) { lcd.print("Restarting... "); }
    else if (system_pendingRestart)
    {
      lcd.print("Restart?      ");
    }

    // Status Message in Display Row 2
    else if (display_showRow2OverlayMsg)
    {
      display_showRow2OverlayMsg = false;
      display_overrideInterval = confDisplay.displayInterval;  
      display_updateImmediately = true;
    }
    else
    {
        display_addConnectionIcon();
    }
  }
}

#endif // DISPLAY_USE_MINIMAL_LAYOUT

#endif // ENABLE_LCD_I2C
