#include <QDebug>
#include <QDateTime>

#include <assert.h>

#include "calculate_parking_tickets.h"
#include "calculate_parking_tickets_global.h"
#include "calculate_parking_tickets_algorithms.h"
#include "calculate_parking_tickets_tariff.h"
#include "calculate_parking_tickets_utils.h"
#include "message_handler.h"

int main() {

    if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling
      atbInstallMessageHandler(atbDebugOutput);
      setDebugLevel(QtMsgType::QtDebugMsg);
    }

    parking_tariff_t *tariff =
        //parking_tariff_t::parseTariff("/opt/ptu5/opt/calculate_parking/calculate_parking_tickets/germersheim.conf");
        parking_tariff_t::parseTariff("/opt/ptu5/opt/calculate_parking/calculate_parking_tickets/apcoa_prio_1.conf");

    if (tariff) {
        price_t price;
        memset(&price, 0x00, sizeof(price));
        QDateTime start = QDateTime::currentDateTime();
        QDateTime st = start;
        QDateTime end, tmp;
        start.setTime(QTime());
        int d1, d2 = 0;

        d1 = start.date().dayOfWeek();

        int hour[] = { 2, 5, 10, 20, 30, 40};

        for (int h = 0; h < 6; ++h) {
            for (int i = 0; i < MIN_PER_DAY; ++i) {
                //int __x = i*(h+1);
                //if (__x == 3060) {
                fprintf(stderr, "\n*** %d:%d *** (parkdauer:%dh)", h, i, hour[h]);

                if (d1 != d2) {
                    qDebug() << ((i==0) ? 0: i/MIN_PER_DAY) << d1 << "***\n";
                    d2 = d1;
                }

                //start = start.addSecs(i*60);
                //start = start.addSecs(420*60);
                minute_t const s = TUtils::toMinutes(start);
                tmp = start;
                end = tmp.addSecs(hour[h]*60*60);
                minute_t const e = TUtils::toMinutes(end);
                //minute_t const e = s + 24*60*3 + 123;

                fprintf(stderr,
                    "[EINFAHRT: %s:%02d:%02d, (day:%ld,night:%ld) Minuten frei, "
                    "AUSFAHRT: %s:%02d:%02d]\n",
                    start.date().toString().toStdString().c_str(),
                    start.time().hour(), start.time().minute(),
                    tariff->free_of_charge_day_tariff,
                    tariff->free_of_charge_night_tariff,
                    end.date().toString().toStdString().c_str(),
                    end.time().hour(), end.time().minute());

                if (compute_price_for_parking_ticket(tariff, s, e, &price)) {

                }
                //}
                start = start.addSecs(60);
                d1 = start.date().dayOfWeek();
            }
            start = st;
            d1 = start.date().dayOfWeek();
        }
    }

    return 0;
}
