#include "scriptactorwidget.h"
#include "ui_scriptactorwidget.h"
#include <QFileDialog>
#include <QMessageBox>

ScriptActorWidget::ScriptActorWidget(std::shared_ptr<ScriptActor> scriptActor, SensorStore* sensors,
                                     QWidget *parent) :
	QWidget(parent),
	scriptActor_(scriptActor),
	sensors_(sensors),
	ui(new Ui::ScriptActorWidget)
{
	ui->setupUi(this);

	// Load current values
	ui->checkBox_watchParent->setChecked(scriptActor_->getWatchParentItem());
	ui->lineEdit_scriptPath->setText(scriptActor_->getScriptPath());

	// Populate sensor list
	for(const auto& sensor : scriptActor_->getSensors())
	{
		ui->listWidget_sensors->addItem(sensor.name + " (Type: " + QString::number(sensor.type) + ", ID: " + QString::number(sensor.id) + ")");
	}

	// Connect signals
	connect(ui->checkBox_watchParent, &QCheckBox::toggled, this, &ScriptActorWidget::setWatchParentItem);
	connect(ui->lineEdit_scriptPath, &QLineEdit::editingFinished, this, &ScriptActorWidget::setScriptPath);
	connect(ui->pushButton_browse, &QPushButton::clicked, this, &ScriptActorWidget::setScriptPath);
	connect(ui->pushButton_addSensor, &QPushButton::clicked, this, &ScriptActorWidget::addSensor);
	connect(ui->pushButton_removeSensor, &QPushButton::clicked, this, &ScriptActorWidget::removeSelectedSensor);
}

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

void ScriptActorWidget::setWatchParentItem(bool checked)
{
	scriptActor_->setWatchParentItem(checked);
}

void ScriptActorWidget::setScriptPath()
{
	QString path = ui->lineEdit_scriptPath->text();
	scriptActor_->setScriptPath(path);
}

void ScriptActorWidget::addSensor()
{
	// Show a dialog to select a sensor
	QDialog dialog(this);
	dialog.setWindowTitle("Select Sensor");
	QVBoxLayout layout(&dialog);
	QListWidget listWidget;
	for(const auto& sensor : *sensors_->getSensors())
	{
		listWidget.addItem(sensor.name + " (Type: " + QString::number(sensor.type) + ", ID: " + QString::number(sensor.id) + ") [" + QString::number(sensor.field) + "]");
	}
	layout.addWidget(&listWidget);
	QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
	layout.addWidget(&buttonBox);
	connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
	connect(&buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);

	if(dialog.exec() == QDialog::Accepted && listWidget.currentItem())
	{
		int index = listWidget.currentRow();
		if(index >= 0 && index < static_cast<int>(sensors_->getSensors()->size()))
		{
			const Sensor& sensor = (*sensors_->getSensors())[index];
			scriptActor_->addSensor(sensor);
			ui->listWidget_sensors->addItem(sensor.name + " (Type: " + QString::number(sensor.type) + ", ID: " + QString::number(sensor.id) + ")");
		}
	}
}

void ScriptActorWidget::removeSelectedSensor()
{
	int row = ui->listWidget_sensors->currentRow();
	if(row >= 0)
	{
		QListWidgetItem* item = ui->listWidget_sensors->takeItem(row);
		if(item)
		{
			QString text = item->text();
			// Extract sensor type and ID from the text
			QRegularExpression regex("Type: (\\d+), ID: (\\d+)");
			QRegularExpressionMatch match = regex.match(text);
			if(match.hasMatch())
			{
				Sensor sensor;
				sensor.type = static_cast<Sensor::sensor_type_t>(match.captured(1).toInt());
				sensor.id = match.captured(2).toULongLong();
				scriptActor_->removeSensor(sensor);
			}
			delete item;
		}
	}
}