#include "legacyapp.h"
#include "filesystem.h"

#include <QtConcurrent/QtConcurrent>
#include <QFontDatabase>

LegacyApp::LegacyApp(QObject *parent) : QObject(parent), window(this),
    client_local_dat(), client_local_dat_busy(false),
    properties(qApp->applicationDirPath() + "/legacy_v2.ini", QSettings::IniFormat)
{
    properties.setIniCodec("UTF-8");
}

void LegacyApp::Init()
{
    QResource::registerResource(QApplication::applicationDirPath() + "/data01.gtr");
    QResource::registerResource(QApplication::applicationDirPath() + "/data02.gtr");

    QFontDatabase::addApplicationFont(":/assets/fonts/trajan.ttf");
    QFontDatabase::addApplicationFont(":/assets/fonts/viking.ttf");
    QFontDatabase::addApplicationFont(":/assets/fonts/title.ttf");


    window.Init();
    StartDatFilesInitialisation();
}

void LegacyApp::StartDatFilesInitialisation()
{
    QString lotro_dir_path = properties.value("settings/lotro_folder", "none").toString();
    QString locale = properties.value("lotro/locale", "English").toString();

    qDebug() << "Initialising file " << lotro_dir_path + "/client_local_" + locale + ".dat";

    QtConcurrent::run([this, lotro_dir_path, locale](){
        if (client_local_dat_busy == false) {
            client_local_dat_busy = true;
            client_local_dat.Initialise((lotro_dir_path + "/client_local_" + locale + ".dat").toStdString(), 0);
            client_local_dat_busy = false;
        }
    });
}

void LegacyApp::DownloadFinished()
{
    // TODO
}
