/*******************************************************************************
***  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 "Goal.h"
using ::QDate;
using pecunia::currency::Money;
using drn::budgeting::BudgetedMoney;
using drn::budgeting::BudgetSource;
using drn::budgeting::EventFrequency;
using drn::budgeting::Goal;

#include <QObject>
using ::QObject;

#include <ostream>
using std::ostream;

#include "BudgetingErrors.h"
using drn::budgeting::BudgetItemError;


Goal::Goal() = default;

Goal::Goal(const BudgetSource& source) :
	BudgetedMoney{source},
	final_{}
{}

Goal::Goal(
	const BudgetSource& source,
	const Money& amount,
	const EventFrequency& period,
	const QDate& nextOccurOn,
	const Money& final
) :
	BudgetedMoney{source, amount, period, nextOccurOn},
	final_{final}
{
	if (this->amount().code() != this->final_.code())
		throw BudgetItemError{
			this->source(),
			QObject::tr(
				"The issued currency for the goal period amount and the final goal amount must "
					"be the same."
			)
		};

	if (this->amount() > this->final_)
		throw BudgetItemError{
			this->source(),
			QObject::tr("The goal period amount must be less than the final goal amount.")
		};
}

const Money& Goal::final() const
{
	return this->final_;
}

bool Goal::operator==(const Goal& other) const
{
	return BudgetedMoney::operator==(other) && this->final_ == other.final_;
}

ostream& drn::budgeting::operator<<(ostream& o, const Goal& g)
{
	return o << dynamic_cast<const BudgetedMoney&>(g) << ", Final Amount(" << g.final() << ')';
}
