#include "translations.h"
#include "letos.h"
#include "services/config.h"
#include <QTranslator>
#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QLibraryInfo>

QHash<QString,QTranslator*> LETOS_TRANSLATIONS;

static QStringList getTranslationDirs()
{
    QStringList translationDirs({"translations"});

    // AppDataLocation, but APPNAME should be a fixed value
    for (const QString& path : QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation))
    {
        translationDirs += QDir::cleanPath(path + "/letos/translations");
        translationDirs += QDir::cleanPath(path + "/sqlitestudio/translations");
    }

    translationDirs += ":/msg/translations";

    return translationDirs;
}

QStringList LETOS_TRANSLATION_DIRS = getTranslationDirs();

void loadTranslation(const QString& baseName)
{
    if (LETOS_TRANSLATIONS.contains(baseName))
        return;

    QTranslator* translator = new QTranslator();

    QString fName;
    bool res = false;
    QString lang = LETOS->getCurrentLang();
    QStringList filters = QStringList({baseName+"_"+lang+".qm"});
    QDir dir;

    if (baseName == "qtbase")
    {
        res = translator->load(QLocale(lang), "qtbase", "_", QLibraryInfo::path(QLibraryInfo::TranslationsPath));
        fName = translator->filePath();
    }
    else
    {
        for (QString& dirPath : LETOS_TRANSLATION_DIRS)
        {
            dir.setPath(dirPath);
            for (QString& f : dir.entryList(filters))
            {
                res = translator->load(f, dirPath);
                if (res)
                {
                    fName = dirPath + "/" + f;
                    break;
                }
            }

            if (res)
                break;
        }
    }

    if (!res)
    {
        delete translator;
        return;
    }

    qApp->installTranslator(translator);
    LETOS_TRANSLATIONS[baseName] = translator;
    qDebug() << "Loaded:" << fName;
}

void unloadTranslation(const QString& baseName)
{
    if (!LETOS_TRANSLATIONS.contains(baseName))
        return;

    QTranslator* trans = LETOS_TRANSLATIONS[baseName];
    LETOS_TRANSLATIONS.remove(baseName);
    qApp->removeTranslator(trans);
    delete trans;
}

void loadTranslations(const QStringList& baseNames)
{
    for (const QString& name : baseNames)
        loadTranslation(name);
}

QStringList getAvailableTranslations()
{
    static QRegularExpression re("[^\\_]+\\_(\\w+)\\.qm");
    QSet<QString> locales;
    QRegularExpressionMatch match;
    QDir dir;
    QStringList filters = QStringList({"*_*.qm"});
    for (QString& dirPath : LETOS_TRANSLATION_DIRS)
    {
        dir.setPath(dirPath);
        for (QString& f : dir.entryList(filters))
        {
            match = re.match(f);
            if (!match.isValid())
                continue;

            locales << match.captured(1).toLower();
        }
    }
    locales << "en";

    // #4278 - the en_us translation as explicit qm file is unnecessary,
    // but produced by CrowdIn. The "en" is default and used for American English.
    locales.remove("en_us");

    return locales.values();
}

QMap<QString,QString> getAvailableLanguages()
{
    QMap<QString,QString> langs;
    QStringList translations = getAvailableTranslations();
    QLocale locale;
    QString langName;
    for (const QString& trans : translations)
    {
        locale = QLocale(trans);
        langName = locale.nativeLanguageName();
        if (langName.isEmpty())
            langName = trans;

        langs[langName] = trans;
    }

    return langs;
}

void setDefaultLanguage(const QString& lang)
{
    CFG_CORE.General.Language.set(lang);
}

QString getConfigLanguageDefault()
{
    return CFG_CORE.General.Language.getDefaultValue();
}
