/*******************************************************************************
***  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/>.    ***
*******************************************************************************/
#include "CurrencyConverter.h"
using std::set;
using drn::conversion::CurrencyConverter;
using drn::conversion::ConversionMap;
using drn::conversion::ConversionRatio;

#include <pecunia/Codes.h>
using pecunia::currency::Iso4217Codes;
using pecunia::currency::toStdString;

#include <utility>
using std::move;

#include "CurrencySource.hpp"
using drn::conversion::CurrencySource;
#include <foundation/Error.h>
using drn::foundation::Error;


ConversionMap CurrencyConverter::conversions_{};

CurrencyConverter::CurrencyConverter(CurrencySource& source) :
	source_{source}
{}

double CurrencyConverter::lookUp(const Iso4217Codes& from, const Iso4217Codes& to) const
{
	if ( ! CurrencyConverter::conversions_.hasConversion(from, to))
	{
		const auto conversion{this->source_.fetch(from, to)};
		CurrencyConverter::setConversion(from, to, conversion);
	}
	return CurrencyConverter::conversions_.at(from).find(to)->ratio_;
}

const ConversionMap& CurrencyConverter::conversions()
{
	return CurrencyConverter::conversions_;
}

void CurrencyConverter::setConversion(
	const Iso4217Codes& from,
	const Iso4217Codes& to,
	double ratio
)
{
	CurrencyConverter::conversions_.setConversion(from, to, ratio);
}

void CurrencyConverter::initialise(ConversionMap&& conversions)
{
	CurrencyConverter::conversions_ = move(conversions);
}

ConversionMap CurrencyConverter::fetchAll(const set<Iso4217Codes> currencies) const
{
	ConversionMap conversions;

	for (const auto& from : currencies)
		for (const auto& to : currencies)
		{
			if (from == to)
				continue;

			const auto conversion{this->source_.fetch(from, to)};
			CurrencyConverter::setConversion(from, to, conversion);
			conversions.setConversion(from, to, conversion);
		}
	return conversions;
}
