/*******************************************************************************
***  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 "MonthlyTotalsWidget.h"
using ::QWidget;
using drn::desktop_ui::internal::MonthlyTotalsWidget;
using drn::foundation::ObserverPtr;

#include <pecunia/Codes.h>
using pecunia::currency::Iso4217Codes;
using pecunia::currency::toStdString;
#include <QtDebug>
// using qDebug;
#include <QString>
using ::QString;
#include <QStringList>
using ::QStringList;
#include <QTableWidgetItem>
using ::QTableWidgetItem;

#include <memory>
using std::make_unique;

#include <budgeting/Budget.h>
using drn::budgeting::BudgetTotals;
#include <budgeting/BudgetItemTypes.h>
using drn::budgeting::BudgetItemTypes;
#include <foundation/PresentationText.h>
using drn::foundation::presentationText;


void MonthlyTotalsWidget::onItemDoubleClicked(ObserverPtr<QTableWidgetItem> item)
{
	Q_ASSERT(item != nullptr);
	const auto rowIndex{item->row()};
	qDebug() << "Selected monthly total budget row" << rowIndex;

	if (rowIndex == MonthlyTotalsWidget::billsTotalIndex_)
	{
		qInfo() << "Selected bills total.";
		emit selectedBudgetType(BudgetItemTypes::Bill);
	}
	else if (rowIndex == MonthlyTotalsWidget::debtsTotalIndex_)
	{
		qInfo() << "Selected debts total.";
		emit selectedBudgetType(BudgetItemTypes::Debt);
	}
	else if (rowIndex == MonthlyTotalsWidget::goalsTotalIndex_)
	{
		qInfo() << "Selected goals total.";
		emit selectedBudgetType(BudgetItemTypes::Goal);
	}
	else if (rowIndex == MonthlyTotalsWidget::nontrackIndex_)
	{
		qInfo() << "Selected nontrack total.";
		emit selectedBudgetType(BudgetItemTypes::Nontrack);
	}
	else if (rowIndex == MonthlyTotalsWidget::wageTotalIndex_)
	{
		qInfo() << "Selected wages total.";
		emit selectedBudgetType(BudgetItemTypes::Wage);
	}
	else
		Q_ASSERT(false && "Unhandled monthly totals widget row index.");
}

const int MonthlyTotalsWidget::wageTotalIndex_{0};
const int MonthlyTotalsWidget::nontrackIndex_{1};
const int MonthlyTotalsWidget::goalsTotalIndex_{2};
const int MonthlyTotalsWidget::billsTotalIndex_{3};
const int MonthlyTotalsWidget::debtsTotalIndex_{4};

MonthlyTotalsWidget::MonthlyTotalsWidget(const ObserverPtr<QWidget>& parent) :
	QWidget{parent.observed()}
{
	this->setAttribute(Qt::WA_DeleteOnClose);
	this->setupUi(this);
}

void MonthlyTotalsWidget::updateTotals(const BudgetTotals& bt, const Iso4217Codes& preferred)
{
	qDebug() << "Updating monthly budget totals";
	QStringList hHeaders{};
	hHeaders << tr("Monthly Total (%1)").arg(QString::fromStdString(toStdString(preferred)));
	this->monthlyTotalsTblwdgt->setHorizontalHeaderLabels(hHeaders);
	this->monthlyTotalsTblwdgt->setItem(
		MonthlyTotalsWidget::wageTotalIndex_,
		0,
		make_unique<QTableWidgetItem>(presentationText(bt.wage_)).release()
	);
	this->monthlyTotalsTblwdgt->setItem(
		MonthlyTotalsWidget::goalsTotalIndex_,
		0,
		make_unique<QTableWidgetItem>(presentationText(bt.goals_)).release()
	);
	this->monthlyTotalsTblwdgt->setItem(
		MonthlyTotalsWidget::billsTotalIndex_,
		0,
		make_unique<QTableWidgetItem>(presentationText(bt.bills_)).release()
	);
	this->monthlyTotalsTblwdgt->setItem(
		MonthlyTotalsWidget::debtsTotalIndex_,
		0,
		make_unique<QTableWidgetItem>(presentationText(bt.debts_)).release()
	);
	this->monthlyTotalsTblwdgt->setItem(
		MonthlyTotalsWidget::nontrackIndex_,
		0,
		make_unique<QTableWidgetItem>(presentationText(bt.nontrack_)).release()
	);
	connect(
		this->monthlyTotalsTblwdgt,
		&QTableWidget::itemDoubleClicked,
		this,
		&MonthlyTotalsWidget::onItemDoubleClicked
	);
}
