#include "properties_user_editor.h"
#include "qvariantedit.h"
#include "SH_types.h"
#include <QGridLayout>
#include <QToolButton>


PropertiesUserEditor::PropertiesUserEditor(QWidget * parent): QWidget(parent) {
	setLayout(new QGridLayout());
	layout()->setContentsMargins(0, 0, 0, 0);
	storage = nullptr;
}


PropertiesUserEditor::~PropertiesUserEditor() {
	clear();
}


void PropertiesUserEditor::clear() {
	qDeleteAll(_widgets);
	_widgets.clear();
}


bool PropertiesUserEditor::isEmpty() const {
	return ((QGridLayout*)layout())->count() == 0;
}


void PropertiesUserEditor::setStorage(PropertyStorage * s) {
	clear();
	storage = s;
	if (!storage) return;
	int r = 0;
	QGridLayout * layoutProps = (QGridLayout*)layout();
	foreach (const PropertyStorage::Property & p, *storage) {
		if (((p.flags & SHS::Hidden) == SHS::Hidden) && ((p.flags & SHS::Label) != SHS::Label)) continue;
		QLabel * lbl = new QLabel(p.name);
		_widgets << lbl;
		if ((p.flags & SHS::Label) == SHS::Label) {
			QString lt = p.name + p.value.toString();
			if (!p.comment.isEmpty())
				lt += " (" + p.comment + ")";
			lbl->setText(lt);
			lbl->setAlignment(Qt::AlignCenter);
			layoutProps->addWidget(lbl, r, 0, 1, 3);
		} else {
			lbl->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
			layoutProps->addWidget(lbl, r, 0);
			QVariantEdit * ve = new QVariantEdit();
			ve->setValue(p.value);
			ve->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
			ve->setMinimumWidth(50);
			connect(ve, SIGNAL(valueChanged(QVariant)), this, SIGNAL(changed()));
			if ((p.flags & SHS::Fixed) == SHS::Fixed || (p.flags & SHS::Internal) == SHS::Internal)
				ve->setEnabled(false);
			layoutProps->addWidget(ve, r, 1); _widgets << ve;
			lbl = new QLabel(p.comment); lbl->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
			layoutProps->addWidget(lbl, r, 2); _widgets << lbl;
			if (((p.flags & SHS::Fixed) == 0) && reset_buttons) {
				QToolButton * rb = new QToolButton();
				rb->setProperty("prop_name", p.name);
				rb->setProperty("prop_edit", quintptr(ve));
				rb->setToolTip(tr("Reset to default")); rb->setIcon(QIcon(":/icons/document-revert.png"));
				connect(rb, SIGNAL(clicked(bool)), this, SLOT(buttonReset_clicked()));
				layoutProps->addWidget(rb, r, 3); _widgets << rb;
			}
		}
		++r;
	}
}


void PropertiesUserEditor::applyProperties() {
	if (!storage) return;
	QList<PropertyStorage::Property> & props(storage->properties());
	QGridLayout * layoutProps = (QGridLayout*)layout();
	for (int r = 0; r < layoutProps->rowCount(); ++r) {
		if (layoutProps->itemAtPosition(r, 0) == 0 || layoutProps->itemAtPosition(r, 1) == 0) continue;
		QLabel * lbl = qobject_cast<QLabel * >(layoutProps->itemAtPosition(r, 0)->widget());
		QVariantEdit * ve = qobject_cast<QVariantEdit * >(layoutProps->itemAtPosition(r, 1)->widget());
		if (lbl == 0 || ve == 0) continue;
		QString pn = lbl->text();
		for (int i = 0; i < props.size(); ++i) {
			PropertyStorage::Property & p(props[i]);
			if (p.name == pn) {
				p.value = ve->value();
				break;
			}
		}
	}
}


void PropertiesUserEditor::setResetButtonsVisible(bool yes) {
	reset_buttons = yes;
}


void PropertiesUserEditor::buttonReset_clicked() {
	if (!sender() || !storage) return;
	QString pname = sender()->property("prop_name").toString();
	QVariantEdit * ve = (QVariantEdit*)(sender()->property("prop_edit").value<quintptr>());
	if (pname.isEmpty() || !ve) return;
	PropertyStorage ss;
	emit resetStorageRequest(&ss);
	foreach (const PropertyStorage::Property & p, ss)
		if (p.name == pname) {
			ve->setValue(p.value);
			return;
		}
}
