/*******************************************************************************
***  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 "NontrackEntryWidget.h"
using ::QWidget;
using std::set;
using drn::budgeting::BudgetSource;
using drn::desktop_ui::internal::NontrackEntryWidget;
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/Nontrack.h>
using drn::budgeting::Nontrack;
#include <foundation/ObserverPtr.hpp>
using drn::foundation::ObserverPtr;


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

NontrackEntryWidget::NontrackEntryWidget(const ObserverPtr<QWidget>& parent) :
	NontrackEntryWidget{Nontrack{}, {}, parent}
{}

NontrackEntryWidget::NontrackEntryWidget(
	const Nontrack& nontrack,
	const set<Iso4217Codes>& usableCurrencies,
	const ObserverPtr<QWidget>& parent
) :
	QWidget{parent.observed()},
	isDirty_{},
	original_{nontrack.source().isEmpty() ? Optional<BudgetSource>{} : nontrack.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(nontrack.amount());
	this->frequencyEntry->setFrequency(nontrack.period());
	this->nextOccurOnDtdt->setDate(
		nontrack.nextOccurOn().isValid() ? nontrack.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,
		&NontrackEntryWidget::markDirty
	);
	connect(
		this->moneyEntry,
		&MoneyEntryWidget::editingFinished,
		this,
		&NontrackEntryWidget::markDirty
	);
	connect(
		this->moneyEntry,
		&MoneyEntryWidget::editingFinished,
		this,
		&NontrackEntryWidget::valueChanged
	);
	connect(
		this->moneyEntry,
		qOverload<qint32>(&MoneyEntryWidget::currentIndexChanged),
		this,
		&NontrackEntryWidget::markDirty
	);
	connect(
		this->moneyEntry,
		qOverload<qint32>(&MoneyEntryWidget::currentIndexChanged),
		this,
		&NontrackEntryWidget::valueChanged
	);
	connect(
		this->frequencyEntry,
		qOverload<qint32>(&EventFrequencyEntryWidget::currentIndexChanged),
		this,
		&NontrackEntryWidget::markDirty
	);
	connect(
		this->frequencyEntry,
		qOverload<qint32>(&EventFrequencyEntryWidget::currentIndexChanged),
		this,
		&NontrackEntryWidget::valueChanged
	);
	connect(
		this->removeChckbx,
		&QCheckBox::stateChanged,
		this,
		&NontrackEntryWidget::markDirty
	);
	connect(
		this->removeChckbx,
		&QCheckBox::stateChanged,
		this,
		&NontrackEntryWidget::valueChanged
	);
	connect(
		this->nextOccurOnDtdt,
		&QDateEdit::dateChanged,
		this,
		&NontrackEntryWidget::markDirty
	);
}

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

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

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

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

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

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

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

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

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

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

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