/*******************************************************************************
***  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_LEDGERSWIDGET_H_
#define DRN_DESKTOP_UI_INTERNAL_LEDGERSWIDGET_H_

#include <QObject>

#include <functional>
#include <map>
#include <set>
#include <vector>

#include <foundation/ObserverPtr.hpp>
#include <foundation/Optional.hpp>
#include <ui_LedgersWidget.h>


class QAbstractButton;
class QPushButton;


namespace pecunia
{
namespace currency
{

enum class Iso4217Codes : quint16;

}}

namespace drn
{
namespace accounting
{

class Account;
class AccountCode;
struct LedgerAccount;
class Transaction;
class TransactionNumber;

}
namespace desktop_ui
{
namespace internal
{

class TransactionWidget;

class LedgersWidget : public QWidget, Ui::LedgersWdgt
{
	Q_OBJECT

	foundation::Optional<foundation::ObserverPtr<internal::TransactionWidget>> findTransaction(
		const std::function<bool(const internal::TransactionWidget&)>& isMatch
	) const;
	bool anyLedgerDirty() const;
	void addTransaction(const accounting::Transaction& txn);
	void save();
	accounting::AccountCode selectedAccountCode() const;
	void displayTransactions(const accounting::AccountCode& ac);

	foundation::ObserverPtr<QPushButton> addPshbttn_;
	std::set<accounting::Account> accounts_;
	std::vector<accounting::AccountCode> accountCodes_;
	const std::set<pecunia::currency::Iso4217Codes> usableCurrencies_;
	/// The first entry is the transaction editable by the user, and the second is the transaction
	/// associated with the editable transaction.
	std::map<
		accounting::AccountCode,
		std::vector<foundation::ObserverPtr<TransactionWidget>>
	> entries_;

private slots:
	void onClicked(foundation::ObserverPtr<QAbstractButton> button);
	void handleSelctedAccount(
		foundation::ObserverPtr<QTreeWidgetItem> current,
		foundation::ObserverPtr<QTreeWidgetItem> previous
	);

signals:
	/// @brief Triggered when the widget is in the process of being closed.
	void closing();
	/// @brief Triggered when an account is selected.
	void selectedLedger(const accounting::AccountCode& code);
	/// @brief Triggered when a new transaction is to be posted.
	void addedTransaction(const accounting::Transaction& txn);
	/// @brief Triggered when a transaction is to be reposted.
	void updatedTransaction(
		const accounting::TransactionNumber& tn,
		const accounting::Transaction& txn
	);
	/// @brief Triggered when a transaction is to be marked as cleared.
	void markCleared(const accounting::TransactionNumber& number);
	/// @brief Triggered when a transaction is to be marked as reconciled.
	void markReconciled(const accounting::TransactionNumber& number);
	/// @brief Triggered when a transaction is selected for removal.
	void removedTransaction(const accounting::TransactionNumber& number);

public slots:
	/**
	 * @brief Loads the transactions for the supplied account ledger.
	 *
	 * @pre The supplied ledger must be the selected account.
	 *
	 * @param ledger The ledger containing the transactions.
	 */
	void onShowLedger(const accounting::LedgerAccount& ledger);
	bool close();
	void onSaveSucceeded(
		const foundation::Optional<accounting::TransactionNumber>& displayed,
		const foundation::Optional<accounting::TransactionNumber>& posted
	);
	void onSaveFailed(
		const foundation::Optional<accounting::TransactionNumber>& tn,
		const ::QString& errorMessage
	);

public:
	LedgersWidget(
		std::set<accounting::Account> accounts,
		std::set<pecunia::currency::Iso4217Codes> usableCurrencies,
		const foundation::ObserverPtr<::QWidget>& parent
	);
};

}}}

#endif
