/*******************************************************************************
***  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_ACCOUNTSWIDGET_H_
#define DRN_DESKTOP_UI_INTERNAL_ACCOUNTSWIDGET_H_

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

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

#include <accounting/AccountCode.h>
#include <foundation/ObserverPtr.hpp>
#include <foundation/Optional.hpp>
#include <foundation/Switch.h>
#include <ui_AccountsWidget.h>


class QAbstractButton;
class QListWidgetItem;
class QPushButton;
class QString;

namespace drn
{
namespace accounting
{

class Account;

}
namespace desktop_ui
{
namespace internal
{

class AccountEntryWidget;

/**
 * @brief Displays an interface for creating, editing, and deleting accounts.
 */
class AccountsWidget : public QWidget, Ui::AccountsWdgt
{
	Q_OBJECT

	void insertAccountEntry(const foundation::Optional<accounting::Account>& a);
	void displayAccount(foundation::ObserverPtr<QListWidgetItem> account);
	foundation::Optional<foundation::ObserverPtr<AccountEntryWidget>> findAccountEntry(
		const std::function<bool(const AccountEntryWidget&)>& isMatch
	);
	foundation::Optional<foundation::ObserverPtr<const AccountEntryWidget>> findAccountEntry(
		const std::function<bool(const AccountEntryWidget&)>& isMatch
	) const;
	void save();
	bool anyAccountDirty() const;
	void removeAccountCode(const ::QString& accountCode);

	std::set<accounting::AccountNumber> accountNumbers_;
	const std::set<pecunia::currency::Iso4217Codes> usableCurrencies_;
	foundation::ObserverPtr<QPushButton> addPshbttn_;

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

public slots:
	void accountSelectionChanged(
		foundation::ObserverPtr<QListWidgetItem> current,
		foundation::ObserverPtr<QListWidgetItem> previous
	);
	/**
	 * @brief Will close the widget and emit the closing signal.
	 *
	 * @return When the widget is closed gives true, false otherwise.
	 */
	bool close();
	void onSaveSucceeded(const accounting::AccountCode& ac);
	void onSaveFailed(
		const foundation::Optional<accounting::AccountCode>& original,
		const ::QString& errorMessage,
		const foundation::Optional<accounting::AccountCode>& displayed
	);


signals:
	/// @brief Triggered when the widget is in the process of being closed.
	void closing();
	/**
	 * @brief Triggered when the account should be removed.
	 *
	 * @param ac The account ac of the account to remove.
	 */
	void removed(const accounting::AccountCode& ac);
	/**
	 * @brief Triggered when the account should be added.
	 *
	 * @param account The account that is to be added.
	 */
	void added(const accounting::Account& a);
	/**
	 * @brief Triggered when the account should be closed.
	 *
	 * @param ac The account ac of the account to close.
	 */
	void closed(const accounting::AccountCode& ac);
	/**
	 * @brief Triggered when the account should be opened.
	 *
	 * @param ac The account ac of the account to reopen.
	 */
	void opened(const accounting::AccountCode& ac);
	/**
	 * @brief Triggered when the account ac should be reacd.
	 *
	 * @param current The current account ac of the account.
	 * @param changed The account ac to have the account changed to.
	 */
	void recoded(const accounting::AccountCode& current, const accounting::AccountCode& changed);

public:
	/**
	 * @brief Fully initialises the widget and displays the supplied information.
	 *
	 * @param accounts The current state of the accounts in the accounting system.
	 * @param usableCurrencies The possible usable currencies in the application.
	 * @param parent The parent widget.
	 */
	AccountsWidget(
		std::set<accounting::Account> accounts,
		std::set<pecunia::currency::Iso4217Codes> usableCurrencies,
		const foundation::ObserverPtr<::QWidget>& parent
	);
};

}}}

#endif

