/*******************************************************************************
***  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 "BankEntryWidget.h"
using std::set;
using drn::banking::Bank;
using drn::banking::BankName;
using drn::desktop_ui::internal::BankEntryWidget;
using drn::foundation::Optional;
using drn::foundation::SwitchStates;

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

#include <utility>
using std::move;

#include <accounting/AccountCode.h>
using drn::accounting::AccountNumber;
#include <foundation/ObserverPtr.hpp>
using drn::foundation::ObserverPtr;


void BankEntryWidget::markDirty()
{
	this->isDirty_.flipOn();
}

BankEntryWidget::BankEntryWidget(Optional<Bank> b, const ObserverPtr<QWidget> parent) :
	QWidget{parent.observed()},
	original_{std::move(b)},
	isDirty_{SwitchStates::Off}
{
	this->setupUi(this);
	this->nameLndt->setPlaceholderText(BankEntryWidget::newBankNameText());

	if (this->original_.hasValue())
	{
		this->nameLndt->setText(this->original_->name_.institution());
		this->closedChckbx->setChecked(this->original_->isClosed());
	}
	this->errorsLbl->hide();

	connect(
		this->nameLndt,
		&QLineEdit::editingFinished,
		this,
		&BankEntryWidget::markDirty
	);
	connect(
		this->closedChckbx,
		&QCheckBox::stateChanged,
		this,
		&BankEntryWidget::markDirty
	);
	connect(
		this->removeChckbx,
		&QCheckBox::stateChanged,
		this,
		&BankEntryWidget::markDirty
	);
}

const QString& BankEntryWidget::newBankNameText()
{
	static const auto name{tr("New Bank")};
	return name;
}

const Optional<Bank>& BankEntryWidget::original() const
{
	return this->original_;
}

QString BankEntryWidget::name() const
{
	return this->nameLndt->text();
}

bool BankEntryWidget::isClosed() const
{
	return this->closedChckbx->isChecked();
}

bool BankEntryWidget::isRemoved() const
{
	return this->removeChckbx->isChecked();
}

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

void BankEntryWidget::setErrorMessage(const QString& error)
{
	this->errorsLbl->setText(error);
	this->markDirty();
	this->errorsLbl->show();
}

void BankEntryWidget::resetDirtyFlag()
{
	qDebug() << "Resetting the dirty flag for" << this->nameLndt->text();
	this->isDirty_.flipOff();
}

void BankEntryWidget::saved(set<AccountNumber> numbers)
{
	this->original_ = Bank{
		BankName{this->nameLndt->text()},
		std::move(numbers),
		this->closedChckbx->isChecked()

	};
	this->isDirty_.flipOff();
}
