/*******************************************************************************
***  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_MONEYENTRYWIDGET_H_
#define DRN_DESKTOP_UI_MONEYENTRYWIDGET_H_

#include <pecunia/MoneyTypes.h>
#include <QString>
#include <QWidget>

#include <set>

#include "desktop-ui_export.h"
#include <foundation/ObserverPtr.hpp>
#include <foundation/Switch.h>
#include <ui_MoneyEntryWidget.h>


namespace pecunia
{
namespace currency
{

enum class Iso4217Codes : quint16;
class Money;

}}

namespace drn
{
namespace desktop_ui
{

/**
 * @brief A Qt Widget to validate the amount entered.
 */
class DRN_DESKTOP_UI_EXPORT MoneyAmountValidator : public QValidator
{
	Q_OBJECT

	QString code_;

public:
	explicit MoneyAmountValidator(const QString& code, QObject* parent = nullptr);
	State validate(QString& input, int& pos) const override;
};
/**
 * @brief A Qt Widget to display a money entry.
 */
class MoneyEntryWidget : public QWidget, Ui::MoneyEntryWidget
{
	Q_OBJECT

	bool eventFilter(QObject* obj, QEvent* event) override;

	quint8 displayDigits_;
	foundation::Switch isReadOnly_;

private slots:
	void setAmountValidator();

signals:
	//{ QLineEdit Signals

	void editingFinished();

	//}

	//{ QComboBox Signals

	void currentIndexChanged(int index);

	//}

	/**
	 * @brief Triggered when the value of the money changes.
	 *
	 * @param m The new value for the money when valid, default constructed value when it's invalid.
	 */
	void moneyChanged(const pecunia::currency::Money& m);

public:
	explicit MoneyEntryWidget(const foundation::ObserverPtr<QWidget>& parent);
	std::set<pecunia::currency::Iso4217Codes> currencies() const noexcept;
	void setUsableCode(const pecunia::currency::Iso4217Codes& code);
	void setUsableCodes(const std::set<pecunia::currency::Iso4217Codes>& codes);
	void setMoney(const pecunia::currency::Money& money);
	pecunia::currency::Money money() const;
	void setLabel(const QString& text);
	void hideCodes();
	void showCodes();
	pecunia::currency::Iso4217Codes code() const;
	void setReadOnly(const bool isReadOnly);
	void showLabel(const bool shouldShow);
	void clear();
	pecunia::UnitStorage major() const;
	pecunia::MinorUnit minor() const;
	QString codeText() const;
	bool isReadOnly() const;

public slots:
	void setCurrentIndex(const int index);
};

}}

#endif
