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

#include <pecunia/Codes.h>
using pecunia::currency::Iso4217Codes;
#include <QtDebug>
// using qDebug;
#include <QPalette>
using ::QPalette;

#include <stdexcept>
using std::logic_error;

#include <adviser/BudgetHealth.h>
using drn::adviser::BudgetHealth;
using drn::adviser::HealthStatuses;
using drn::adviser::presentationText;
#include <budgeting/Budget.h>
using drn::budgeting::BudgetTotals;
#include <foundation/ObserverPtr.hpp>
using drn::foundation::ObserverPtr;


BudgetHealthWidget::BudgetHealthWidget(const ObserverPtr<QWidget>& parent) :
	QWidget{parent.observed()}
{
	this->setAttribute(Qt::WA_DeleteOnClose);
	this->setupUi(this);
	connect(
		this->monthlyTotals,
		&MonthlyTotalsWidget::selectedBudgetType,
		this,
		&BudgetHealthWidget::displayBudgetInterface
	);
}

void BudgetHealthWidget::updateTotals(const BudgetTotals& bt, const Iso4217Codes& preferred)
{
	qDebug() << "Updating to the calculated budget totals";
	this->monthlyTotals->updateTotals(bt, preferred);
}

void BudgetHealthWidget::updateHealth(const BudgetHealth& bh)
{
	qDebug() << "Updating budget health information";

	switch (bh.status())
	{
	case HealthStatuses::Healthy:
		this->budgetHealthDl->setPalette(QPalette{Qt::green});
	break;
	case HealthStatuses::Ailing:
		this->budgetHealthDl->setPalette(QPalette{Qt::yellow});
	break;
	case HealthStatuses::Sick:
		this->budgetHealthDl->setPalette(QPalette{Qt::red});
	break;
	default:
		throw logic_error{"The budget health widget did not handle the health status."};
	};
	this->budgetHealthStatusLbl->setText(
		tr("Status: %1").arg(presentationText(bh.status()))
	);
	this->budgetHealthDl->setValue(static_cast<qint32>(bh.percent()));
	this->infoTxtdt->setText(bh.information().join("\n\n"));
}
