#include "patch.h"
#include "models/settings.h"
#include "models/filesystem.h"
#include "models/lotrodatmanager.h"

#include <QUrlQuery>

Patch::Patch(QString patch_name, LotroDatManager* mgr, QObject *parent)
    : QObject(parent)
    , patch_name_(patch_name)
    , lotro_mgr_(mgr)
{
    status_.process = CurrentProcess::E_FINISHED;
    status_.percent = 0;
    status_.debug_msg = "";
}

bool Patch::needDownloadDatabase(QString db_name)
{
    QString patch_filename = Settings::getValue("PatchDatabases/" + db_name + "/path").toString();
    QString current_file_hash = FileSystem::fileHash(patch_filename);
    QString required_file_hash = Settings::getValue("PatchDatabases/" + db_name + "/hashsum").toString();
    return isDatabaseDownloadEnabled(db_name) && (current_file_hash != required_file_hash);
}

bool Patch::isDatabaseDownloadEnabled(QString db_name)
{
    if (db_name == "text") {
        return Settings::getValue("Components/texts_main").toBool() ||
               Settings::getValue("Components/texts_items").toBool() ||
               Settings::getValue("Components/texts_emotes").toBool();
    }

    if (db_name == "font") {
        return Settings::getValue("Components/fonts").toBool();
    }

    if (db_name == "image") {
        return Settings::getValue("Components/maps").toBool();
    }

    if (db_name == "texture") {
        return Settings::getValue("Components/logos").toBool();
    }

    if (db_name == "loadscreen") {
        return Settings::getValue("Components/loadscreens").toBool();
    }

    if (db_name == "sound") {
        return Settings::getValue("Components/sounds").toBool();
    }

    if (db_name == "video") {
        return Settings::getValue("Components/video").toBool();
    }

    return false;
}

QString Patch::getPatchName()
{
    return patch_name_;
}
