/*******************************************************************************
***  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_BANKING_BANKMAP_H_
#define DRN_BANKING_BANKMAP_H_

#include <iosfwd>
#include <map>
#include <set>

#include "Bank.h"
#include "banking_export.h"


namespace drn
{
namespace accounting
{

class AccountNumber;

}
namespace foundation
{

template<typename>
class Optional;

}
namespace banking
{

/**
 * @brief A representation of financial institutions mapped to their names.
 */
struct DRN_BANKING_EXPORT BankMap : private std::map<BankName, Bank>
{
	friend DRN_BANKING_EXPORT std::ostream& operator<<(std::ostream&, const BankMap&);
	friend DRN_BANKING_EXPORT bool operator==(const BankMap& lhs, const BankMap& rhs);

	using std::map<BankName, Bank>::begin;
	using std::map<BankName, Bank>::cbegin;
	using std::map<BankName, Bank>::cend;
	using std::map<BankName, Bank>::clear;
	using std::map<BankName, Bank>::const_iterator;
	using std::map<BankName, Bank>::empty;
	using std::map<BankName, Bank>::end;
	using std::map<BankName, Bank>::size;

	BankMap() = default;
	/**
	 * @brief Full initialisation constructor.
	 *
	 * @exception BankError When a financial institution name key does not match its name.
	 * @exception BankError When a financial institution uses a general ledger account number used
	 * by another financial institution.
	 * @exception BankError When a financial institution uses the a default ledger account.
	 *
	 * @param banks The financial institutions.
	 */
	explicit BankMap(std::map<BankName, Bank> banks);
	/**
	 * @brief An accessor to the bank via its financial institution name.
	 *
	 * @exception BankError When the financial institution name does not exist.
	 *
	 * @param bn The name of the financial institution to look-up.
	 *
	 * @return The requested financial institution.
	 */
	const Bank& bank(const BankName& bn) const;
	/**
	 * @brief Adds a new financial institution to the container.
	 *
	 * @exception BankError When the financial institution is already closed.
	 * @exception BankError When the financial institution is already present in the container.
	 *
	 * @param b The financial institution to add.
	 */
	void add(Bank b);
	/**
	 * @brief Adds an associated account to the financial institution.
	 *
	 * @exception BankError When the financial institution does not exist.
	 * @exception BankError When the account number is already associated with another financial
	 * institution.
	 *
	 * @param name The name of the financial institution.
	 * @param number The account number to add the association.
	 */
	void add(const BankName& name, const accounting::AccountNumber& number);
	/**
	 * @brief Determines if a financial institution is in a container.
	 *
	 * @param name The name of the financial institution to check.
	 *
	 * @return Gives true when the institution is present, false otherwise.
	 */
	bool hasBank(const BankName& name) const;
	/**
	 * @brief Finds the bank that is associated with a given account number.
	 *
	 * @param number The account number to find the associated bank.
	 *
	 * @return Gives the found associated bank, or an empty optional.
	 */
	foundation::Optional<Bank> find(const accounting::AccountNumber& number) const;
	/**
	 * @brief Marks the financial institution as closed for usage.
	 *
	 * @exception BankError When the financial institution does not exist.
	 *
	 * @param name The name of the financial institution.
	 */
	void close(const BankName& name);
	/**
	 * @brief Marks the financial institution as opened for usage.
	 *
	 * @exception BankError When the financial institution does not exist.
	 *
	 * @param name The name of the financial institution.
	 */
	void open(const BankName& name);
	/**
	 * @brief Removes the financial institution from the container.
	 *
	 * @exception BankError When the financial institution does not exist.
	 *
	 * @param name The name of the financial institution.
	 */
	void remove(const BankName& name);
	/**
	 * @brief Removes an associated account from the financial institution.
	 *
	 * @exception BankError When the financial institution does not exist.
	 * @exception BankError When the financial institution does not have the account.
	 *
	 * @param name The name of the financial institution.
	 * @param number The account number to remove the association.
	 */
	void remove(const BankName& name, const accounting::AccountNumber& number);
	/**
	 * @brief Renames the financial institution in the container.
	 *
	 * @exception BankError When the financial institution name to be changed does not exist.
	 * @exception BankError When the financial institution name to be changed and to be changed
	 * into are the same.
	 * @exception BankError When the financial institution name to change to already exists.
	 *
	 * @param original The name of the financial institution.
	 * @param changed The changed name of the financial institution.
	 */
	void rename(const BankName& original, const BankName& changed);
	/**
	 * @brief Updates the financial institution information.
	 *
	 * @exception BankError When the financial institution does not exist.
	 * @exception BankError When the financial institution to update is closed.
	 * @exception BankError When the financial institution's associated account ledger is already
	 * associated with another financial institution.
	 *
	 * @param b The financial institution to update.
	 */
	void update(Bank b);
	/**
	 * @brief Collects all the known bank names.
	 */
	std::set<banking::BankName> names() const;
};

DRN_BANKING_EXPORT bool operator==(const BankMap& lhs, const BankMap& rhs);
DRN_BANKING_EXPORT bool operator!=(const BankMap& lhs, const BankMap& rhs);
DRN_BANKING_EXPORT std::ostream& operator<<(std::ostream& o, const BankMap& bm);

}}

#endif
