#include "ui_piqt_iodevice_edit_dialog.h"
#include "piqt_iodevice_edit_dialog.h"
#include "piqt.h"
#include "picodeinfo.h"
#include "piiodevice.h"
#include <QCheckBox>


IODeviceEditDialog::IODeviceEditDialog(QWidget * parent): QDialog(parent) {
	ui = new Ui::IODeviceEditDialog();
	ui->setupUi(this);
	PICodeInfo::EnumInfo * ei = PICodeInfo::enumsInfo->value("PIIODevice::DeviceMode");
	if (ei) {
		piForeachC (PICodeInfo::EnumeratorInfo & e, ei->members)
			ui->comboMode->addItem(PI2QString(e.name + " (" + PIString::fromNumber(e.value) + ")"), QVariant::fromValue<int>(e.value));
	}
	ui->comboMode->setCurrentIndex(ui->comboMode->count() - 1);
	ei = PICodeInfo::enumsInfo->value("PIIODevice::DeviceOption");
	if (ei) {
		piForeachC (PICodeInfo::EnumeratorInfo & e, ei->members) {
			QCheckBox * cb = new QCheckBox();
			cb->setText(PI2QString(e.name + " (" + PIString::fromNumber(e.value) + ")"));
			cb->setProperty("__value", e.value);
			ui->layoutOptions->addWidget(cb);
		}
	}
	PIStringList pl = PIIODevice::availablePrefixes();
	piForeachC (PIString & p, pl) {
		ui->comboType->addItem(PI2QString(p));
	}
}


IODeviceEditDialog::~IODeviceEditDialog() {
}


int IODeviceEditDialog::getOptions() const {
	int ret = 0;
	for (int i = 0; i < ui->layoutOptions->count(); ++i) {
		QCheckBox * cb = qobject_cast<QCheckBox*>(ui->layoutOptions->itemAt(i)->widget());
		if (!cb) continue;
		if (cb->isChecked())
			ret |= cb->property("__value").toInt();
	}
	return ret;
}


void IODeviceEditDialog::setOptions(int o) {
	for (int i = 0; i < ui->layoutOptions->count(); ++i) {
		QCheckBox * cb = qobject_cast<QCheckBox*>(ui->layoutOptions->itemAt(i)->widget());
		if (!cb) continue;
		int cbf = cb->property("__value").toInt();
		cb->setChecked((o & cbf) == cbf);
	}
}


void IODeviceEditDialog::on_comboType_currentIndexChanged(int index) {
	PIString prefix = Q2PIString(ui->comboType->currentText());
	PIVector<const PIObject * > rd(PICollection::groupElements("__PIIODevices__"));
	piForeachC (PIObject * d, rd) {
		const PIIODevice * dev = ((const PIIODevice * )d);
		if (prefix != dev->fullPathPrefix())
			continue;
		ps = PI2QPropertyStorage(dev->constructVariant().get());
		ui->widgetProperties->setStorage(&ps);
		return;
	}
}


QAD::IODevice IODeviceEditDialog::getIODevice(const QAD::IODevice & d) {
	setValue(d);
	if (QDialog::exec() != QDialog::Accepted)
		return QAD::IODevice();
	return value();
}


QAD::IODevice IODeviceEditDialog::value() const {
	QAD::IODevice d;
	ui->widgetProperties->applyProperties();
	d.prefix = ui->comboType->currentText();
	d.mode = ui->comboMode->itemData(ui->comboMode->currentIndex()).toInt();
	d.options = getOptions();
	d.props = ps;
	return d;
}


void IODeviceEditDialog::setValue(const QAD::IODevice & d) {
#if QT_VERSION >= 0x050000
	ui->comboType->setCurrentText(d.prefix);
#else
	for (int i = 0; i < ui->comboType->count(); ++i) {
		if (ui->comboType->itemText(i) == d.prefix) {
			ui->comboType->setCurrentIndex(i);
			break;
		}
	}
#endif
	for (int i = 0; i < ui->comboMode->count(); ++i)
		if (ui->comboMode->itemData(i).toInt() == d.mode) {
			ui->comboMode->setCurrentIndex(i);
			break;
		}
	setOptions(d.options);
	ps = d.props;
	ui->widgetProperties->setStorage(&ps);
}
