/***
   This file is part of fly-ocr.

   Copyright (C) 2016-2020 Sandro Mani <manisandro@gmail.com>
   Copyright (C) 2021 LLC "RusBITech-Astra" (https://astralinux.ru)

    Based on gImageReader source code by Sandro Mani under the GNU
    General Public License (GPL) version 3 or later

   Foobar is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Foobar is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with fly-ocr. If not, see <https://www.gnu.org/licenses/>.
***/

#include "configdialog.h"
#include "configsettings.h"
#include "langtables.h"
#include "mainwindow.h"
#include "utils.h"

#include <QDesktopServices>
#include <QDir>
#include <QMultiMap>
#include <QStandardPaths>
#include <QUrl>
#include <enchant-provider.h>
#include <fly-ocr/ocrengine.h>


const QMultiMap<QString, QString> ConfigDialog::LANGUAGE_CULTURES = LangTables::languageCultures<QMultiMap<QString, QString>>();


ConfigDialog::ConfigDialog(QWidget* parent)
    : QDialog(parent)
{
    ui.setupUi(this);

    connect(ui.checkBoxDefaultOutputFont, &QCheckBox::toggled, ui.pushButtonOutputFont, &QPushButton::setDisabled);
    connect(ui.pushButtonOutputFont, &QPushButton::clicked, &m_fontDialog, &QFontDialog::exec);

    ADD_SETTING(SwitchSetting("openafterexport", ui.checkBoxOpenAfterExport, false));
    ADD_SETTING(SwitchSetting("systemoutputfont", ui.checkBoxDefaultOutputFont, true));
    ADD_SETTING(FontSetting("customoutputfont", &m_fontDialog, QFont().toString()));
    ADD_SETTING(ComboSetting("textencoding", ui.comboBoxEncoding, 0));
    ADD_SETTING(VarSetting<QString>("sourcedir", Utils::documentsFolder()));
    ADD_SETTING(VarSetting<QString>("outputdir", Utils::documentsFolder()));
    ADD_SETTING(VarSetting<QString>("auxdir", Utils::documentsFolder()));

    for (const OcrEngineFactory* factory : OcrEngine::factories()) {
        ui.comboBoxEngine->addItem(factory->name(), factory->shortName());
    }

    if (OcrEngine::selected()) {
        int index = ui.comboBoxEngine->findData(OcrEngine::selected()->shortName());
        ui.comboBoxEngine->setCurrentIndex(index);
    }
}

QStringList ConfigDialog::searchLangCultures(const QString& code) const
{
    return LANGUAGE_CULTURES.values(code);
}

bool ConfigDialog::useUtf8() const
{
    return ui.comboBoxEncoding->currentIndex() == 1;
}

void ConfigDialog::disableDictInstall()
{
    ConfigSettings::get<SwitchSetting>("dictinstall")->setValue(false);
}

void ConfigDialog::disableUpdateCheck()
{
    ConfigSettings::get<SwitchSetting>("updatecheck")->setValue(false);
}

void ConfigDialog::accept()
{
    OcrEngine::select(ui.comboBoxEngine->currentData().toString());
    QDialog::accept();
}

void ConfigDialog::openSpellingDir()
{
    int idx = QSettings().value("datadirs").toInt();
    QString spellingDir = spellingLocation(static_cast<Location>(idx));
    QDir().mkpath(spellingDir);
    QDesktopServices::openUrl(QUrl::fromLocalFile(spellingDir));
}

QString ConfigDialog::spellingLocation(Location location)
{
    if (location == SystemLocation) {
        char* prefix = enchant_get_prefix_dir();
        QDir dataDir(QDir(prefix).absoluteFilePath("share"));
        free(prefix);
#if HAVE_ENCHANT2
        if (QDir(dataDir.absoluteFilePath("myspell")).exists()) {
            return dataDir.absoluteFilePath("myspell");
        } else {
            return dataDir.absoluteFilePath("hunspell");
        }
#else
        return dataDir.absoluteFilePath("myspell/dicts");
#endif
    } else {
        QDir configDir = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation));
#if HAVE_ENCHANT2
        return configDir.absoluteFilePath("enchant/hunspell");
#else
        return configDir.absoluteFilePath("enchant/myspell");
#endif
    }
}
