#ifndef CALCULATE_PARKING_TICKET_TARIFF_STEP_H_INCLUDED
#define CALCULATE_PARKING_TICKET_TARIFF_STEP_H_INCLUDED

#include <cinttypes>
#include <QDateTime>
#include <QDebug>
#include <assert.h>

///
/// \brief The TariffStep class
///
/// Ein Berechnungsschritt kann alles sein zwischen einer Minute und einem Tag.
/// Jeder Tag wird zerlegt in 1440 Berechnungsschritte.

class TariffStep {
    QDateTime m_datetime;  //  minute since start of tariff calculation
    int m_dayOfWeek;
    uint32_t m_price;
    TariffStep const *m_next;

    static int m_offsetInDays;

public:
    // static setOffsetinDays(int offsetInDays) { m_offsetInDays = offsetInDays; }

    TariffStep()
        : m_datetime(QDateTime())
        , m_dayOfWeek(-1)
        , m_price(0)
        , m_next(nullptr) {
    }

    TariffStep(QDateTime const &datetime,
               uint32_t price,
               TariffStep const *next)
        : m_datetime(datetime)
        , m_dayOfWeek(m_datetime.isValid() ? m_datetime.date().dayOfWeek() : -1)
        , m_price(price)
        , m_next(next) {

    }

    TariffStep const *next() const { return m_next; }
    //TariffStep *next() { return m_next; }

    int dayOfWeek() const { return m_dayOfWeek; }

    bool contains(QTime const &minute) const {
        if (dateTime().isValid()
         && minute.isValid()
         && m_next && m_next->dateTime().isValid()) {
            if (dayOfWeek() == m_next->dayOfWeek()) {
                return
                    (dateTime().time() <= minute
                     && minute < m_next->dateTime().time());
            }
            return (dateTime().time() <= minute);
        }
        return false;
    }

    uint16_t startHour() {
        return m_datetime.time().hour();
    }

    uint32_t price() const { return m_price; }
    bool isValid() { return m_datetime.isValid(); }
    TariffStep const *address() { return this; }

    void setPrice(uint32_t price) { m_price = price; }
    void setDateTime(QDateTime const &datetime) { m_datetime = datetime; }
    void setNextTariffStep(TariffStep *const next) { m_next = next; }

    QDateTime &dateTime() { return m_datetime; }
    QDateTime const &dateTime() const { return m_datetime; }
};

#endif // CALCULATE_PARKING_TICKET_TARIFF_STEP_H_INCLUDED
