#include "actorsettingsdialog.h"
#include "ui_actorsettingsdialog.h"
#include "itemsettingsdialog.h"
#include "scripteditordialog.h"
#include <QDebug>
#include <QSpinBox>
#include <QPushButton>

ActorSettingsDialog::ActorSettingsDialog(std::shared_ptr<AlarmTime> alarm, QWidget *parent):
	QDialog(parent),
	actor_(alarm),
	ui(new Ui::ActorSettingsDialog)
{
	init();

	widget = new AlarmWidget(alarm, this);
	ui->vertlayout->addWidget(widget);
}

ActorSettingsDialog::ActorSettingsDialog(std::shared_ptr<SensorActor> actor, QWidget *parent) :
	QDialog(parent),
	actor_(actor),
	ui(new Ui::ActorSettingsDialog)
{
	init();

	widget = new SensorActorWidget(actor, &globalSensors, this);
	ui->vertlayout->addWidget(widget);
}

ActorSettingsDialog::ActorSettingsDialog(std::shared_ptr<Regulator> actor, QWidget *parent) :
	QDialog(parent),
	actor_(actor),
	ui(new Ui::ActorSettingsDialog)
{
	init();

	widget = new RegulatorWdiget(actor, &globalSensors, this);
	ui->vertlayout->addWidget(widget);
}

ActorSettingsDialog::ActorSettingsDialog(std::shared_ptr<TimerActor> actor, QWidget *parent) :
	QDialog(parent),
	actor_(actor),
	ui(new Ui::ActorSettingsDialog)
{
	init();

	widget = new TimerActorWidget(actor, this);
	ui->vertlayout->addWidget(widget);
}

ActorSettingsDialog::ActorSettingsDialog(std::shared_ptr<PolynomalActor> actor, QWidget *parent) :
	QDialog(parent),
	actor_(actor),
	ui(new Ui::ActorSettingsDialog)
{
	init();

	widget = new PolynomalActorWidget(actor, &globalSensors, this);
	ui->vertlayout->addWidget(widget);
}

ActorSettingsDialog::ActorSettingsDialog(std::shared_ptr<ScriptActor> actor, QWidget *parent) :
	QDialog(parent),
	actor_(actor),
	ui(new Ui::ActorSettingsDialog)
{
	init();

	widget = new ScriptActorWidget(actor, &globalSensors, this);
	ui->vertlayout->addWidget(widget);

	// Add "Edit Script" button for ScriptActor
	QPushButton* editScriptButton = new QPushButton("Edit Script", this);
	ui->vertlayout->addWidget(editScriptButton);
	connect(editScriptButton, &QPushButton::clicked, this, &ActorSettingsDialog::editScript);
}

ActorSettingsDialog::ActorSettingsDialog(std::shared_ptr<Actor> actor, QWidget *parent) :
	QDialog(parent),
	actor_(actor),
	ui(new Ui::ActorSettingsDialog)
{
	init();
}

void ActorSettingsDialog::init()
{
	ui->setupUi(this);
	connect(ui->comboBox_action, SIGNAL(currentIndexChanged(int)), this, SLOT(changeAction(int)));
	connect(ui->spinBox,  SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
	connect(ui->pushButton_editItem, &QPushButton::clicked, this, &ActorSettingsDialog::editAsItem);
	connect(ui->pushButton_enable, &QPushButton::clicked, this, &ActorSettingsDialog::setEnabled);
	ui->spinBox->hide();

	ui->spinBox->setValue(actor_->getTriggerValue());
	if(actor_->getTriggerValue() == 0)
		ui->comboBox_action->setCurrentIndex(0);
	else if(actor_->getTriggerValue() == 1)
		ui->comboBox_action->setCurrentIndex(1);
	else
		ui->comboBox_action->setCurrentIndex(2);

	ui->label_Exausted->setText(actor_->isExausted() ? "True" : "False");
	ui->label_Enabled->setText(actor_->isActive() ? "True" : "False");
	ui->pushButton_enable->setText(actor_->isActive() ? "Disable" : "Enable");
}

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

void ActorSettingsDialog::editAsItem()
{
	setModal(false);
	ItemSettingsDialog itemSettingsDiag(actor_, false, this);
	itemSettingsDiag.setModal(false);
	itemSettingsDiag.exec();
	setModal(true);
}

void ActorSettingsDialog::setEnabled()
{
	actor_->setActive(!actor_->isActive());
	ui->label_Enabled->setText(actor_->isActive() ? "True" : "False");
	ui->pushButton_enable->setText(actor_->isActive() ? "Disable" : "Enable");
}

void ActorSettingsDialog::valueChanged(int value)
{
	actor_->setTriggerValue(value);
}

void ActorSettingsDialog::hideCancle([[maybe_unused]] const bool hide)
{

}

void ActorSettingsDialog::changeAction(int index)
{
	if(index == 0) actor_->setTriggerValue(0);
	else if(index == 1) actor_->setTriggerValue(1);
	else if(index == 2)
	{
		ui->spinBox->show();
		actor_->setTriggerValue(ui->spinBox->value());
	}
	if(index != 2)ui->spinBox->hide();
}

void ActorSettingsDialog::editScript()
{
	std::shared_ptr<ScriptActor> scriptActor = std::dynamic_pointer_cast<ScriptActor>(actor_);
	if(scriptActor)
	{
		setModal(false);
		ScriptEditorDialog dialog(scriptActor.get(), this);
		dialog.setModal(true);
		dialog.exec();
		setModal(true);
	}
}

