#include "sensoractorwidget.h"
#include "ui_sensoractorwidget.h"

#include <QDebug>

SensorActorWidget::SensorActorWidget(std::shared_ptr<SensorActor> sensorActor, SensorStore* sensors, QWidget *parent) :
	QWidget(parent),
	sensorActor_(sensorActor),
	sensors_(sensors),
	ui(new Ui::SensorActorWidget)
{
	ui->setupUi(this);
	if(sensors)ui->listView->sensorsChanged(*(sensors->getSensors()));
	else
	{
		ui->listView->hide();
		ui->label->hide();
	}

	if(sensorActor_->getSloap() == SensorActor::SLOPE_UP)
		ui->comboBox_slope->setCurrentIndex(0);
	else if(sensorActor_->getSloap() == SensorActor::SLOPE_DOWN)
		ui->comboBox_slope->setCurrentIndex(1);
	else if(sensorActor_->getSloap() == SensorActor::SLOPE_BOTH)
		ui->comboBox_slope->setCurrentIndex(2);

	ui->doubleSpinBox_threshold->setValue(sensorActor_->getThreshold());

	connect(ui->listView, &SensorListWidget::clicked, this, &SensorActorWidget::setSensor);
	connect(ui->doubleSpinBox_threshold, SIGNAL(valueChanged(double)),  this, SLOT(setThreshold(double)));
	connect(ui->comboBox_slope, SIGNAL(currentIndexChanged(int)), this, SLOT(setSlope(int)));
}

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

void SensorActorWidget::setThreshold(double in)
{
	sensorActor_->setThreshold(in);
}

void SensorActorWidget::setSlope(int index)
{
	if(index == 0) sensorActor_->setSloap(SensorActor::SLOPE_UP);
	else if(index == 1) sensorActor_->setSloap(SensorActor::SLOPE_DOWN);
	else if(index == 2) sensorActor_->setSloap(SensorActor::SLOPE_BOTH);
}

void SensorActorWidget::setSensor(const QModelIndex &index)
{
	sensorActor_->setSensor(ui->listView->getSensorForIndex(index));
}
