/*******************************************************************************
***  This file is part of Dux Rei Nummariae.                                 ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Affero General Public License as published   ***
***  by the Free Software Foundation, either version 3 of the License, or    ***
***  (at your option) any later version.                                     ***
***                                                                          ***
***  This program is distributed in the hope that it will be useful, but     ***
***  WITHOUT ANY WARRANTY; without even the implied warranty of              ***
***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    ***
***  See the GNU Affero General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Affero General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include <qt-mocks/Matchers.h>
using qt_mocks::equals;
// using QVERIFY_NO_THROW;
// using QVERIFY_THAT;
#include <QDate>
using ::QDate;
#include <QDateTime>
using ::QDateTime;
#include <QTime>
using ::QTime;
#include <QTest>
// using GTEST_MAIN

#include <chrono>
using namespace std::chrono_literals;
#include <numeric>
using std::numeric_limits;

#include <budgeting/EventFrequency.h>
using drn::budgeting::EventFrequency;
using drn::budgeting::nextTimePoint;
#include <foundation/Error.h>
using drn::foundation::Error;


namespace drn
{
namespace budgeting
{
namespace tests
{

struct EventFrequencyUT : QObject
{
	Q_OBJECT

private slots:
	void nextTimePoint_InvalidCurrentPoint_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(nextTimePoint(QDateTime{}, EventFrequency::Daily), Error);
	}

	void nextTimePoint_UnknownParams_ShouldThrow()
	{
		const QDateTime tp{QDate{2020, 11, 8}, QTime{17, 00}};
		QVERIFY_EXCEPTION_THROWN(nextTimePoint(tp, EventFrequency::Unknown), Error);
	}

	void nextTimePoint_HourlyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2020, 11, 8}, QTime{18, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Hourly));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_DailyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2020, 11, 9}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Daily));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_SemiWeeklyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2020, 11, 12}, QTime{5, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::SemiWeekly));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_WeeklyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2020, 11, 15}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Weekly));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_BiWeeklyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2020, 11, 22}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::BiWeekly));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_SemiMonthlyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2020, 11, 23}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::SemiMonthly));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_MonthlyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2020, 12, 8}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Monthly));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_BiMonthlyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2021, 1, 8}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::BiMonthly));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_QuarterlyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2021, 2, 8}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Quarterly));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_SemiAnnuallyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2021, 5, 8}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::SemiAnnually));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_AnnuallyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2021, 11, 8}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Annually));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_BiannuallyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2022, 11, 8}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Biannually));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_TriannuallyParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{QDate{2023, 11, 8}, QTime{17, 00}};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Triannually));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

	void nextTimePoint_NeverParams_ShouldCalculate()
	{
		const QDateTime current{QDate{2020, 11, 8}, QTime{17, 00}};
		const QDateTime expected{
			QDate{numeric_limits<qint32>::max(), 12, 31},
			QTime{23, 59, 59, 9999ull}
		};
		QDateTime next;
		QVERIFY_NO_THROW(next = nextTimePoint(current, EventFrequency::Never));
		QVERIFY_THAT(next.toString(), equals(expected.toString()));
	}

};

}}}

QTEST_MAIN(drn::budgeting::tests::EventFrequencyUT)
#include "EventFrequency-unit-tests.moc"
