#include <ui.h>

void clearScreen()
{
    Serial.print("\033[2J"); // clear screen
    Serial.print("\033[H");  // cursor home
    Serial.flush();
}

void setCursor(const uint8_t x, const uint8_t y)
{
    Serial.printf("\033[%d;%d", y, x + 1);
    Serial.flush();
}

void printField(const char name[], const uint32_t val)
{
    Serial.printf("%15s: %06u\n", name, val);
}

void printField(const char name[], const int32_t val)
{
    Serial.printf("%15s: %06d\n", name, val);
}

void printField(const char name[], const int64_t val)
{
    Serial.printf("%15s: %06u\n", name, (uint64_t)val);
}

void printField(const char name[], const float val)
{
    Serial.printf("%15s: %4.2f\n", name, val);
}

void printField(const char name[], const char *val)
{
    Serial.printf("%15s: %s\n", name, val);
}

void printInfo(const ignitionBoxStatus &info)
{
    clearScreen();
    setCursor(0, 0);
    printField("++ Timestamp ++", (uint32_t)info.timestamp);
    Serial.println("========== Coils 12 =============");
    printField("Events", info.coils12.n_events);
    printField("Events Missed", info.coils12.n_missed_firing);
    printField("Spark Dly", (uint32_t)info.coils12.spark_delay);
    printField("Spark Sts", sparkStatusNames.at(info.coils12.spark_status));
    printField("Peak P_IN", info.coils12.peak_p_in);
    printField("Peak N_IN", info.coils12.peak_n_in);
    printField("Peak P_OUT", info.coils12.peak_p_out);
    printField("Peak N_OUT", info.coils12.peak_n_out);
    printField("Soft Start ", softStartStatusNames.at(info.coils12.sstart_status));

    Serial.println("========== Coils 34 =============");
    printField("Events", info.coils34.n_events);
    printField("Events Missed", info.coils34.n_missed_firing);
    printField("Spark Dly", (uint32_t)info.coils34.spark_delay);
    printField("Spark Sts", sparkStatusNames.at(info.coils34.spark_status));
    printField("Peak P_IN", info.coils34.peak_p_in);
    printField("Peak N_IN", info.coils34.peak_n_in);
    printField("Peak P_OUT", info.coils34.peak_p_out);
    printField("Peak N_OUT", info.coils34.peak_n_out);
    printField("Soft Start ", softStartStatusNames.at(info.coils34.sstart_status));

    Serial.println("============ END ===============");
    Serial.println();
    printField("Engine RPM", info.eng_rpm);
    printField("ADC Read Time", info.adc_read_time);
    printField("Queue Errors", info.n_queue_errors);
}
