#ifdef FIRMWARE_VARIANT_HEATCONTROL

uint8_t heatcontrol_display_nextStatusMsg = 0;

void display_update()
{
    if (display_showFullscreenMsg)
    {
        if(display_nextMsgToShow > 0) {
            display_showNextMessageAfterTimeout();
        }
        else {
            display_showFullscreenMsg = false;
            display_overrideInterval = 0;
            display_updateImmediately = true;
        }
    }
    else
    {
        char _ch_temp[6];

        int _tOut = (int)owTemp_out;

        display_lastDisplayUpdate = millis();

        // LCD line 1
        lcd.setCursor(0, 0);

        // clear display line
        lcd.print("                ");

        lcd.setCursor(0, 0);

        char _buf[3];

        // T Vorlauf
        sprintf(_buf, PGMStr_heatc_display_caption_tempFeed);
        lcd.print(_buf);

        dtostrf(owTemp_feed, 2, 0, _ch_temp);
        lcd.print(_ch_temp);
        
        lcd.print(" ");

        sprintf(_buf, PGMStr_heatc_display_caption_tempReturn);
        lcd.print(_buf);

        dtostrf(owTemp_return, 2, 0, _ch_temp);
        lcd.print(_ch_temp);

        lcd.print(" ");

        sprintf(_buf, PGMStr_heatc_display_caption_tempOutside);
        lcd.print(_buf);

        if(owTemp_out < -9) dtostrf(owTemp_out, 3, 0, _ch_temp);
        else if(owTemp_out < 0) dtostrf(owTemp_out, 2, 0, _ch_temp);
        else if(owTemp_out < 10) dtostrf(owTemp_out, 1, 0, _ch_temp);
        else if(owTemp_out >= 10) dtostrf(owTemp_out, 2, 0, _ch_temp);
        else dtostrf(owTemp_out, 3, 0, _ch_temp);
        lcd.print(_ch_temp);



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

        if (system_doRestart) { 
            char _buf[17];
            snprintf_P(_buf, 16, PGMStr_display_restarting);
            lcd.print(_buf);
        }
        else if (system_pendingRestart) {
            char _buf[17];
            snprintf_P(_buf, 16, PGMStr_display_restartQuest);
            lcd.print(_buf);
        }
        // Status Message in Display Row 2
        else if (display_showRow2OverlayMsg) {
            display_showRow2OverlayMsg = false;
            //displayInterval = confDisplay.displayInterval;  
            display_updateImmediately = true;
        }
        else if (heatcontrol_state_testmode_active) {
            heatcontrol_display_showTestmodeRemaining();
        }
        else if (heatcontrol_out_heat) {
            char _buf[17];
            if(confHeatc.useHeatCurve) {
                snprintf_P(_buf, 16, PGMStr_heatc_display_heatingUntilTFeed, heatcontrol_currentHeatCurveValue + confHeatc.hystereseOff);
            }
            else {
                snprintf_P(_buf, 16, PGMStr_heatc_display_heating);
            }
            lcd.print(_buf);
            display_addConnectionIcon();
        }

        else if (heatcontrol_lockTime_startMillis > 0 && heatcontrol_state_pumpBacklash_active) {
            heatcontrol_display_showToggling_LockTimeRemaining_pumpBacklash();
        }

        else if (heatcontrol_lockTime_startMillis > 0) {
            heatcontrol_display_showLockTimeRemaining();
        }
        
        else if (heatcontrol_lockTemp_active() && heatcontrol_state_pumpBacklash_active) {
            heatcontrol_display_showToggling_LockUntilTempFeed_pumpBacklash();
        }

        else if (heatcontrol_state_pumpBacklash_active) {
            heatcontrol_display_showPumpBacklashRemaining();
        }

        else if (heatcontrol_lockTemp_active()) {
            heatcontrol_display_showLockUntilTempFeed();
        }

        else {
            char _buf[17];

            // overrule precondition to false if outside temp is above limit
            if ( heatcontrol_in_sw_disableControl_heating && heatcontrol_in_sw_disableControl_pump) {
                snprintf_P(_buf, 16, PGMStr_heatc_display_disabled_switch_both);
            }
            else if ( heatcontrol_in_sw_disableControl_heating) {
                snprintf_P(_buf, 16, PGMStr_heatc_display_disabled_switch_heat);
            }
            else if ( heatcontrol_in_sw_disableControl_pump) {
                snprintf_P(_buf, 16, PGMStr_heatc_display_disabled_switch_pump);
            }
            else if (_tOut >= confHeatc.disableAboveTOut) {
                snprintf_P(_buf, 16, PGMStr_heatc_display_disabled_outtemp);
            } 
            else {
                snprintf_P(_buf, 16, PGMStr_heatc_display_idle_ready);
            }

            lcd.print(_buf);
            display_addConnectionIcon();
        }
    }
}

void heatcontrol_display_showLockTimeRemaining() {
    char _buf[17];
    uint8_t _remainingMins = heatcontrol_lockTime_getRemainingMins();
    char _space[2];
    if(_remainingMins >= 10) {
        _space[0] = '\0';
    }
    else {
        _space[0] = ' ';
        _space[1] = '\0';
    }
    snprintf_P(_buf, 16, PGMStr_heatc_display_lockTimeRemaining, _space, _remainingMins);
    lcd.print(_buf);
    display_addConnectionIcon();
}

void heatcontrol_display_showLockUntilTempFeed() {
    char _buf[17];

    snprintf_P(_buf, 16, PGMStr_heatc_display_lockUntilTFeed, (heatcontrol_currentHeatCurveValue - confHeatc.hystereseOn - 1));
    lcd.print(_buf);
    display_addConnectionIcon();
}

void heatcontrol_display_showPumpBacklashRemaining() {
    char _buf[17];
    uint8_t _remainingMins = heatcontrol_pumpBacklash_getRemainingMins();
    char _space[2];
    if(_remainingMins >= 10) {
        _space[0] = '\0';
    }
    else {
        _space[0] = ' ';
        _space[1] = '\0';
    }
    snprintf_P(_buf, 16, PGMStr_heatc_display_pumpBacklashRemaining, _space, _remainingMins);
    lcd.print(_buf);
    display_addConnectionIcon();
}

void heatcontrol_display_showTestmodeRemaining() {
    //display_enable(); // display stays ON in this mode
    display_extendTimeout(); // display stays ON in this mode
    char _buf[17];
    uint8_t _remainingMins = heatcontrol_testMode_getRemainingMins();
    char _space[2];
    if(_remainingMins >= 10) {
        _space[0] = '\0';
    }
    else {
        _space[0] = ' ';
        _space[1] = '\0';
    }
    snprintf_P(_buf, 16, PGMStr_heatc_display_testModeRemaining, _space, _remainingMins);
    lcd.print(_buf);
    display_addConnectionIcon();
}

void heatcontrol_display_showToggling_LockTimeRemaining_pumpBacklash() {
    if(heatcontrol_display_nextStatusMsg == 0) {
        heatcontrol_display_showLockTimeRemaining();
        heatcontrol_display_nextStatusMsg++;
    }
    else {
        heatcontrol_display_showPumpBacklashRemaining();
        heatcontrol_display_nextStatusMsg = 0;
    }
}

void heatcontrol_display_showToggling_LockUntilTempFeed_pumpBacklash() {
    if(heatcontrol_display_nextStatusMsg == 0) {
        heatcontrol_display_showLockUntilTempFeed();
        heatcontrol_display_nextStatusMsg++;
    }
    else {
        heatcontrol_display_showPumpBacklashRemaining();
        heatcontrol_display_nextStatusMsg = 0;
    }
}


#endif // FIRMWARE_VARIANT_HEATCONTROL
