#include "regulatorwdiget.h"
#include "ui_regulatorwdiget.h"
#include <QDebug>

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



RegulatorWdiget::RegulatorWdiget(std::shared_ptr<Regulator> regulator, SensorStore* sensors, QWidget *parent) :
	QWidget(parent),
	regulator_(regulator),
	sensors_(sensors),
	ui(new Ui::RegulatorWdiget)
{
	ui->setupUi(this);
	if(sensors)ui->listView->sensorsChanged(*(sensors->getSensors()));
	else
	{
		ui->listView->hide();
		ui->label->hide();
	}
	ui->doubleSpinBox_setPoint->setValue(regulator->getSetPoint());
	ui->doubleSpinBox_band->setValue(regulator->getBand());
	ui->spinBox_safe->setValue(regulator_->getSafeValue());
	ui->spinBox_timeout->setValue(regulator_->getTimeout());

	connect(ui->listView, &SensorListWidget::clicked, this, &RegulatorWdiget::setSensor);
	connect(ui->doubleSpinBox_setPoint, SIGNAL(valueChanged(double)),  this, SLOT(setPoint(double)));
	connect(ui->doubleSpinBox_band, SIGNAL(valueChanged(double)), this, SLOT(setBand(double)));
	connect(ui->spinBox_safe, SIGNAL(valueChanged(int)), regulator_.get(), SLOT(setSafeValue(int)));
	connect(ui->spinBox_timeout, SIGNAL(valueChanged(int)), regulator_.get(), SLOT(setTimeout(int)));
}


void RegulatorWdiget::setPoint(double in)
{
	regulator_->setPoint(in);
}

void RegulatorWdiget::setBand(double band)
{
	regulator_->setBand(band);
}

void RegulatorWdiget::setSensor(const QModelIndex &index)
{
	regulator_->setSensor(ui->listView->getSensorForIndex(index));
	setPoint(ui->listView->getSensorForIndex(index).field);
	ui->doubleSpinBox_setPoint->setValue(ui->listView->getSensorForIndex(index).field);
}
