#include <QQmlApplicationEngine>

#include <QtCore/QDebug>
#include <QtCore/QCommandLineParser>
#include <QtCore/QUrlQuery>
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
#include <QtQuick/QQuickWindow>
#include <navigation.h>
#include <vehiclesignals.h>
#include <QScreen>

#include "navigation_client.h"
#include "qcheapruler.hpp"
#include "file_operation.h"
#include "shell-desktop.h"

#include <qpa/qplatformnativeinterface.h>
#include <wayland-client.h>

#include "wayland-agl-shell-desktop-client-protocol.h"

#define OUTPUT_ID	"remoting-remote-1"

static void
global_add(void *data, struct wl_registry *reg, uint32_t name,
	   const char *interface, uint32_t)
{
	struct agl_shell_desktop **shell = static_cast<struct agl_shell_desktop **>(data);

	if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
		*shell = static_cast<struct agl_shell_desktop *>(
			wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
		);
	}
}

static void
global_remove(void *data, struct wl_registry *reg, uint32_t id)
{
	/* Don't care */
	(void) data;
	(void) reg;
	(void) id;
}

static const struct wl_registry_listener registry_listener = {
	global_add,
	global_remove,
};

static struct agl_shell_desktop *
register_agl_shell_desktop(QPlatformNativeInterface *native)
{
	struct wl_display *wl;
	struct wl_registry *registry;
	struct agl_shell_desktop *shell = nullptr;

	wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
	registry = wl_display_get_registry(wl);

	wl_registry_add_listener(registry, &registry_listener, &shell);

	/* Roundtrip to get all globals advertised by the compositor */
	wl_display_roundtrip(wl);
	wl_registry_destroy(registry);

	return shell;
}


static QScreen *
find_qscreen(const char *screen_name)
{
	QList<QScreen *> screens = qApp->screens();
	QScreen *found = nullptr;
	QString qstr_name = QString::fromUtf8(screen_name, -1);

	for (QScreen *xscreen : screens) {
		if (qstr_name == xscreen->name()) {
			found = xscreen;
			break;
		}
	}

	return found;
}

int main(int argc, char *argv[])
{
	QString graphic_role = QString("tbtnavi");
	struct agl_shell_desktop *agl_shell_desktop = nullptr;

	setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);

	QGuiApplication app(argc, argv);
	QCoreApplication::setOrganizationDomain("automotivelinux.org");
	QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
	QCoreApplication::setApplicationName(graphic_role);
	QCoreApplication::setApplicationVersion("0.1.0");
	app.setDesktopFileName(graphic_role);

	QPlatformNativeInterface *native = qApp->platformNativeInterface();
	agl_shell_desktop = register_agl_shell_desktop(native);
	if (!agl_shell_desktop) {
		qDebug() << "Could not find agl_shell_desktop extension. Is agl-compositor running?";
		exit(EXIT_FAILURE);
	}

	std::shared_ptr<struct agl_shell_desktop> shell{agl_shell_desktop, agl_shell_desktop_destroy};
	Shell *aglShell = new Shell(shell, nullptr);

	/* inform the compositor that the window be placed on a different
	 * output */
	QScreen *screen_to_put = find_qscreen(OUTPUT_ID);
	if (screen_to_put) {
		qDebug() << "Found screen to put " << screen_to_put->name();
		aglShell->set_window_on_screen(screen_to_put, graphic_role);
	} else {
		qDebug() << "Couldn't find screen to put " << OUTPUT_ID;
		qDebug() << "Available screens: ";
		for (auto &ss: qApp->screens()) {
			qDebug() << "screen: " << ss->name();
		}
	}

	// Load qml
	QQmlApplicationEngine engine;

	qmlRegisterType<QCheapRuler>("com.mapbox.cheap_ruler", 1, 0, "CheapRuler");

	QQmlContext *context = engine.rootContext();

	File_Operation file;
	context->setContextProperty("fileOperation", &file);

	VehicleSignalsConfig vsConfig("tbtnavi");
	VehicleSignals *vs = new VehicleSignals(vsConfig);
	if (!vs) {
		qFatal("Could not create VehicleSignals!");
	}
	context->setContextProperty("VehicleSignals", vs);

	// Give the navigation client it's own vehicle signals connection
	// to simplify state management wrt QML initialization, and keep the
	// notification streams separate.
	Navigation *navigation = new Navigation(new VehicleSignals(vsConfig), false, context);

	engine.load(QUrl(QStringLiteral("qrc:qml/Main.qml")));

	QObject *root = engine.rootObjects().first();
	new navigation_client(navigation, root->findChild<QObject*>("mapwindow"));

	return app.exec();
}