/*******************************************************************************
***  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_ACCOUNTING_ACCOUNTNUMBERGENERATOR_H_
#define DRN_ACCOUNTING_ACCOUNTNUMBERGENERATOR_H_

#include <memory>
#include <set>

#include "accounting_export.h"
#include "AccountCode.h"


namespace drn
{
namespace accounting
{

class Account;
enum class AccountTypes : quint8;

/**
 * @brief Generalised generator for account code numbers.
 */
class DRN_ACCOUNTING_EXPORT AccountNumberGenerator
{
	AccountNumber number_;

protected:
	/**
	 * @brief When implemented produces the next account code number.
	 *
	 * @exception Error When the next account code number cannot be generated.
	 *
	 * @return The next account code number generated.
	 */
	virtual AccountNumber increment() const =0;
	/**
	 * @brief When implemented produces the previous account code number.
	 *
	 * @exception Error When the previous account code number cannot be generated.
	 *
	 * @return The previous account code number generated.
	 */
	virtual AccountNumber decrement() const =0;
	/**
	 * @brief An accessor to the current account code number.
	 *
	 * @return The current account code number.
	 */
	const AccountNumber& number() const;
	/**
	 * @brief When implemented, determines if two account numbers are larger than the other.
	 *
	 * @param an The account number to compare against the current account number.
	 *
	 * @return True when the account number is considered to be larger than the current account
	 * number.
	 */
	virtual bool isLarger(const AccountNumber& an) const =0;

public:
	/**
	 * @brief Constructor for fully initialising the generator.
	 *
	 * @param number The current account code number that is used.
	 */
	explicit AccountNumberGenerator(AccountNumber number = {});
	AccountNumberGenerator(const AccountNumberGenerator&) = default;
	AccountNumberGenerator(AccountNumberGenerator&&) noexcept = default;
	virtual ~AccountNumberGenerator() = default;
	AccountNumberGenerator& operator=(const AccountNumberGenerator&) = default;
	AccountNumberGenerator& operator=(AccountNumberGenerator&&) noexcept = default;
	/**
	 * @brief Prefix incremental operator to advance to the next account code number.
	 *
	 * @exception Error When the next account code number cannot be generated.
	 *
	 * @return The next generated account code number.
	 */
	const AccountNumber& operator++();
	/**
	 * @brief Postfix incremental operator to advance to the next account code number.
	 *
	 * @exception Error When the next account code number cannot be generated.
	 *
	 * @return The current generated account code number.
	 */
	AccountNumber operator++(int);
	/**
	 * @brief Prefix decremental operator to go back to the previous account code number.
	 *
	 * @exception Error When the previous account code number cannot be generated.
	 *
	 * @return The current generated account code number.
	 */
	const AccountNumber& operator--();
	/**
	 * @brief Postfix decremental operator to go back to the previous account code number.
	 *
	 * @exception Error When the previous account code number cannot be generated.
	 *
	 * @return The previous generated account code number.
	 */
	AccountNumber operator--(int);
	/**
	 * @brief Dereference operator to the current generated account code number.
	 */
	const AccountNumber& operator*() const;
	/**
	 * @brief Assignment operator for setting the current generated account code number.
	 *
	 * @param number The number to set the account code number generated to.
	 *
	 * @return The current instance.
	 */
	AccountNumberGenerator& operator=(const AccountNumber& number);
	/**
	 * @brief When implemented, finds the current largest account code number relevant to the
	 * account code generator in the account listings.
	 *
	 * @param accounts The current listing of all the accounts in the account ledger.
	 *
	 * @return The current highest account code number found, or default empty if there is not any.
	 */
	virtual AccountNumber current(const std::set<Account>& accounts) const =0;
	/**
	 * @brief When implemented, will clone all data members of the object.
	 *
	 * @return The cloned object.
	 */
	virtual std::unique_ptr<AccountNumberGenerator> clone() const =0;
	/**
	 * @brief Updates the current account number to the supplied number when it is larger.
	 *
	 * @param an The account number to update to if it is larger than the current account number
	 * value.
	 */
	void update(AccountNumber an);
};
/**
 * @brief An account code number generator that creates values sequentially.
 */
class DRN_ACCOUNTING_EXPORT SequentialAccountNumberGenerator : public AccountNumberGenerator
{
	AccountNumber increment() const override;
	AccountNumber decrement() const override;
	bool isLarger(const AccountNumber& an) const override;

public:
	using AccountNumberGenerator::AccountNumberGenerator;
	using AccountNumberGenerator::operator=;

	/**
	 * @brief Finds the current largest account code number.
	 *
	 * @param accounts The current listing of all the accounts in the account ledger.
	 * @param type Ignored.
	 *
	 * @return The current highest account code number found, or zero if there is not any.
	 */
	AccountNumber current(const std::set<Account>& accounts) const override;
	std::unique_ptr<AccountNumberGenerator> clone() const override;
};

}}

#endif
