#include "itemcreationdialog.h"
#include "ui_itemcreationdialog.h"

#include "itemsettingswidgets/messageitemsettingswidget.h"
#include "itemsettingswidgets/systemitemsettingswidget.h"
#include "itemsettingswidgets/mqttitemsettingswidget.h"
#include "items/mqttitem.h"

ItemCreationDialog::ItemCreationDialog(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::ItemCreationDialog)
{
	ui->setupUi(this);
	std::shared_ptr<MessageItem> messageItem(new MessageItem);
	item = messageItem;
	widget = new MessageItemSettingsWidget(messageItem, this);
	ui->verticalLayout->addWidget(widget);
	connect(ui->comboBox, &QComboBox::currentTextChanged, this, &ItemCreationDialog::itemTypeChanged);
	connect(ui->lineEdit, &QLineEdit::textChanged, this, &ItemCreationDialog::itemNameChanged);
}

ItemCreationDialog::~ItemCreationDialog()
{
	delete ui;
	delete widget;
}

void ItemCreationDialog::itemTypeChanged(const QString& type)
{
	ui->verticalLayout->removeWidget(widget);
	delete widget;
	if(type == "Message")
	{
		std::shared_ptr<MessageItem> messageItem(new MessageItem);
		item = messageItem;
		widget = new MessageItemSettingsWidget(messageItem, this);
		ui->verticalLayout->addWidget(widget);
	}
	if(type == "System")
	{
		std::shared_ptr<SystemItem> systemItem(new SystemItem);
		item = systemItem;
		widget = new SystemItemSettingsWidget(systemItem, this);
		ui->verticalLayout->addWidget(widget);
	}
	if(type == "Mqtt")
	{
		std::shared_ptr<MqttItem> mqttItem(new MqttItem);
		item = mqttItem;
		widget = new MqttItemSettingsWidget(mqttItem, this);
		ui->verticalLayout->addWidget(widget);
	}
}

void ItemCreationDialog::itemNameChanged(const QString& name)
{
	if(item)
	{
		item->setName(name);
	}
}

