#include <QApplication>
#include <QMessageBox>
#include <QLockFile>
#include <QDir>

#include "widgets/mainwindow.h"

#include <QTextCodec>
#include <QDebug>
#include <QResource>
#include <QFontDatabase>

// Global. Should be updated only by MainWindow!!!
double window_height = 1000;
double window_width = 648;

int main(int argc, char *argv[])
{
    setlocale(LC_ALL,"Russian");
    QApplication a(argc, argv);

    qDebug() << "Checking if there's another instance of Legacy";

    QLockFile lockFile(QDir::temp().absoluteFilePath("rulotro.lock"));
    if(!lockFile.tryLock(1)){
        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText("Приложение уже запущено.\nРазрешено запускать только один экземпляр приложения.");
        msgBox.exec();
        return 1;
    }

    qDebug() << "Initialising fonts and resources";
    QResource::registerResource(QApplication::applicationDirPath() + "/data01.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/data02.gtr");

    QFontDatabase::addApplicationFont(":/fonts/trpro.ttf");
    QFontDatabase::addApplicationFont(":/fonts/CrimsonText.ttf");
    QFontDatabase::addApplicationFont(":/fonts/EBGaramond.ttf");
    QFontDatabase::addApplicationFont(":/fonts/aniron.ttf");

    qDebug() << "Starting main frame initialisation...";

    MainWindow* main_window = new MainWindow(nullptr);
    main_window->setAttribute(Qt::WA_DeleteOnClose);

    qDebug() << "Finished initialisation, starting application...";
    return a.exec();
}
