#ifndef STEPLOGGERWRAPPER_H
#define STEPLOGGERWRAPPER_H

#include "Scaler.h"
#include "StepLogger.h"
#include "../nav/NavControllerListener.h"

#include <QWidget>
#include <QPushButton>
#include <QWindow>
#include <QMainWindow>
#include <QDesktopWidget>

/**
 * helper class to connect our NavigationController with the StepLogger
 */
class StepLoggerWrapper : public StepLogger, public NavControllerListener {

private:

	/** convert from our position-format to ipin position-format */
	IPINScaler& scaler;

	QMainWindow* window;
	QPushButton* btn;

public:

	/** ctor */
	StepLoggerWrapper(IPINScaler& scaler) : scaler(scaler) {
		setupUI();
	}

	/**
	 * event fired every 500ms from the navigation controller.
	 * convert from our format to IPIN format
	 * and pass it to the logger
	 */
	void onNewEstimation(const Point3 pos_m) override {

		// convert from our coordinate system (meter relative to (0,0,0)) to the WGS84 format
		const IPIN ipin = scaler.toIPIN3(pos_m);

		// inform the logger
		StepLogger::onNewEstimation(ipin.lat, ipin.lon, ipin.floorNr);

	}

private:

	/** create a clickable check button */
	void setupUI() {

		window = new QMainWindow();
		//window->setTitle("StepLogger");
		btn = new QPushButton(window);

		// almost fullscreen
		QRect geom = QDesktopWidget().availableGeometry();
		const int w = geom.width() * 0.9;
		const int h = geom.height() * 0.6;

		// center and set always-on-top
		window->setGeometry(geom.width()/2 - w/2, geom.height()/2 - h/2, w, h);
		const Qt::WindowFlags flags = window->windowFlags();
		window->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);


		btn->setGeometry(5,5,w-5-5,h-5-5);
		window->show();

		// button clicked -> next waypoint
		btn->connect(btn, &QPushButton::clicked, [&] () {
			StepLogger::onButtonPress();
			updateButton();
		});

		// show the current waypoint
		updateButton();

	}

	/** update the current button label */
	void updateButton() {

		// show the number of the next waypoint
		const int nr = getCurrentWaypointNumber() + 1;
		btn->setText(QString::number(nr));

		// button is visible when we are not yet done
		btn->setVisible(!StepLogger::isDone());

	}

};

#endif // STEPLOGGERWRAPPER_H
