#include "plugins_list_item.h"
#include "SH_runtime.h"
#include <QMenu>
#include <qad_types.h>

using Plugin = SHPluginsManager::Plugin;


SHPluginsListItem::SHPluginsListItem(QWidget *parent): QWidget(parent) {
	setupUi(this);
	auto m = new QMenu();
	m->addActions({actionEnable, actionDisable, actionLoad, actionUnload, actionRemove});
	buttonMenu->setMenu(m);
	updateStyle();
}

SHPluginsListItem::~SHPluginsListItem() {
}


void SHPluginsListItem::setPlugin(const Plugin & p) {
	plugin = p;
	updateContent();
}


bool SHPluginsListItem::isFilterMatch(const QString & filter) const {
	if (filter.isEmpty()) return true;
	auto m = plugin.validManifest();
	if (m.name.contains(filter, Qt::CaseInsensitive)) return true;
	if (m.description.contains(filter, Qt::CaseInsensitive)) return true;
	if (m.author.contains(filter, Qt::CaseInsensitive)) return true;
	if (m.version.contains(filter, Qt::CaseInsensitive)) return true;
	return false;
}


QString SHPluginsListItem::name() const {
	return plugin.validManifest().name;
}


bool SHPluginsListItem::event(QEvent * e) {
	if (e->type() == QEvent::FontChange || e->type() == QEvent::Polish || e->type() == QEvent::Show) {
		updateStyle();
	}
	return QWidget::event(e);
}


void SHPluginsListItem::updateStyle() {
	auto is = preferredIconSize(1.5, this);
	buttonUpdate->setIconSize(is);
	buttonMenu->setIconSize(is);
	widgetColor->setMinimumWidth(is.width() / 2);
	setFixedHeight(is.height() * 4);
}


void SHPluginsListItem::updateContent() {
	auto m = plugin.validManifest();
	labelName->setText(m.name);
	labelDescription->setText(m.description);
	labelVersion->setText(plugin.displayVersion());
	labelPlugins->setText(plugin.plugins_list);
	labelAuthor->setText(m.author.isEmpty() ? "" : tr("Author") + ": " + m.author);
	iconedLabelError->setToolTip(tr("Error") + ": " + plugin.error);
	QColor color = Qt::gray;
	auto flags = plugin.flags;

	// Location icon
	stackedInstalled->setCurrentIndex(flags[Plugin::Installed] ? 0 : 1);

	// Loaded status icon
	stackedLoaded->setCurrentIndex(0);
	if (flags[Plugin::Installed]) {
		if (flags[Plugin::Loaded])
			stackedLoaded->setCurrentIndex(1);
		if (flags[Plugin::Error])
			stackedLoaded->setCurrentIndex(2);
	}

	// Update status icon
	stackedUpdate->setCurrentIndex(0);
	if (flags[Plugin::Installed] && flags[Plugin::Available]) {
		if (flags[Plugin::UpToDate])
			stackedUpdate->setCurrentIndex(1);
		else {
			if (flags[Plugin::Downgradable]) {
				stackedUpdate->setCurrentIndex(3);
			} else {
				stackedUpdate->setCurrentIndex(2);
			}
		}
	}

	// Actions
	actionRemove->setVisible(flags[Plugin::Installed]);
	if (flags[Plugin::Installed]) {
		actionEnable->setVisible(flags[Plugin::Disabled]);
		actionDisable->setVisible(!flags[Plugin::Disabled]);
		if (flags[Plugin::Disabled]) {
			actionLoad->setVisible(false);
			actionLoad->setVisible(false);
			actionUnload->setVisible(flags[Plugin::Loaded]);
		} else {
			actionLoad->setVisible(true);
			actionLoad->setVisible(!flags[Plugin::Loaded]);
			actionUnload->setVisible(flags[Plugin::Loaded]);
		}
	} else {
		actionEnable->setVisible(false);
		actionDisable->setVisible(false);
		actionLoad->setVisible(false);
		actionLoad->setVisible(false);
		actionUnload->setVisible(false);
	}
	if (flags[Plugin::Installed] && flags[Plugin::Available]) {
		buttonUpdate->setEnabled(flags[Plugin::Upgradable]);
	} else {
		buttonUpdate->setEnabled(flags[Plugin::Available]);
	}

	// Color
	if (!flags[Plugin::Disabled]) {
		if (!flags[Plugin::Installed] && flags[Plugin::Available])
			color = Qt::blue;
		else {
			if (flags[Plugin::Error])
				color = Qt::red;
			else {
				if (flags[Plugin::Installed] && flags[Plugin::Available]) {
					if (flags[Plugin::UpToDate])
						color = Qt::darkGreen;
					else if (flags[Plugin::Downgradable])
						color = QColor(255, 160, 0);
					else if (flags[Plugin::Upgradable])
						color = Qt::cyan;
				} else if (flags[Plugin::Loaded])
					color = Qt::darkGreen;
			}
		}
	}


	/*
	if (flags[Plugin::Installed] && flags[Plugin::Available]) {
		if (flags[Plugin::UpToDate])
			stackedUpdate->setCurrentIndex(1);
		else {
			if (flags[Plugin::Downgradable]) {
				stackedUpdate->setCurrentIndex(3);
				color = QColor(255, 160, 0);
			} else {
				stackedUpdate->setCurrentIndex(2);
			}
		}
		buttonUpdate->setEnabled(flags[Plugin::Upgradable]);
		if (flags[Plugin::Loaded])
			color = Qt::cyan;
	} else {
		stackedUpdate->setCurrentIndex(0);
		buttonUpdate->setEnabled(flags[Plugin::Available]);
		if (!flags[Plugin::Installed])
			color = Qt::blue;
	}
	actionLoad->setVisible(flags[Plugin::Installed]);
	if (flags[Plugin::Installed]) {
		if (flags[Plugin::Loaded]) {
			stackedLoaded->setCurrentIndex(1);
			if (flags[Plugin::Upgradable]) color = Qt::cyan;
			//else color = Qt::darkGreen;
		} else if (flags[Plugin::Error]) {
			stackedLoaded->setCurrentIndex(2);
			color = Qt::red;
		} else stackedLoaded->setCurrentIndex(0);
		iconedLabelError->setToolTip("Error: " + plugin.error);
		actionLoad->setVisible(!flags[Plugin::Loaded]);
		actionUnload->setVisible(flags[Plugin::Loaded]);
	} else {
		stackedLoaded->setCurrentIndex(0);
		actionLoad->setVisible(false);
		actionUnload->setVisible(false);
	}*/

	widgetColor->setStyleSheet(QString("background-color: rgb(%1,%2,%3)").arg(color.red()).arg(color.green()).arg(color.blue()));
	buttonMenu->setEnabled(false);
	for (auto a: buttonMenu->menu()->actions())
		if (a->isVisible()) {
			buttonMenu->setEnabled(true);
			break;
		}
	//for (auto l: {labelName, labelDescription, labelVersion, labelPlugins})
	//	if (l->text().isEmpty())
	//		l->setText(" ");
}


void SHPluginsListItem::on_buttonUpdate_clicked() {
	if (RUNTIME->plugins_manager)
		RUNTIME->plugins_manager->update({plugin.validManifest().name});
}


void SHPluginsListItem::on_actionRemove_triggered() {
	if (RUNTIME->plugins_manager)
		RUNTIME->plugins_manager->remove({plugin.validManifest().name});
}


void SHPluginsListItem::on_actionLoad_triggered() {
	if (RUNTIME->plugins_manager)
		RUNTIME->plugins_manager->load({plugin.validManifest().name});
}


void SHPluginsListItem::on_actionUnload_triggered() {
	if (RUNTIME->plugins_manager)
		RUNTIME->plugins_manager->unload({plugin.validManifest().name});
}


void SHPluginsListItem::on_actionEnable_triggered() {
	if (RUNTIME->plugins_manager)
		RUNTIME->plugins_manager->enable({plugin.local.name});
}


void SHPluginsListItem::on_actionDisable_triggered() {
	if (RUNTIME->plugins_manager)
		RUNTIME->plugins_manager->disable({plugin.local.name});
}
