#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QLocale>
#include <QTextCodec>
#include <QTranslator>
#include <QPalette>
#include <QFontDatabase>

#include "Logger.h"
#include "CanController.h"
#include "BatteryController.h"

int main(int argc, char *argv[])
{
	qSetMessagePattern(logMessagePattern());
	qInstallMessageHandler(logMessageHandler);

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

	QGuiApplication app(argc, argv);

	QCoreApplication::setOrganizationName("Tksi");
	QCoreApplication::setOrganizationDomain("Tksi.rus");
	QCoreApplication::setApplicationName("Tksi monitor");

	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Black.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-BlackItalic.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Bold.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-BoldItalic.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Italic.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Light.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-LightItalic.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Medium.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-MediumItalic.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Regular.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-Thin.ttf");
	QFontDatabase::addApplicationFont(":/fonts/Roboto/Roboto-ThinItalic.ttf");

	QTranslator translator;
	if (translator.load(":/i18n/StandBatteryView_ru_RU"))
	{
		app.installTranslator(&translator);
	}

	QQmlApplicationEngine engine;
	qmlRegisterSingletonType<CanController>("Core", 1, 0, "CanController", &CanController::qmlInstance);
	qmlRegisterType<BatteryController>("Core", 1, 0, "BatteryController");

	const QUrl url(QStringLiteral("qrc:/Main.qml"));
	QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl)
					 {
						 if (!obj && url == objUrl)
						 {
							 QCoreApplication::exit(-1);
						 }
					 }, Qt::QueuedConnection);

	engine.addImportPath(QStringLiteral("qrc:/"));
	engine.load(url);

	return app.exec();
}
