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


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

BillEntryWidget::BillEntryWidget(const ObserverPtr<QWidget>& parent) :
	BillEntryWidget{Bill{}, {}, parent}
{}

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

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

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

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

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

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

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

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

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

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

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

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