#ifndef TARIFF_PREPAID_H_INCLUDED
#define TARIFF_PREPAID_H_INCLUDED

#include <QDateTime>
#include <QString>

#include "time_range.h"

enum class ApplyPrepaid {
    NEVER = 0,
    MATCH_PREV_DAY = 1,
    MATCH_NEXT_DAY = 2,
    ALWAYS = 3
};

struct ATBTariffPrepaid {
    int m_id;
    QString m_weekDay;
    QDate m_date;
    TimeRange m_range;
    ApplyPrepaid m_prepaidIf;

    explicit ATBTariffPrepaid()
      : m_id(-1)
      , m_prepaidIf(ApplyPrepaid::NEVER) {
    }

    void setPrepaidIf(QString const &ppif) {
        if (ppif == "never") {
            m_prepaidIf = ApplyPrepaid::NEVER;
        } else
        if (ppif == "match_prev_day") {
            m_prepaidIf = ApplyPrepaid::MATCH_PREV_DAY;
        } else
        if (ppif == "match_next_day") {
            m_prepaidIf = ApplyPrepaid::MATCH_NEXT_DAY;
        } else
        if (ppif == "always") {
            m_prepaidIf = ApplyPrepaid::ALWAYS;
        } else {
           qCritical() << __func__ << ":" << __LINE__ << "ERROR unknown carry over application" << ppif;
        }
    }

    ApplyPrepaid prepaidIf() const {
        return m_prepaidIf;
    }

    QString prepaidIfStr() const {
        if (m_prepaidIf == ApplyPrepaid::NEVER) {
            return "never";
        }
        if (m_prepaidIf == ApplyPrepaid::ALWAYS) {
            return "always";
        }
        if (m_prepaidIf == ApplyPrepaid::MATCH_PREV_DAY) {
            return "match prev day";
        }
        if (m_prepaidIf == ApplyPrepaid::MATCH_NEXT_DAY) {
            return "match next day";
        }
        return QString("ERROR unknown prepaid application: %1").arg(static_cast<int>(m_prepaidIf));
    }

    friend QDebug operator<<(QDebug debug, ATBTariffPrepaid const &pp) {
        QDebugStateSaver saver(debug);

        debug.nospace()
            << "\nTariffPrepaid:\n"
            << "       week day: " << pp.m_weekDay << "\n"
            << "           date: " << pp.m_date.toString(Qt::ISODate) << "\n"
            << "             id: " << pp.m_id << "\n"
            << "          start: " << pp.m_range.m_start << "\n"
            << "            end: " << pp.m_range.m_end << "\n"
            << "       duration: " << pp.m_range.m_duration << "\n"
            << "     prepaid if: " << pp.prepaidIfStr() << endl;
        return debug;
    }
};

// deprecated

struct ATBPrepaid {
    int id;
    bool anytime;
    bool never;
    QTime static_start;
    QTime static_end;

    struct week {
        int day;
        QTime static_start;
        QTime static_end;
        int duration;
    } prepaid[8];

    explicit ATBPrepaid()
      : id(-1)
      , anytime(false)
      , never(false)
      , static_start(QTime(23, 59, 59))
      , static_end(QTime(0, 0, 0)) {

        for (int i = 0 ; i < 8; ++i) {
            prepaid[i].day = -1;
            prepaid[i].static_start = QTime(23, 59, 59);
            prepaid[i].static_end = QTime(0, 0, 0);
            prepaid[i].duration = -1;
        }
    }

    friend QDebug operator<<(QDebug debug, ATBPrepaid const &pp) {
        QDebugStateSaver saver(debug);

        debug.nospace()
            << "                 id: " << pp.id << "\n"
            << "       static_start: " << pp.static_start.toString(Qt::ISODate) << "\n"
            << "         static_end: " << pp.static_end.toString(Qt::ISODate) << "\n"
            << "            anytime: " << pp.anytime << "\n"
            << "              never: " << pp.never << "\n"
            << "        **** Monday **** \n"
            << "                day: " << pp.prepaid[(int)Qt::Monday].day << "\n"
            << "       static_start: " << pp.prepaid[(int)Qt::Monday].static_start.toString(Qt::ISODate) << "\n"
            << "         static_end: " << pp.prepaid[(int)Qt::Monday].static_end.toString(Qt::ISODate) << "\n"
            << "           duration: " << pp.prepaid[(int)Qt::Monday].duration << "\n"
            << "        **** Tuesday **** \n"
            << "                day: " << pp.prepaid[(int)Qt::Tuesday].day << "\n"
            << "       static_start: " << pp.prepaid[(int)Qt::Tuesday].static_start.toString(Qt::ISODate) << "\n"
            << "         static_end: " << pp.prepaid[(int)Qt::Tuesday].static_end.toString(Qt::ISODate) << "\n"
            << "           duration: " << pp.prepaid[(int)Qt::Tuesday].duration << "\n"
            << "        **** Wednesday **** \n"
            << "                day: " << pp.prepaid[(int)Qt::Wednesday].day << "\n"
            << "       static_start: " << pp.prepaid[(int)Qt::Wednesday].static_start.toString(Qt::ISODate) << "\n"
            << "         static_end: " << pp.prepaid[(int)Qt::Wednesday].static_end.toString(Qt::ISODate) << "\n"
            << "           duration: " << pp.prepaid[(int)Qt::Wednesday].duration << "\n"
            << "        **** Thursday **** \n"
            << "                day: " << pp.prepaid[(int)Qt::Thursday].day << "\n"
            << "       static_start: " << pp.prepaid[(int)Qt::Thursday].static_start.toString(Qt::ISODate) << "\n"
            << "         static_end: " << pp.prepaid[(int)Qt::Thursday].static_end.toString(Qt::ISODate) << "\n"
            << "           duration: " << pp.prepaid[(int)Qt::Thursday].duration << "\n"
            << "        **** Friday **** \n"
            << "                day: " << pp.prepaid[(int)Qt::Friday].day << "\n"
            << "       static_start: " << pp.prepaid[(int)Qt::Friday].static_start.toString(Qt::ISODate) << "\n"
            << "         static_end: " << pp.prepaid[(int)Qt::Friday].static_end.toString(Qt::ISODate) << "\n"
            << "        **** Saturday **** \n"
            << "                day: " << pp.prepaid[(int)Qt::Saturday].day << "\n"
            << "       static_start: " << pp.prepaid[(int)Qt::Saturday].static_start.toString(Qt::ISODate) << "\n"
            << "         static_end: " << pp.prepaid[(int)Qt::Saturday].static_end.toString(Qt::ISODate) << "\n"
            << "           duration: " << pp.prepaid[(int)Qt::Saturday].duration << "\n"
            << "        **** Sunday **** \n"
            << "                day: " << pp.prepaid[(int)Qt::Sunday].day << "\n"
            << "       static_start: " << pp.prepaid[(int)Qt::Sunday].static_start.toString(Qt::ISODate) << "\n"
            << "         static_end: " << pp.prepaid[(int)Qt::Sunday].static_end.toString(Qt::ISODate) << "\n"
            << "           duration: " << pp.prepaid[(int)Qt::Sunday].duration << "\n";

        return debug;
    }
};

#endif // TARIFF_PREPAID_H_INCLUDED