// LCD
byte customCharSun[8] = {0b00100, 0b10001, 0b01110, 0b11111, 0b11111, 0b01110, 0b10001, 0b00100};
byte customCharMoon[8] = {0b11000, 0b01110, 0b00110, 0b00111, 0b00111, 0b00110, 0b01110, 0b11000};
byte customCharArrowRight[8] = {0b00000, 0b00100, 0b00110, 0b11111, 0b11111, 0b00110, 0b00100, 0b00000};
//bool displayActive = false;
unsigned long displayLastOnMillis = 0;

void enableDisplay() {
  lcd.backlight();
  displayActive = true;
  displayLastOnMillis = millis();
}

void disableDisplay() {
  lcd.noBacklight();
  displayActive = false;
}

void extendDisplayTimeout() {
  displayLastOnMillis = millis();
}

void handleDisplayTimeout() {
  if (displayActive && (millis() - displayLastOnMillis) > (displayTimeout * 1000)) {
    disableDisplay();
  }
}

void initDisplay() {
  lcd.init();
  lcd.createChar(0, customCharMoon);
  lcd.createChar(1, customCharSun);
  lcd.createChar(2, customCharArrowRight);

  lcd.setCursor(0, 0);

  lcd.print("WiFi-Thermostat ");
  lcd.setCursor(0, 1);
  lcd.print("      by Flo Kra");

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

void updateDisplay() {

  long now = millis();
  if ( ((now - outTempHumLastUpdate) < 300000) && outTempHumLastUpdate != 0 && whichTempToDisplay == 1 && ((now - lastDisplayToggle) > (displayInterval * 1000)) ) {
    // outside temp has been updated < 5 min ago, last displayed temp was "I" and display interval is overdue
    whichTempToDisplay = 2; // 1= I, 2= A
    lastDisplayToggle = now;
  }
  else if ( whichTempToDisplay != 1 && ((now - lastDisplayToggle) > (displayInterval * 1000)) ) {
    whichTempToDisplay = 1; // 1= I, 2= A
    lastDisplayToggle = now;
  }

  lastDisplayUpdate = now;

  boolean showTemp = false;
  char temp_chararr[6];
  char hum_chararr[4];
  char tempLabel[2];

  if (whichTempToDisplay == 2) {
    if ( outTempHumLastUpdate != 0 && (now - outTempHumLastUpdate) < 120000 ) {
      showTemp = true;
      dtostrf(outTemp, 5, 1, temp_chararr );
      sprintf(hum_chararr, "%2i", outHum);
      strcpy(tempLabel, "A");
    }
  }
  else {
    if ( lastTempUpdate != 0 && (now - lastTempUpdate) < 120000 ) {
      showTemp = true;
      dtostrf(temperature, 5, 1, temp_chararr );
      sprintf(hum_chararr, "%2i", humidity);
      strcpy(tempLabel, "I");
    }
  }

  // LCD line 1
  // print temperature incl = and ° symbol + humidity to lcd, line 1, first 11 chars
  lcd.setCursor(0, 0);

  if (showTemp) {
    //lcd.write(0x3D); // = Zeichen
    lcd.print(tempLabel);
    lcd.print(" ");
    lcd.print(temp_chararr);
    lcd.write(0xDF); // degree symbol
    lcd.print(" ");

    if (humidity == 0) lcd.print("--");
    else if (humidity < 10) {
      lcd.print(" ");
      lcd.print(hum_chararr);
    }
    else {
      lcd.print(hum_chararr);
    }
    lcd.print("%");
    lcd.print("  ");
  }
  else {
    lcd.print("             ");
  }

  // display current mode on LCD
  lcd.setCursor(13, 0);
  if (heatingMode > 0) {
    if ( heatingMode == 1 ) { // day/normal mode
      lcd.print(" ");
      lcd.write((uint8_t)1); // sun symbol if mode is day/normal
      lcd.print(" ");
    }
    else if ( heatingMode == 2 ) { // night/reduction mode
      lcd.print(" ");
      lcd.write((uint8_t)0); // moon symbol if mode is night/reduction
      lcd.print(" ");
    }
  }
  else lcd.print("   "); // mode is heating off



  float curr_setTemp;
  if (heatingMode == 1) curr_setTemp = setTemp;
  else if (heatingMode == 2) curr_setTemp = setTempLow;
  char currSetTemp_chararr[6];
  dtostrf(curr_setTemp, 5, 1, currSetTemp_chararr );


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

  // when the heating mode is OFF, do not display target temp - instead show "Heating off" info in line 2
  if ( heatingMode == 0 ) {
    //         1234567890123456
    lcd.print("  Heizung aus   ");
  }
  else {
    lcd.write((uint8_t)2); // Pfeil rechts
    lcd.print(" ");
    lcd.print(currSetTemp_chararr);
    lcd.write(0xDF); // degree symbol
    lcd.print(" ");

    // display status info to line 2 from char 9 -> 8 chars length
    lcd.setCursor(8, 1);
    lcd.print(" ");
    if (turnHeatingOn) {
      //         1234567
      lcd.print("heizen ");
    }
    else if ( heatingMode == 1 ) { // day/normal mode
      //         1234567
      lcd.print("       ");
    }
    else if ( heatingMode == 2 ) { // night/reduction mode
      //         1234567
      lcd.print("Absenk.");
    }
    else lcd.print("        ");
  }

}

