#include <QtPlugin>
#include <QDesignerFormEditorInterface>
#include <QDesignerPropertyEditorInterface>
#include <QtPlugin>
#include <QEvent>
#include <QExtensionManager>
#include <QDesignerFormEditorInterface>
#include <QDesignerFormWindowInterface>
#include <QDesignerFormWindowManagerInterface>
#include <QDesignerFormWindowCursorInterface>
#include <QBoxLayout>
#include <QDialogButtonBox>
#include <QAction>
#include <QDialog>
#include <QGroupBox>
#include <QMessageBox>
#include <QDir>
#include "SH_runtime.h"
#include "plugin_graphic.h"
#include "ui_graphicmenuextension.h"



SHSGraphicMenuExtension::SHSGraphicMenuExtension(SHSGraphic * widget_, QObject * ): QDialog(), ui(new Ui::SHSGraphicMenuExtension) {
	ui->setupUi(this);
	widget = widget_;
	icon_command = QIcon(":/icons/code-function.png");
	action_default = new QAction("Assign variables ...", this);
	connect(action_default, SIGNAL(triggered(bool)), this, SLOT(action_default_triggered()));
	connect(this, SIGNAL(accepted()), this, SLOT(select_ok()));
}


SHSGraphicMenuExtension::~SHSGraphicMenuExtension() {
	delete ui;
}


QAction * SHSGraphicMenuExtension::preferredEditAction() const {
	return action_default;
}


QList<QAction * > SHSGraphicMenuExtension::taskActions() const {
	return (QList<QAction * >() << action_default);
}


void SHSGraphicMenuExtension::on_treeVars_itemClicked(QTreeWidgetItem * item, int column) {
	if (!item) return;
	Qt::ItemFlags f = item->flags();
	if (column == 1) f |= Qt::ItemIsEditable;
	else f &= ~Qt::ItemIsEditable;
	item->setFlags(f);
}


void SHSGraphicMenuExtension::on_treeVars_itemChanged(QTreeWidgetItem * item, int column) {
	if (item && (column == 1))
		item->setCheckState(0, Qt::Checked);
}


void SHSGraphicMenuExtension::action_default_triggered() {
	QMap<QString, QString> wv = widget->variablesMap();
	ui->treeVars->blockSignals(true);
	ui->treeVars->clear();
	foreach (const auto & v, RUNTIME->variables) {
		QTreeWidgetItem * ti = new QTreeWidgetItem();
		ti->setCheckState(0, wv.contains(v.name) ? Qt::Checked : Qt::Unchecked);
		ti->setData(0, Qt::UserRole, v.name);
		ti->setText(0, QString("%1 (%2)").arg(v.name, v.store));
		QString dn = wv.value(v.name);
		if (dn.isEmpty())
			dn =  v.display_name;
		ti->setText(1, dn);
		ui->treeVars->addTopLevelItem(ti);
	}
	ui->treeVars->blockSignals(false);
	exec();
}


void SHSGraphicMenuExtension::select_ok() {
	QStringList vars;
	for (int i = 0; i < ui->treeVars->topLevelItemCount(); ++i) {
		QTreeWidgetItem * ti = ui->treeVars->topLevelItem(i);
		if (ti->checkState(0) != Qt::Checked) continue;
		vars << (ti->data(0, Qt::UserRole).toString() + "=" + ti->text(1));
	}
	QDesignerFormWindowInterface * formWindow = QDesignerFormWindowInterface::findFormWindow(widget);
	if (formWindow != 0) {
		formWindow->cursor()->setProperty("variables", vars);
	} else {
		widget->setVariables(vars);
	}
}




QObject * SHSGraphicTaskMenuFactory::createExtension(QObject * object, const QString & iid, QObject * parent) const {
	if (iid != Q_TYPEID(QDesignerTaskMenuExtension))
		return 0;
	if (SHSGraphic * w = qobject_cast<SHSGraphic * >(object))
		return new SHSGraphicMenuExtension(w, parent);
	return 0;
}




SHSGraphicPlugin::SHSGraphicPlugin(QObject * parent): QObject(parent) {
	m_initialized = m_designer = false;
}


void SHSGraphicPlugin::initialize(QDesignerFormEditorInterface * core) {
	m_designer = true;
	if (m_initialized)
		return;
	//RUNTIME->commandsDB.openFromArgs("-db=");
	RUNTIME->openFromCmdFile();
	QExtensionManager * manager = core->extensionManager();
	Q_ASSERT(manager != 0);
	manager->registerExtensions(new SHSGraphicTaskMenuFactory(manager), Q_TYPEID(QDesignerTaskMenuExtension));
	m_initialized = true;
}


bool SHSGraphicPlugin::isInitialized() const {
	return m_initialized;
}


QWidget * SHSGraphicPlugin::createWidget(QWidget * parent) {
	SHSGraphic * w = new SHSGraphic(parent);
	if (m_designer)
		w->buildDebug();
	return w;
}


QString SHSGraphicPlugin::name() const {
	return QLatin1String("SHSGraphic");
}


QString SHSGraphicPlugin::group() const {
	return QLatin1String("SHS Widgets");
}


QIcon SHSGraphicPlugin::icon() const {
	return QIcon(":/icons/graphic.png");
}


QString SHSGraphicPlugin::toolTip() const {
	return QLatin1String("");
}


QString SHSGraphicPlugin::whatsThis() const {
	return QLatin1String("");
}


bool SHSGraphicPlugin::isContainer() const {
	return false;
}


QString SHSGraphicPlugin::domXml() const {
	return QLatin1String("<widget class=\"SHSGraphic\" name=\"graphic\">\n</widget>\n");
}


QString SHSGraphicPlugin::includeFile() const {
	return QLatin1String("graphic.h");
}

