#include "stateiconplugin.h"

#include "ui_stateiconmenuextension.h"

#include <QAction>
#include <QDesignerFormEditorInterface>
#include <QDesignerFormWindowCursorInterface>
#include <QDesignerFormWindowInterface>
#include <QExtensionManager>
#include <QTimer>
#include <QtCore/QtPlugin>
#include <private/qtresourceview_p.h>

enum Column {
	cState,
	cText,
	cIcon
};


StateIconMenuExtension::StateIconMenuExtension(StateIcon * widget_, QObject *): QDialog(), ui(new Ui::StateIconMenuExtension) {
	ui->setupUi(this);
	widget         = widget_;
	icon_command   = QIcon(":/icons/code-function.png");
	action_default = new QAction("Edit states ...", this);
	connect(action_default, SIGNAL(triggered(bool)), this, SLOT(action_default_triggered()));
}


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


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


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


void StateIconMenuExtension::accept() {
	widget->clearStates();
	for (int i = 0; i < ui->tree->topLevelItemCount(); ++i) {
		QTreeWidgetItem * ti = ui->tree->topLevelItem(i);
		widget->addState(ti->text(cState).toInt(), ti->text(cText), ti->text(cIcon));
	}
	QDesignerFormWindowInterface * formWindow = QDesignerFormWindowInterface::findFormWindow(widget);
	if (formWindow) {
		formWindow->cursor()->setProperty("rawStates", widget->saveStates());
	}
	QDialog::accept();
}


void StateIconMenuExtension::on_buttonAdd_clicked() {
	auto ti = new QTreeWidgetItem({"0"});
	ti->setFlags(ti->flags() | Qt::ItemIsEditable);
	ui->tree->addTopLevelItem(ti);
	ui->tree->editItem(ti, cState);
}


void StateIconMenuExtension::on_buttonDel_clicked() {
	qDeleteAll(ui->tree->selectedItems());
}


void StateIconMenuExtension::on_tree_itemClicked(QTreeWidgetItem * item, int column) {
	if (!item) return;
	Qt::ItemFlags f = item->flags();
	f.setFlag(Qt::ItemIsEditable, column != cIcon);
	item->setFlags(f);
}


void StateIconMenuExtension::on_tree_itemDoubleClicked(QTreeWidgetItem * item, int column) {
	if (!item || column != cIcon) return;
	QDesignerFormWindowInterface * formWindow = QDesignerFormWindowInterface::findFormWindow(widget);
	if (formWindow) {
		QtResourceViewDialog dlg(formWindow->core());
		dlg.selectResource(item->text(cIcon));
		if (dlg.exec() == QDialog::Accepted) {
			QString ni = dlg.selectedResource();
			item->setText(cIcon, ni);
			item->setIcon(cIcon, QIcon(ni));
		}
	}
}


void StateIconMenuExtension::action_default_triggered() {
	ui->tree->clear();
	auto states = widget->allStates();
	for (auto s: states) {
		QTreeWidgetItem * ti = new QTreeWidgetItem();
		ti->setText(cState, QString::number(s));
		ti->setText(cText, widget->stateText(s));
		ti->setText(cIcon, widget->stateIcon(s));
		ti->setIcon(cIcon, QIcon(widget->stateIcon(s)));
		ui->tree->addTopLevelItem(ti);
	}
	exec();
}


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


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


void StateIconPlugin::initialize(QDesignerFormEditorInterface * core) {
	if (m_initialized) return;
	QExtensionManager * manager = core->extensionManager();
	Q_ASSERT(manager != 0);
	manager->registerExtensions(new StateIconTaskMenuFactory(manager), Q_TYPEID(QDesignerTaskMenuExtension));
	m_initialized = true;
}


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


QWidget * StateIconPlugin::createWidget(QWidget * parent) {
	auto * ret                                = new StateIcon(parent);
	QDesignerFormWindowInterface * formWindow = QDesignerFormWindowInterface::findFormWindow(parent);
	if (formWindow) {
		connect(formWindow, &QDesignerFormWindowInterface::resourceFilesChanged, ret, [ret] {
			QTimer::singleShot(0, ret, [ret] { ret->loadStates(ret->saveStates()); });
		});
	}
	return ret;
}


QString StateIconPlugin::name() const {
	return QLatin1String("StateIcon");
}


QString StateIconPlugin::group() const {
	return QLatin1String("Display Widgets");
}


QIcon StateIconPlugin::icon() const {
	return QIcon(":/icons/widgets/status-icon.png");
}


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


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


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


QString StateIconPlugin::domXml() const {
	return QLatin1String("<widget class=\"StateIcon\" name=\"stateIcon\">\n</widget>\n");
}


QString StateIconPlugin::includeFile() const {
	return QLatin1String("stateicon.h");
}
