#include "../misc/fixc11.h"
#include "PlotWiFiScan.h"

#include <QPainter>
#include <QStaticText>
#include "../UIHelper.h"

PlotWiFiScan::PlotWiFiScan(QWidget *parent) : QWidget(parent) {

	setMinimumWidth(96);
	setMinimumHeight(96);


	//setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

//	setMaximumHeight(300);
//	sets
	//setAutoFillBackground(false);

}

void PlotWiFiScan::add(const Timestamp ts, const WiFiMeasurements& data) {
	(void) ts;
	this->data = data;
	QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
}

void PlotWiFiScan::paintEvent(QPaintEvent* evt) {

	(void) evt;
	QPainter p(this);

	const int x0 = 4;	const int xw = UIHelper::getWifiLabelDistX(this->parent());
	const int y0 = 3;
	const int lh = UIHelper::getWifiLabelDistY(this->parent());

	int x = x0;
	int y = y0;

	int w = width();
	int h = height();

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

	const QFont font("Arial", 9);
	p.setFont(font);
	p.setPen(Qt::black);

	for (const WiFiMeasurement& m : data.entries) {
		const std::string& mac = m.getAP().getMAC().asString();
		std::string str = mac + ": " + std::to_string((int)m.getRSSI());
		p.drawStaticText(x, y, QStaticText(str.c_str()));
		y += lh;
		if (y > this->height()-10) {y = y0; x += xw;}
	}

	p.end();

}
