#include <QApplication>
//#include <QDesktopWidget>
#include <QMessageBox>
#include <QFileInfo>
#include <QDir>
#include <QIcon>
#include <QTimer>
#include <QFontDatabase>
#include <QStyleFactory>
#include <qad_locations.h>
#include <qpiconfig.h>
#include <picli.h>
#include <piqt.h>
#include <picrypt.h>
#include <pisysteminfo.h>
#include <session_manager.h>
#include "SH_ui_loader.h"
#include "SH_base.h"
#include "pultwindow_desktop.h"
#include "pultwindow_android.h"
#include "client.h"
#include "client_messages.h"
#ifdef ANDROID
#  include "android.h"
#  include <QAndroidService>
#  include <QAndroidJniObject>
#else
#  include "desktop.h"
#endif

typedef
#ifdef ANDROID
		PultWindowAndroid
#else
		PultWindowDesktop
#endif
						SHSMainWindow;



#  include "shs_graphic.h"
int main(int argc, char * argv[]) {
	initAbout();
	/*QApplication _app(argc, argv);
	QWidget _wnd;
	_wnd.show();
	_wnd.setLayout(new QBoxLayout(QBoxLayout::TopToBottom));
	_wnd.layout()->addWidget(new Graphic());
	_wnd.layout()->addWidget(new SHSGraphic());
	return _app.exec();*/

#ifdef ANDROID
	qDebug() << "!!! start !!!";
	ANDROID_LAYER->init();
	bool service = false;
	for (int i = 0; i < argc; ++i) {
		qDebug() << i << argv[i];
		if (QString(argv[i]).endsWith("service")) {
			service = true;
			break;
		}
	}
	if (service) {
		QAndroidService service(argc, argv);
		QApplication::setApplicationName("Pult_remote_messages");
		QAD::loadTranslations();

		//RemoteClientMessages messages;
		//QObject::connect(&messages, SIGNAL(status(QString)), ANDROID_LAYER, SLOT(status(QString)));

		//ANDROID_LAYER->notify("service started");
		qDebug() << "!!! start service !!!";
		return service.exec();
	}
	qDebug() << "!!! start ui !!!";
#endif

	QApplication app(argc, argv);
	QAD_SETUP_APPLICATION;
	QApplication::setStyle(QStyleFactory::create("Fusion"));
#if defined(MAC_OS) || defined(MOBILE_VIEW)
#endif
	QAD::loadTranslations();
	QStringList fonts = QDir(":/fonts/").entryList(QStringList() << "*.ttf", QDir::Files | QDir::NoDotDot | QDir::NoDot);
	foreach (const QString & f, fonts) {
		QFontDatabase::addApplicationFont(":/fonts/" + f);
	}
	enableHighDPI();
	app.setWindowIcon(QIcon(":/icons/SH_pult_remote.png"));

	PIString addr;
#ifndef ANDROID
	app.setQuitOnLastWindowClosed(false);
	PICLI cli(argc, argv);
	cli.addArgument("address", true);
	cli.addArgument("fullscreen");
	cli.addArgument("no-cursor", PIChar('\0'));
	if (cli.hasArgument("address"))
		addr = cli.argumentValue("address");
#endif
	SHSMainWindow * view = new SHSMainWindow();
	RemoteClient client(view);
#ifdef ANDROID
	QObject::connect(&client, SIGNAL(packageExists(QString)), ANDROID_LAYER, SLOT(packageExists(QString)));
	QObject::connect(&client, SIGNAL(packageReady(QString, bool)), ANDROID_LAYER, SLOT(packageReady(QString, bool)));
#else
	QObject::connect(&client, SIGNAL(packageExists(QString)), view, SLOT(packageExists(QString)));
	QObject::connect(&client, SIGNAL(packageReady(QString, bool)), view, SLOT(packageReady(QString, bool)));
#endif

	SessionManager session(QAD::userPath(QAD::ltConfig, "session"));
#ifdef ANDROID
	//ANDROID_LAYER->startService(); // WARNING!
	QObject::connect(&client, SIGNAL(serverConnected(PacketServerInfo)), ANDROID_LAYER, SLOT(connectedToServer(PacketServerInfo)));
	//QObject::connect(&client, SIGNAL(statusChanged(QString)), ANDROID_LAYER, SLOT(status(QString)));
	QObject::connect(view, SIGNAL(dialogRequest(QString)), ANDROID_LAYER, SLOT(showDialog(QString)));
	QObject::connect(ANDROID_LAYER, SIGNAL(dialogDone(QString)), view, SIGNAL(addressEntered(QString)));
	QObject::connect(ANDROID_LAYER, &AndroidLayer::activityStarted, [&client](QString addr){client.connect(addr);});
	QTimer::singleShot(0, ANDROID_LAYER, SLOT(queryIntent()));
#else
	DESKTOP_LAYER->setPultWindow(view);
	DESKTOP_LAYER->setPultTray(view->trayIcon());
	//RemoteClientMessages messages;
	//QObject::connect(&client, SIGNAL(serverConnected(PacketServerInfo)), &messages, SLOT(connectedToServer(PacketServerInfo)));
	session.addMainWidget(view);
#endif
	view->addToExternalSession(session);
	QObject::connect(view, &PultWindowBase::saveExternalSession, view, [&session](){session.save();});
	session.load();
#ifndef ANDROID
	if (cli.hasArgument("fullscreen"))
		view->showFullScreen();
	else
		view->show();
	if (cli.hasArgument("no-cursor"))
		QApplication::setOverrideCursor(Qt::BlankCursor);
#else
	view->show();
#endif

	//QMessageBox::information(0, "", app.arguments().join("\n\n"));

	if (!addr.isEmpty())
		client.connect(PI2QString(addr));
	//view->showFullScreen();

	QObject::connect(qApp, &QApplication::aboutToQuit, [&view,&client,&session](){
		view->saveSession();
		session.save();
		session.clear();
		//qDebug() << "~main 1";
		client.stop();
		//qDebug() << "~main 2";
		client.setForm(0, 0);
		//qDebug() << "~main 3";
		QAD::unloadTranslations();
		if (view) delete view;
		//qDebug() << "~main 4";
	});

	int ret = app.exec();

	//qDebug() << "ret" << ret;
#ifndef ANDROID
	//messages.disconnect();
#endif

	return ret;
}
