#include "plugins_properties.h"
#include "qvariantedit.h"
#include <QTabBar>
#include <QLabel>


PluginsProperties::PluginsProperties(QWidget * parent): QWidget(parent), Ui::PluginsProperties() {
	setupUi(this);
	scrollArea->setAutoFillBackground(false);
	scrollArea->viewport()->setAutoFillBackground(false);
	connect(properties, SIGNAL(changed()), actions_, SLOT(check()));
	connect(SHCORE, SIGNAL(changedPlugins()), this, SLOT(changedPlugins()));
	connect(SHCORE, SIGNAL(changedPluginsProperties()), this, SLOT(changedPluginsProperties()));
}


PluginsProperties::~PluginsProperties() {
}


SHPluginBase * PluginsProperties::currentPlugin() {
	int index = comboPlugin->currentIndex();
	if (index < 0 || index >= comboPlugin->count()) return 0;
	uint id = comboPlugin->itemData(index).toUInt();
	auto p = RUNTIME->plugins_manager->localInterface().getPlugin(id);
	return p.plugin;
}


void PluginsProperties::showCurrent() {
	on_comboPlugin_currentIndexChanged(comboPlugin->currentIndex());
}


void PluginsProperties::on_properties_changed() {
	SHPluginBase * p = currentPlugin();
	if (!p) return;
	uint id = p->id();
	properties->applyProperties();
	RUNTIME->plugins_props_project[id] = ps;
}

/*
void PluginsProperties::on_properties_resetStorageRequest(PropertyStorage * s) {
	BlockItem * srcb = SHCORE->srcBlock(block);
	if (srcb) *s = srcb->propertyStorage();
}
*/

void PluginsProperties::on_actions__executeAction(SHPluginBase::PluginAction action) {
	SHPluginBase * p = currentPlugin();
	if (!p) return;
	p->runProjectAction(action);
	showCurrent();
}


void PluginsProperties::on_actions__checkRequest(SHPluginBase::PluginAction action, bool * ret) {
	SHPluginBase * p = currentPlugin();
	if (!p) return;
	*ret = p->checkProjectAction(action);
}


void PluginsProperties::changedPlugins() {
	comboPlugin->blockSignals(true);
	comboPlugin->clear();
	no_actions.clear();
	auto pl = RUNTIME->plugins_manager->localInterface().loadedPlugins();
	for (const auto & p: pl) {
		uint id = p.plugin->id();
		QList<SHPluginBase::PluginAction> al = p.plugin->addProjectActions();
		bool no_p = RUNTIME->plugins_props_project.value(id).isEmpty();
		bool no_a = al.isEmpty();
		if (no_a) no_actions.insert(id);
		if (!no_p || !no_a) {
			visible.insert(id);
			comboPlugin->addItem(p.plugin->name(), id);
		}
	}
	comboPlugin->blockSignals(false);
	showCurrent();
}


void PluginsProperties::changedPluginsProperties() {
	auto pl = RUNTIME->plugins_manager->localInterface().loadedPlugins();
	for (const auto & p: pl) {
		uint id = p.plugin->id();
		//qDebug() << p.plugin->name() << no_actions.contains(id) << RUNTIME->plugins_props_project.value(id).isEmpty() << indeces[id];
		bool no_p = RUNTIME->plugins_props_project.value(id).isEmpty();
		bool no_a = no_actions.contains(id);
		if ((!no_p || !no_a) && !visible.contains(id)) {
			visible.insert(id);
			comboPlugin->addItem(p.plugin->name(), id);
		}
	}
	showCurrent();
}


void PluginsProperties::on_comboPlugin_currentIndexChanged(int index) {
	SHPluginBase * p = currentPlugin();
	if (!p) {
		groupProps->setHidden(true);
		groupActions->setHidden(true);
		return;
	}
	uint id = p->id();
	ps = RUNTIME->plugins_props_project.value(id);
	properties->setStorage(&ps);
	groupProps->setHidden(ps.isEmpty());
	QList<SHPluginBase::PluginAction> al = p->addProjectActions();
	actions_->setActions(al);
	groupActions->setHidden(al.isEmpty());
}
