/*******************************************************************************
***  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/>.    ***
*******************************************************************************/
#ifndef DRN_DESKTOP_UI_INTERNAL_GOALENTRYWIDGET_H_
#define DRN_DESKTOP_UI_INTERNAL_GOALENTRYWIDGET_H_

#include <pecunia/Money.h>
#include <QWidget>

#include <set>

#include <accounting/AccountCode.h>
#include <banking/BankName.h>
#include <budgeting/BudgetSource.h>
#include <foundation/CalendarTime.hpp>
#include <foundation/Optional.hpp>
#include <foundation/Switch.h>
#include <ui_GoalEntryWidget.h>


class QDate;
class QString;

namespace pecunia
{
namespace currency
{

enum class Iso4217Codes : quint16;

}}

namespace drn
{
namespace accounting
{

class AccountCode;

}
namespace banking
{

struct BankAccount;
struct BankAccountType;

}
namespace budgeting
{

enum class EventFrequency : quint8;
class Goal;

}
namespace foundation
{

template<typename>
class ObserverPtr;

}
namespace desktop_ui
{
namespace internal
{
namespace tests
{

struct GoalEntryWidgetUT;

}

/**
 * @brief A Qt Widget to display a single goal entry.
 */
class GoalEntryWidget : public ::QWidget, Ui::GoalEntryWdgt
{
	Q_OBJECT

	friend tests::GoalEntryWidgetUT;

	void updatePercentageAmount(
		const pecunia::currency::Money& goal,
		const pecunia::currency::Money& monthlyGoal
	);

	foundation::Switch isDirty_;
	foundation::Optional<budgeting::BudgetSource> original_;
	budgeting::EventFrequency previousPeriod_;
	pecunia::currency::Iso4217Codes previousCode_;
	const pecunia::currency::Money totalWage_;
	const std::chrono::hours& hrInWeek_;
	const foundation::Days& dayInWeek_;
	foundation::Optional<banking::BankName> isBankRequested_;

private slots:
	void markDirty();
	void updateByPercentage(const double value);
	void updateByAmount();
	void updateByPeriod();
	void updateByCurrency();
	void onLookUpBankAccounts(const banking::BankName& bn);

signals:
	/// @brief Triggered when the monetary value changes.
	void valueChanged();
	void lookUpBankAccounts(const banking::BankName& bn);

public:
	GoalEntryWidget(
		const budgeting::Goal& goal,
		const foundation::Optional<banking::BankAccount>& ba,
		const std::set<banking::BankName>& banks,
		const pecunia::currency::Money& totalWage,
		const std::chrono::hours& hrInWeek,
		const foundation::Days& dayInWeek,
		const std::set<pecunia::currency::Iso4217Codes>& usableCurrencies,
		const foundation::ObserverPtr<QWidget> parent
	);
	const foundation::Optional<budgeting::BudgetSource>& originalSource() const;
	budgeting::BudgetSource source() const;
	pecunia::currency::Money amount() const;
	budgeting::EventFrequency period() const;
	pecunia::currency::Money final() const;
	::QDate nextOccurOn() const;
	std::set<banking::BankName> banks() const noexcept;
	foundation::Optional<banking::BankName> bank() const noexcept;
	foundation::Optional<accounting::AccountCode> account() const noexcept;
	foundation::Optional<banking::SupportedAccountTypes> bankAccountType() const noexcept;
	bool shouldRemove() const;
	bool isDirty() const;
	/**
	 * @brief Updates the widget state to indicate that the entry are saved.
	 */
	void saved();
	void setErrorMessage(const ::QString& message);
	void resetDirty();
	void setAccountCodes(std::set<banking::BankAccountType> accountTypes);
	bool isRequested(const banking::BankName& bn) const;
};

}}}

#endif
