#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 <signalcomposer.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"

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;

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

	QCommandLineParser parser;
	parser.addPositionalArgument("port", app.translate("main", "port for binding"));
	parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
	parser.addHelpOption();
	parser.addVersionOption();
	parser.process(app);
	QStringList positionalArguments = parser.positionalArguments();
	QUrl bindingAddress;

	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);

	int port = 0;
	QString token;
	if (positionalArguments.length() == 2) {
		port = positionalArguments.takeFirst().toInt();
		token = positionalArguments.takeFirst();
		bindingAddress.setScheme(QStringLiteral("ws"));
		bindingAddress.setHost(QStringLiteral("localhost"));
		bindingAddress.setPort(port);
		bindingAddress.setPath(QStringLiteral("/api"));
		QUrlQuery query;
		query.addQueryItem(QStringLiteral("token"), token);
		bindingAddress.setQuery(query);
	}
	fprintf(stderr, "[tbtnavi] app_name: %s, port: %d, token: %s.\n",
		graphic_role.toStdString().c_str(),
		port,
		token.toStdString().c_str());

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

	Navigation *navigation = new Navigation(bindingAddress, context);

	context->setContextProperty("SignalComposer", new SignalComposer(bindingAddress, 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();
}