#include "messageitemsettingswidget.h"
#include "ui_messageitemsettingswidget.h"

#include <QFileDialog>
#include <QDebug>

MessageItemSettingsWidget::MessageItemSettingsWidget(std::weak_ptr<MessageItem> item, QWidget *parent) :
	QWidget(parent),
	item_(item),
	ui(new Ui::MessageItemSettingsWidget)
{
	ui->setupUi(this);
	qDebug()<<"test";
	if(auto workingItem = item_.lock())
	{
		ui->lineEdit->setText(workingItem->getMessage());
		ui->lineEdit_alert->setText(workingItem->getAlert());
	}

	connect(ui->lineEdit, &QLineEdit::textChanged, this, &MessageItemSettingsWidget::setText);
	connect(ui->lineEdit_alert, &QLineEdit::textChanged, this, &MessageItemSettingsWidget::setAlert);
	connect(ui->pushButton, &QPushButton::pressed, [this]()
	{
		ui->lineEdit_alert->setText(QFileDialog::getOpenFileName(this, "Choose File"));
	});
}


void MessageItemSettingsWidget::setText(const QString& text)
{
	if(auto workingItem = item_.lock())
	{
		workingItem->setMessage(text);
	}
}

void MessageItemSettingsWidget::setAlert(const QString &in)
{
	if(auto workingItem = item_.lock())
	{
		workingItem->setAlert(in);
	}
}

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