#pragma once
#include <Arduino.h>
#include <map>

// =====================
// Event Flags (bitmask)
// =====================
static const uint32_t TRIG_FLAG_12P = (1 << 0);
static const uint32_t TRIG_FLAG_12N = (1 << 1);
static const uint32_t TRIG_FLAG_34P = (1 << 2);
static const uint32_t TRIG_FLAG_34N = (1 << 3);

static const uint32_t SPARK_FLAG_TIMEOUT = (1 << 8);
static const uint32_t SPARK_FLAG_12 = (1 << 9);
static const uint32_t SPARK_FLAG_34 = (1 << 10);

// Spark Status
enum sparkStatus
{
    SPARK_POS_OK,
    SPARK_NEG_OK,
    SPARK_POS_SKIP,
    SPARK_NEG_SKIP,
    SPARK_POS_WAIT,
    SPARK_NEG_WAIT,
    SPARK_POS_FAIL,
    SPARK_NEG_FAIL,
    SPARK_POS_UNEXPECTED,
    SPARK_NEG_UNEXPECTED,
    SPARK_SYNC_FAIL,
};

static const std::map<const sparkStatus, const char *> sparkStatusNames = {
    {SPARK_POS_OK, "SPARK_POS_OK"},
    {SPARK_NEG_OK, "SPARK_NEG_OK"},
    {SPARK_POS_SKIP, "SPARK_POS_SKIP"},
    {SPARK_NEG_SKIP, "SPARK_NEG_SKIP"},
    {SPARK_POS_WAIT, "SPARK_POS_WAIT"},
    {SPARK_NEG_WAIT, "SPARK_NEG_WAIT"},
    {SPARK_POS_FAIL, "SPARK_POS_FAIL"},
    {SPARK_NEG_FAIL, "SPARK_NEG_FAIL"},
    {SPARK_POS_UNEXPECTED, "SPARK_POS_UNEXPECTED"},
    {SPARK_NEG_UNEXPECTED, "SPARK_NEG_UNEXPECTED"},
    {SPARK_SYNC_FAIL, "SPARK_SYNC_FAIL"},
};

enum softStartStatus
{
    NORMAL,
    SOFT_START,
    ERROR,
};

const std::map<const softStartStatus, const char *> softStartStatusNames = {
    {NORMAL, "NORMAL"},
    {SOFT_START, "SOFT_START"},
    {ERROR, "ERROR"},
};

struct coilsStatus
{
    int64_t trig_time = 0;
    int64_t spark_time = 0;
    int32_t spark_delay = 0; // in microseconds
    sparkStatus spark_status = sparkStatus::SPARK_POS_OK;
    softStartStatus sstart_status = softStartStatus::NORMAL;
    float peak_p_in = 0.0;
    float peak_n_in = 0.0;
    float peak_p_out = 0.0;
    float peak_n_out = 0.0;
    float level_spark = 0.0;
    uint32_t n_events = 0;
    uint32_t n_missed_firing = 0;
};

// Task internal Status
struct ignitionBoxStatus
{
    int64_t timestamp = 0;
    // coils pairs for each ignition
    coilsStatus coils12;
    coilsStatus coils34;
    // voltage from generator
    float volts_gen = 0.0;
    // enine rpm
    int32_t eng_rpm = 0;
    // debug values
    uint32_t n_queue_errors = 0;
    int32_t adc_read_time = 0;
};
