/*******************************************************************************
***  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 <pecunia/MoneyTypes.h>
using pecunia::converter;
using pecunia::defaultConverter;
#include <QDate>
using ::QDate;
#include <QTest>
// using QTEST_MAIN
#include <qt-mocks/Matchers.h>
using qt_mocks::equals;
// using QVERIFY_THAT;

#include <budgeting/BudgetedMoney.h>
using drn::budgeting::BudgetedMoney;
#include <budgeting/BudgetingErrors.h>
using drn::budgeting::BudgetItemError;
#include <budgeting/BudgetSource.h>
using drn::budgeting::BudgetSource;
#include <budgeting/EventFrequency.h>
using drn::budgeting::EventFrequency;
#include <foundation/ScopeExecutor.h>
using drn::foundation::ScopeExecutor;
#include <foundation/Switch.h>
using drn::foundation::SwitchStates;


namespace drn
{
namespace budgeting
{
namespace tests
{

struct BudgetedMoneyUT : QObject
{
	Q_OBJECT

private slots:
	//{ Constructor Tests

	void constructor_Default_ShouldSet()
	{
		const BudgetSource source{};
		const Money amount{0, 0, Iso4217Codes::XXX};
		const EventFrequency period{EventFrequency::Unknown};
		const QDate nextOccurOn{};
		const BudgetedMoney money{};
		QVERIFY_THAT(money.source(), equals(source));
		QVERIFY_THAT(money.amount(), equals(amount));
		QVERIFY_THAT(money.period(), equals(period));
		QVERIFY_THAT(money.nextOccurOn(), equals(nextOccurOn));
	}
	void constructor_ValidParams_ShouldSet()
	{
		const BudgetSource source{"Source Name 0"};
		const Money amount{200, 00, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{9999, 6, 20};
		const BudgetedMoney money{source, amount, period, nextOccurOn};
		QVERIFY_THAT(money.source(), equals(source));
		QVERIFY_THAT(money.amount(), equals(amount));
		QVERIFY_THAT(money.period(), equals(period));
		QVERIFY_THAT(money.nextOccurOn(), equals(nextOccurOn));
	}
	void constructor_InvalidSource_ShouldThrow()
	{
		const BudgetSource source{};
		const Money amount{200, 00, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{9999, 6, 20};
		QVERIFY_EXCEPTION_THROWN(
			BudgetedMoney(source, amount, period, nextOccurOn),
			BudgetItemError
		);
	}
	void constructor_InvalidAmount_LessThanZero_ShouldThrow()
	{
		const BudgetSource source{"Source Name 0"};
		const Money amount{-200, 00, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{9999, 6, 20};
		QVERIFY_EXCEPTION_THROWN(
			BudgetedMoney(source, amount, period, nextOccurOn),
			BudgetItemError
		);
	}
	void constructor_InvalidAmount_UnknownCurrency_ShouldThrow()
	{
		const BudgetSource source{"Source Name 0"};
		const Money amount{200, 00, Iso4217Codes::XXX};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{9999, 6, 20};
		QVERIFY_EXCEPTION_THROWN(
			BudgetedMoney(source, amount, period, nextOccurOn),
			BudgetItemError
		);
	}
	void constructor_InvalidPeriod_ShouldThrow()
	{
		const BudgetSource source{"Source Name 0"};
		const Money amount{200, 00, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Unknown};
		const QDate nextOccurOn{9999, 6, 20};
		QVERIFY_EXCEPTION_THROWN(
			BudgetedMoney(source, amount, period, nextOccurOn),
			BudgetItemError
		);
	}
	void constructor_InvalidNextOccurDate_ShouldThrow()
	{
		const BudgetSource source{"Source Name 0"};
		const Money amount{200, 00, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{};
		QVERIFY_EXCEPTION_THROWN(
			BudgetedMoney(source, amount, period, nextOccurOn),
			BudgetItemError
		);
	}

	//}

	//{ setSource Tests

	void setSource_ValidParams_ShouldSet()
	{
		const BudgetSource source{"Source Name 0"};
		const Money amount{200, 00, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{9999, 6, 20};
		BudgetedMoney money{source, amount, period, nextOccurOn};
		const BudgetSource newSource{"Excel"};
		money.setSource(newSource);
		QCOMPARE(money.source(), newSource);
	}
	void setSource_InvalidSource_ShouldThrow()
	{
		const BudgetSource source{"Source Name 0"};
		const Money amount{200, 00, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{9999, 6, 20};
		BudgetedMoney money{source, amount, period, nextOccurOn};
		const BudgetSource newSource{};
		QVERIFY_EXCEPTION_THROWN(money.setSource(newSource), BudgetItemError);
	}

	//}

	//{ updateNextOccurOn Tests

	void updateNextOccurOn_MonthlyPeriod_ShouldTouch()
	{
		const BudgetSource source{"Source Name 0"};
		const Money amount{200, 00, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{9999, 11, 8};
		BudgetedMoney money{source, amount, period, nextOccurOn};
		QVERIFY_NO_THROW(money.updateNextOccurOn());
		const QDate expected{9999, 12, 8};
		QVERIFY_THAT(money.nextOccurOn(), equals(expected));
	}

	void updateNextOccurOn_UnknownPeroid_ShouldThrow()
	{
		BudgetedMoney money{};
		QVERIFY_THAT(money.period(), equals(EventFrequency::Unknown));
		QVERIFY_EXCEPTION_THROWN(money.updateNextOccurOn(), BudgetItemError);
	}

	//}
};

}}}

QTEST_MAIN(drn::budgeting::tests::BudgetedMoneyUT)
#include "BudgetedMoney-unit-tests.moc"
