#include "polynomalactorwidget.h"
#include "ui_polynomalactorwidget.h"

PolynomalActorWidget::PolynomalActorWidget(std::shared_ptr<PolynomalActor> actor, SensorStore* sensors,
        QWidget *parent):
	QWidget(parent),
	sensors_(sensors),
	actor_(actor),
	ui(new Ui::PolynomalActorWidget)
{
	ui->setupUi(this);
	if(sensors)ui->listView->sensorsChanged(*(sensors->getSensors()));
	else
	{
		ui->listView->hide();
		ui->label->hide();
	}

	double pow3, pow2, pow1, pow0;

	actor_->getCoeffiancts(pow3, pow2, pow1, pow0);

	ui->doubleSpinBox_pow0->setValue(pow0);
	ui->doubleSpinBox_pow1->setValue(pow1);
	ui->doubleSpinBox_pow2->setValue(pow2);
	ui->doubleSpinBox_pow3->setValue(pow3);

	connect(ui->doubleSpinBox_pow3, &QDoubleSpinBox::valueChanged, this, &PolynomalActorWidget::setPow);
	connect(ui->doubleSpinBox_pow2, &QDoubleSpinBox::valueChanged, this, &PolynomalActorWidget::setPow);
	connect(ui->doubleSpinBox_pow1, &QDoubleSpinBox::valueChanged, this, &PolynomalActorWidget::setPow);
	connect(ui->doubleSpinBox_pow0, &QDoubleSpinBox::valueChanged, this, &PolynomalActorWidget::setPow);
	connect(ui->listView, &SensorListWidget::clicked, this, &PolynomalActorWidget::setSensor);

}

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

void PolynomalActorWidget::setPow()
{
	actor_->setCoeffiancts(ui->doubleSpinBox_pow3->value(), ui->doubleSpinBox_pow2->value(),
	                       ui->doubleSpinBox_pow1->value(), ui->doubleSpinBox_pow0->value());
}

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