#include "../misc/fixc11.h"
#include "PlotTurns.h"
#include <QPainter>

PlotTurns::PlotTurns(QWidget *parent) : QWidget(parent) {

	setMinimumWidth(96);
	setMinimumHeight(96);

	//setSizeIncrement(QSize(1,1));
	//setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
// resize(96, 96);

//	setMaximumWidth(64);
//	setMaximumHeight(64);

}

void PlotTurns::add(const Timestamp ts, const TurnData& data) {
	(void) ts;
	this->turnData = data;
	static int i = 0;
	if (++i % 4 == 0) {
		QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
	}
}

void PlotTurns::add(const Timestamp ts, const CompassData& data) {
	(void) ts;
	this->compassData = data;
	static int i = 0;
	if (++i % 4 == 0) {
		QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
	}
}

void PlotTurns::paintEvent(QPaintEvent* evt) {

	(void) evt;
	QPainter p(this);

	const float s = std::min(width(), height());
	const float s1 = s / 1.9;

	const float cx = width() / 2;
	const float cy = height() / 2;

	p.fillRect(0,0,width(),height(),QColor(255,255,255,192));
	p.setPen(Qt::black);
	p.drawRect(0,0,width()-1,height()-1);

	// turn [relative]
	const float x1 = cx + std::cos(turnData.radSinceStart-M_PI_2) * s1;
	const float y1 = cy + std::sin(turnData.radSinceStart-M_PI_2) * s1;
	const QPen pen1(Qt::black, 2);
	p.setPen(pen1);
	p.drawLine(cx, cy, x1, y1);

	// compass [absolute]
	if (compassData.isValid()) {
		const float x2 = cx + std::cos(compassData.azimuth) * s1;
		const float y2 = cy + std::sin(compassData.azimuth) * s1;
		const QPen pen2(Qt::gray, 2);
		p.setPen(pen2);
		p.drawLine(cx, cy, x2, y2);
	}

	p.end();

}
