#ifndef STEPLOGGERWRAPPER_H
#define STEPLOGGERWRAPPER_H

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

#include <QWidget>
#include <QDialog>
#include <QPushButton>
#include <QWindow>
#include <QMainWindow>
#include <QDesktopWidget>
#include <QGridLayout>
#include <QLabel>

#include <Indoor/data/Timestamp.h>

/**
 * 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;

	QWidget* widget = nullptr;
	QPushButton* btn = nullptr;
	QLabel* lblInfo = nullptr;
	QWidget* parent;// = nullptr;
	int cnt = 0;
	Timestamp lastEstTS;

public:

	/** ctor */
	StepLoggerWrapper(IPINScaler& scaler, QWidget* parent) : scaler(scaler), parent(parent) {
		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);

		++cnt;
		const Timestamp curTS = Timestamp::fromUnixTime();
		const Timestamp diff = curTS - lastEstTS;
		lastEstTS = curTS;
		lblInfo->setText("Est: " + QString::number(cnt) + " time: " + QString::number(diff.ms()));

	}

private:

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

		// almost fullscreen
		QRect geom = QDesktopWidget().availableGeometry();
		const int w = std::min(geom.width(), geom.height()) * 0.4;
		const int h = w;

		/*
		window = new QDialog();

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


		btn = new QPushButton(window);
		btn->setGeometry(5,5,w-5-5,h-5-5);
		*/


		widget = new QWidget(parent);
		widget->setGeometry(geom.width() - w - 5, geom.height()*0.85 - h, w, h);
		widget->setWindowFlags(widget->windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
		widget->setVisible(true);

		QGridLayout* lay = new QGridLayout(widget);

		btn = new QPushButton();
		btn->setMinimumSize(w-10, h*0.8);
		btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
		btn->setWindowFlags(btn->windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);

		lay->addWidget(btn, 0, 0, 1, 1);

		lblInfo = new QLabel();
		lblInfo->setMinimumHeight(32); lblInfo->setMaximumHeight(32);
		lblInfo->setText("123");
		lay->addWidget(lblInfo, 1, 0, 1, 1);

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

		// show the current waypoint
		updateButton();

//		//window->exec();
//		window->setFocusPolicy(Qt::StrongFocus);
//		window->setAttribute(Qt::WA_AcceptTouchEvents, true);
//		window->show();

	}

	/** 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
