/*******************************************************************************
***  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_DESKTOP_UI_INTERNAL_EXCHANGERATESDIALOG_H_
#define DRN_DESKTOP_UI_INTERNAL_EXCHANGERATESDIALOG_H_

#include <QDialog>

#include <set>

#include <conversion/ConversionMap.h>
#include <foundation/ObserverPtr.hpp>
#include <ui_ExchangeRatesDialog.h>


class QAbstractButton;

namespace pecunia
{
namespace currency
{

enum class Iso4217Codes : quint16;

}}

namespace drn
{
namespace conversion
{

class ConversionMap;
struct CurrencySource;

}
namespace desktop_ui
{
namespace internal
{

/**
 * @brief Provides an interface to display, fetch, and edit currency exchange rates.
 */
class ExchangeRatesDialog : public QDialog, Ui::ExchangeRatesDlg
{
	Q_OBJECT

	void saveRates();
	void prepareFetchExchangeRates();

	foundation::ObserverPtr<QAbstractButton> fetch_;

private slots:
	void onClicked(foundation::ObserverPtr<QAbstractButton> button);

public slots:
	/**
	 * @brief Updates the displayed exchangeable currencies.
	 *
	 * @param currencies The exchangeable currencies to be updated.
	 * @param conversions The updated exchange rates for all currencies.
	 */
	void onFetchedExchangeRates(
		const std::set<pecunia::currency::Iso4217Codes>& currencies,
		const conversion::ConversionMap& conversions
	);

signals:
	/**
	 * @brief Triggered when a user wishes to update remotely the exchange rates.
	 *
	 * @param currencies The currencies that should be updated.
	 */
	void fetchExchangeRates(const std::set<pecunia::currency::Iso4217Codes>& currencies);
	void savedExchangeRates(const conversion::ConversionMap& conversions);

public:
	/**
	 * @brief Full initialisation constructor.
	 *
	 * @param currencies The currency codes that are exchangeable.
	 * @param currentConversions The current known conversions for all currencies.
	 * @param parent The parent of the object.
	 */
	ExchangeRatesDialog(
		const std::set<pecunia::currency::Iso4217Codes>& currencies,
		const conversion::ConversionMap& currentConversions,
		const foundation::ObserverPtr<::QWidget>& parent
	);
};

}}}

#endif
