/*******************************************************************************
***  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_CONVERSION_CURRENCYCONVERTER_H_
#define DRN_CONVERSION_CURRENCYCONVERTER_H_

#include <QtGlobal>

#include <set>

#include "ConversionMap.h"
#include "conversion_export.h"


namespace pecunia
{
namespace currency
{

enum class Iso4217Codes : quint16;

}}

namespace drn
{
namespace conversion
{

struct CurrencySource;

/**
 * @brief Handles the conversion between currencies. Built in is a caching mechanism to manage
 * multiple requests for the same conversion.
 */
class DRN_CONVERSION_EXPORT CurrencyConverter
{
	CurrencySource& source_;
	static ConversionMap conversions_;

public:
	/**
	 * @brief Fully initialises the object.
	 *
	 * @param source The object that will supply conversion information should it not be stored
	 * locally.
	 */
	CurrencyConverter(CurrencySource& source);
	/**
	* @brief Supplies a conversion ratio for going from a given currency to another.
	*
	* @exception Error When either the from or to currency codes are not supported.
	*
	* @param from The currency to convert from.
	* @param to The currency to convert to.
	*
	* @return The ratio that when multiplied will take the value of "from" to the value of "to".
	*/
	double lookUp(
		const pecunia::currency::Iso4217Codes& from,
		const pecunia::currency::Iso4217Codes& to
	) const;
	/**
	 * @brief Provides access to all the cached currency conversions.
	 *
	 * @return The cached currency conversions.
	 */
	static const ConversionMap& conversions();
	/**
	 * @brief Adds or updates the conversion ratio for going from one currency to another.
	 *
	 * @param from The currency to convert from.
	 * @param to The currency to convert to.
	 * @param ratio The conversion ratio that gets you from the supplied currency to the supplied
	 * other currency.
	 */
	static void setConversion(
		const pecunia::currency::Iso4217Codes& from,
		const pecunia::currency::Iso4217Codes& to,
		double ratio
	);
	/**
	 * @brief initialises the cached conversion ratios.
	 *
	 * @param conversions The values to initialise the cache with.
	 */
	static void initialise(ConversionMap&& conversions);
	/**
	 * @brief Fetches all the supplied currency conversions for every direction.
	 *
	 * @param currencies The currencies whose conversions for going from one to the other should
	 * be fetched.
	 *
	 * @return All the conversion ratios requested.
	 */
	ConversionMap fetchAll(const std::set<pecunia::currency::Iso4217Codes> currencies) const;
};

}}
#endif
