/*******************************************************************************
***  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_POSTBUDGETITEMSWIDGET_H_
#define DRN_DESKTOP_UI_INTERNAL_POSTBUDGETITEMSWIDGET_H_

#include <pecunia/Codes.h>
#include <QDate>
#include <QWidget>

#include <exception>
#include <functional>
#include <set>
#include <vector>

#include <banking/BankName.h>
#include <budgeting/BudgetSource.h>
#include <foundation/ObserverPtr.hpp>
#include <foundation/Optional.hpp>
#include <ui_PostBudgetItemsWidget.h>


class QPushButton;

namespace drn
{
namespace accounting
{

class AccountCode;
class TransactionNumber;

}
namespace banking
{

struct BankAccountType;

}
namespace budgeting
{

struct BudgetItemDue;
struct BudgetItemIdentifier;
enum class BudgetItemTypes : quint8;

}
namespace desktop_ui
{
namespace internal
{

class PostBudgetItemEntryWidget;

// TODO: When an unexpected budget item is added the total stops being calculated.
class PostBudgetItemsWidget : public QWidget, Ui::PostBudgetItemsWdgt
{
	Q_OBJECT

	foundation::Optional<
		foundation::ObserverPtr<internal::PostBudgetItemEntryWidget>
	> findPostBudgetItem(
		const std::function<bool(const internal::PostBudgetItemEntryWidget&)>& isMatch
	);
	foundation::Optional<
		foundation::ObserverPtr<const internal::PostBudgetItemEntryWidget>
	> findPostBudgetItem(
		const std::function<bool(const internal::PostBudgetItemEntryWidget&)>& isMatch
	) const;
	void add(const foundation::Optional<budgeting::BudgetItemDue>& item);
	void processBudgetItemsDue();
	bool hasDirtyEntry() const;

	const std::set<banking::BankName> banks_;
	const std::set<pecunia::currency::Iso4217Codes> usableCurrencies_;
	foundation::ObserverPtr<::QPushButton> addPshbttn_;

private slots:
	void onClicked(foundation::ObserverPtr<QAbstractButton> button);
	void updateTotals();

public slots:
	bool close();
	void onLookedUpBankAccounts(
		const banking::BankName& bn,
		const std::set<banking::BankAccountType>& accountTypes
	);
	void onLookUpBudgetItemDueSources(
		const budgeting::BudgetItemIdentifier& requestedBy,
		const budgeting::BudgetItemTypes& bit,
		std::set<drn::budgeting::BudgetSource> sources
	);
	void onSaveSucceeded(
		const budgeting::BudgetItemIdentifier& bii,
		const foundation::Optional<accounting::TransactionNumber>& txnNum
	);
	void onSaveFailed(
		const budgeting::BudgetItemIdentifier& bii,
		const ::QString& errorMessage
	);

signals:
	/**
	 * @brief Triggered when the widget is in the process of being closed.
	 */
	void closing();
	/**
	 * @brief Triggered when the user selects a bank account.
	 *
	 * @param name The name of the bank whose accounts are desired.
	 */
	void lookUpBankAccounts(const banking::BankName& bn);
	void lookUpBudgetItemDueSources(
		const budgeting::BudgetItemIdentifier& requestedBy,
		const budgeting::BudgetItemTypes& bit
	);
	void posted(
		const budgeting::BudgetItemDue& bid,
		const accounting::AccountCode& ac,
		const ::QDate& postOn
	);
	void postedUnexpected(
		const budgeting::BudgetItemIdentifier& bii,
		const pecunia::currency::Money& amount,
		const accounting::AccountCode& ac,
		const ::QDate& postOn
	);
	void skipped(const budgeting::BudgetItemIdentifier& bii);

public:
	PostBudgetItemsWidget(
		const qint32& dueWithin,
		const std::vector<budgeting::BudgetItemDue>& itemsDue,
		const std::set<banking::BankName>& banks,
		const pecunia::currency::Iso4217Codes& preferred,
		const std::set<pecunia::currency::Iso4217Codes>& usableCurrencies,
		const foundation::ObserverPtr<::QWidget>& parent
	);
};

}}}

#endif
