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

#include <QDebug>
// using qDebug
#include <QString>
using ::QString;

#include <stdexcept>
using std::exception;

#include <budgeting/EventFrequency.h>
using drn::budgeting::allEventFrequencies;
using drn::budgeting::EventFrequency;
using drn::budgeting::toEventFrequency;
using drn::budgeting::toWeak;
#include "DesktopUiErrors.h"
using drn::desktop_ui::DataEntryError;
#include <foundation/ObserverPtr.hpp>
using drn::foundation::ObserverPtr;


EventFrequencyEntryWidget::EventFrequencyEntryWidget(const ObserverPtr<QWidget>& parent) :
	QWidget{parent.observed()}
{
	this->setupUi(this);

	for (const auto& eventFrequency : allEventFrequencies)
		if (eventFrequency == EventFrequency::Unknown)
			this->frequencyCmbbx->addItem("");
		else
			this->frequencyCmbbx->addItem(presentationText(eventFrequency));

	//{ Connect QComboBox Signals

	connect(
		this->frequencyCmbbx,
		static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
		this,
		&EventFrequencyEntryWidget::currentIndexChanged
	);

	//}
}

void EventFrequencyEntryWidget::setFrequency(const EventFrequency& frequency)
{
	qDebug() << "Setting Frequency:" << presentationText(frequency);

	if (frequency == EventFrequency::Unknown)
		this->frequencyCmbbx->setCurrentIndex(0);
	else
		this->frequencyCmbbx->setCurrentIndex(toWeak(frequency));
}

EventFrequency EventFrequencyEntryWidget::frequency() const
{
	const auto freq{this->frequencyCmbbx->currentText()};

	try
	{
		if (freq.isEmpty())
			return EventFrequency::Unknown;
		return toEventFrequency(freq);
	}
	catch (const exception& error)
	{
		throw DataEntryError{
			QObject::tr("event frequency"),
			freq,
			QObject::tr("The frequency name is not invalid."),
			error
		};
	}
}

int drn::desktop_ui::EventFrequencyEntryWidget::currentIndex() const
{
	return this->frequencyCmbbx->currentIndex();
}
