/*******************************************************************************
***  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 <pecunia/Codes.h>
using pecunia::currency::Iso4217Codes;
#include <pecunia/Money.h>
using pecunia::currency::Money;
#include <QTest>
// using GTEST_MAIN

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

#include <budgeting/Algorithm.h>
using drn::budgeting::adjustTo;
using drn::budgeting::convert;
#include <budgeting/EventFrequency.h>
using drn::budgeting::allEventFrequencies;
using drn::budgeting::EventFrequency;
#include <foundation/Error.h>
using drn::foundation::Error;


namespace drn
{
namespace budgeting
{
namespace tests
{

struct AlgorithmUT : QObject
{
	Q_OBJECT

private slots:
	//{ convert Tests

	void convert_FromHourlyToAll_ShouldThrow()
	{
		const auto from{EventFrequency::Unknown};

		for (const auto& to : allEventFrequencies)
			QVERIFY_EXCEPTION_THROWN(convert(from, to), Error);
	}
	void convert_FromHourlyToUnknown_ShouldBeExpected()
	{
		const auto from{EventFrequency::Hourly};
		const auto to{EventFrequency::Unknown};
		QVERIFY_EXCEPTION_THROWN(convert(from, to), Error);
	}
	void convert_FromHourlyToHourly_ShouldBeExpected()
	{
		const auto from{EventFrequency::Hourly};
		const auto to{EventFrequency::Hourly};
		const double actual{convert(from, to)};
		QCOMPARE(actual, 1.00);
	}
	void convert_FromHourlyToDaily_ShouldBeExpected()
	{
		const auto from{EventFrequency::Hourly};
		const auto to{EventFrequency::Daily};
		const double actual{convert(from, to)};
		QCOMPARE(actual, 1 / 24.0);
	}
	void convert_FromHourlyToWeekly_ShouldBeExpected()
	{
		const auto from{EventFrequency::Hourly};
		const auto to{EventFrequency::Weekly};
		const double actual{convert(from, to)};
		QCOMPARE(actual, 1 / 168.0);
	}
	void convert_FromHourlyToMonthly_ShouldBeExpected()
	{
		const auto from{EventFrequency::Hourly};
		const auto to{EventFrequency::Monthly};
		const double actual{convert(from, to)};
		QCOMPARE(actual, 1 / (168.0 * 52.0 / 12.0));
	}
	void convert_FromHourlyToAnnually_ShouldBeExpected()
	{
		const auto from{EventFrequency::Hourly};
		const auto to{EventFrequency::Annually};
		const double actual{convert(from, to)};
		QCOMPARE(actual, 1 / (168.0 * 52.0));
	}
	void convert_FromHourlyToNever_ShouldBeZero()
	{
		const auto from{EventFrequency::Hourly};
		const auto to{EventFrequency::Never};
		const double actual{convert(from, to)};
		QCOMPARE(actual, 0.0);
	}

	//}

	//{ adjustTo Tests

	void adjustTo_FromHourlyToUnknown_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(
			adjustTo(EventFrequency::Unknown, Money(10, 1500u, Iso4217Codes::PLN), EventFrequency::Hourly),
			Error
		);
	}
	void adjustTo_FromHourlyToHourly_ShouldMatch()
	{
		const Money actual{
			adjustTo(EventFrequency::Hourly, Money{10, 1500u, Iso4217Codes::PLN}, EventFrequency::Hourly)
		};
		const Money expected{10, 1500u, Iso4217Codes::PLN};
		QCOMPARE(actual, expected);
	}
	void adjustTo_FromHourlyToMonthly_ShouldBeAdjusted()
	{
		const Money actual{
			adjustTo(EventFrequency::Monthly, Money{10, 1500u, Iso4217Codes::PLN}, EventFrequency::Hourly)
		};
		const Money expected{7389, 2000u, Iso4217Codes::PLN};
		QCOMPARE(actual, expected);
	}
	void adjustTo_FromUnknownToHourly_ShouldThrow()
	{
		QVERIFY_EXCEPTION_THROWN(
			adjustTo(EventFrequency::Hourly, Money(10, 1500u, Iso4217Codes::PLN), EventFrequency::Unknown),
			Error
		);
	}
	void adjustTo_FromHourlyToNever_ShouldBeZero()
	{
		const Money actual{
			adjustTo(EventFrequency::Never, Money{10, 1500u, Iso4217Codes::PLN}, EventFrequency::Hourly)
		};
		const Money expected{0, 0u, Iso4217Codes::PLN};
		QCOMPARE(actual, expected);
	}
	void adjustTo_FromNeverToHourly_ShouldBeAdjusted()
	{
		const Money actual{
			adjustTo(EventFrequency::Hourly, Money{10, 1500u, Iso4217Codes::PLN}, EventFrequency::Never)
		};
		const Money expected{10, 1500u, Iso4217Codes::PLN};
		QCOMPARE(actual, expected);
	}

	//}
};

}}}

QTEST_MAIN(drn::budgeting::tests::AlgorithmUT)
#include "Algorithm-unit-tests.moc"
