/*******************************************************************************
***  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 "WageEntryWidget.h"
using ::QWidget;
using std::set;
using drn::budgeting::BudgetSource;
using drn::desktop_ui::internal::WageEntryWidget;
using drn::foundation::Optional;

#include <pecunia/Codes.h>
using pecunia::currency::Iso4217Codes;
#include <pecunia/Money.h>
using pecunia::currency::Money;
#include <QDate>
using ::QDate;
#include <QDebug>
// using qDebug
#include <QString>
using ::QString;

#include <exception>
using std::exception;

#include <budgeting/EventFrequency.h>
using drn::budgeting::EventFrequency;
#include <budgeting/Wage.h>
using drn::budgeting::Wage;
#include <foundation/ObserverPtr.hpp>
using drn::foundation::ObserverPtr;


void WageEntryWidget::markDirty()
{
	qDebug() << "Wage Entry Widget is Dirty:" << this->sourceLndt->text();
	this->isDirty_.flipOn();
}

WageEntryWidget::WageEntryWidget(const foundation::ObserverPtr<::QWidget>& parent) :
	WageEntryWidget{Wage{}, {}, parent}
{}

WageEntryWidget::WageEntryWidget(
	const Wage& wage,
	const set<Iso4217Codes>& usableCurrencies,
	const ObserverPtr<QWidget>& parent
) :
	QWidget{parent.observed()},
	isDirty_{},
	original_{wage.source().isEmpty() ? Optional<BudgetSource>{} : wage.source()}
{
	this->setupUi(this);

	if (this->original_.hasValue())
	{
		this->sourceLndt->setText(this->original_->name());
		this->sourceLndt->setCursorPosition(0);
	}
	this->moneyEntry->setUsableCodes(usableCurrencies);
	this->moneyEntry->setMoney(wage.amount());
	this->frequencyEntry->setFrequency(wage.period());
	this->nextOccurOnDtdt->setDate(
		wage.nextOccurOn().isValid() ? wage.nextOccurOn() : QDate::currentDate()
	);
	this->errorMessageLbl->hide();

	//{ Update Error Message Colour

	QPalette error{this->errorMessageLbl->palette()};
	error.setColor(this->errorMessageLbl->foregroundRole(), Qt::red);
	this->errorMessageLbl->setPalette(error);

	//}

	connect(
		this->sourceLndt,
		&QLineEdit::textEdited,
		this,
		&WageEntryWidget::markDirty
	);
	connect(
		this->moneyEntry,
		&MoneyEntryWidget::editingFinished,
		this,
		&WageEntryWidget::markDirty
	);
	connect(
		this->moneyEntry,
		&MoneyEntryWidget::editingFinished,
		this,
		&WageEntryWidget::valueChanged
	);
	connect(
		this->moneyEntry,
		qOverload<qint32>(&MoneyEntryWidget::currentIndexChanged),
		this,
		&WageEntryWidget::markDirty
	);
	connect(
		this->moneyEntry,
		qOverload<qint32>(&MoneyEntryWidget::currentIndexChanged),
		this,
		&WageEntryWidget::valueChanged
	);
	connect(
		this->frequencyEntry,
		qOverload<qint32>(&EventFrequencyEntryWidget::currentIndexChanged),
		this,
		&WageEntryWidget::markDirty
	);
	connect(
		this->frequencyEntry,
		qOverload<qint32>(&EventFrequencyEntryWidget::currentIndexChanged),
		this,
		&WageEntryWidget::valueChanged
	);
	connect(
		this->removeChckbx,
		&QCheckBox::stateChanged,
		this,
		&WageEntryWidget::markDirty
	);
	connect(
		this->removeChckbx,
		&QCheckBox::stateChanged,
		this,
		&WageEntryWidget::valueChanged
	);
	connect(
		this->nextOccurOnDtdt,
		&QDateEdit::dateChanged,
		this,
		&WageEntryWidget::markDirty
	);
}

const Optional<BudgetSource>& WageEntryWidget::originalSource() const
{
	return this->original_;
}

QString WageEntryWidget::sourceText() const
{
	return this->sourceLndt->text();
}

BudgetSource WageEntryWidget::source() const
{
	return BudgetSource{this->sourceLndt->text()};
}

Money WageEntryWidget::amount() const
{
	return this->moneyEntry->money();
}

EventFrequency WageEntryWidget::period() const
{
	return this->frequencyEntry->frequency();
}

QDate WageEntryWidget::nextOccurOn() const
{
	return this->nextOccurOnDtdt->date();
}

bool WageEntryWidget::shouldRemove() const
{
	return this->removeChckbx->isChecked();
}

bool WageEntryWidget::isDirty() const
{
	return this->isDirty_;
}

void WageEntryWidget::saved()
{
	this->original_ = BudgetSource{this->sourceLndt->text()};
	this->errorMessageLbl->hide();
	this->errorMessageLbl->clear();
	this->isDirty_.flipOff();
}

void WageEntryWidget::setErrorMessage(const QString& message)
{
	this->errorMessageLbl->setText(message);
	this->errorMessageLbl->show();
	this->isDirty_.flipOn();
}

void WageEntryWidget::resetDirty()
{
	this->isDirty_.flipOff();
}
