#include "piqt_iodevice_edit_dialog.h"

#include "picodeinfo.h"
#include "piiodevice.h"
#include "piqt.h"
#include "ui_piqt_iodevice_edit_dialog.h"

#include <QCheckBox>


IODeviceEditDialog::IODeviceEditDialog(QWidget * parent): QDialog(parent) {
	ui = new Ui::IODeviceEditDialog();
	ui->setupUi(this);
	PICodeInfo::EnumInfo * ei = PICODEINFO::enums().value("PIIODevice::DeviceMode");
	if (ei) {
		for (const auto & e: ei->members)
#if PIP_VERSION >= PIP_MAKE_VERSION(2, 39, 0)
			ui->comboMode->addItem(PI2QString(e.name.toString() + " (" + PIString::fromNumber(e.value) + ")"),
			                       QVariant::fromValue<int>(e.value));
#else
			ui->comboMode->addItem(PI2QString(e.name + " (" + PIString::fromNumber(e.value) + ")"), QVariant::fromValue<int>(e.value));
#endif
	}
	ui->comboMode->setCurrentIndex(ui->comboMode->count() - 1);
	ei = PICODEINFO::enums().value("PIIODevice::DeviceOption");
	if (ei) {
		for (const auto & e: ei->members) {
			QCheckBox * cb = new QCheckBox();
#if PIP_VERSION >= PIP_MAKE_VERSION(2, 39, 0)
			cb->setText(PI2QString(e.name.toString() + " (" + PIString::fromNumber(e.value) + ")"));
#else
			cb->setText(PI2QString(e.name + " (" + PIString::fromNumber(e.value) + ")"));
#endif
			cb->setProperty("__value", e.value);
			ui->layoutOptions->addWidget(cb);
		}
	}
	PIStringList pl = PIIODevice::availablePrefixes();
	for (const auto & 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());
	auto * d        = PIIODevice::createFromFullPath(prefix + "://");
	if (d) {
		ps = PI2QPropertyStorage(d->constructVariant().get());
		ui->widgetProperties->setStorage(&ps);
		delete d;
	}
}


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);
}
