/*******************************************************************************
***  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 "CurrencyCodeWidget.h"
using std::set;
using drn::desktop_ui::CurrencyCodeWidget;

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

#include <exception>
using std::exception;

#include "DesktopUiErrors.h"
using drn::desktop_ui::DataEntryError;
#include <foundation/ObserverPtr.hpp>
using drn::foundation::ObserverPtr;


CurrencyCodeWidget::CurrencyCodeWidget(const ObserverPtr<QWidget>& parent) :
	QComboBox{parent.observed()}
{}

Iso4217Codes CurrencyCodeWidget::currentCode() const
try
{
	const auto text{this->currentText()};

	if (text.isEmpty())
		return Iso4217Codes::XXX;
	return toIso4217Code(text.toStdString());
}
catch (const exception& error)
{
	throw DataEntryError{
		QObject::tr("currency"),
		this->currentText(),
		QObject::tr("The country code is not supported or known."),
		error
	};
}

void CurrencyCodeWidget::setAvailableCodes(const Iso4217Codes& code)
{
	this->setAvailableCodes(set<Iso4217Codes>{code});
}

void CurrencyCodeWidget::setAvailableCodes(const set<Iso4217Codes>& codes)
{
	this->clear();

	if (codes.size() == 1)
	{
		this->addItem(QString::fromStdString(toStdString(*codes.begin())));
		this->setVisible(false);
		this->setCurrentIndex(0);
	}
	else
	{
		this->addItem("");
		for (const auto& code : codes)
			this->addItem(QString::fromStdString(toStdString(code)));
		this->setVisible(true);
	}
}

void CurrencyCodeWidget::setCurrentCode(const Iso4217Codes& code)
{
	if (code == Iso4217Codes::XXX)
		this->setCurrentIndex(0);
	else
		this->setCurrentText(QString::fromStdString(toStdString(code)));
}
