/***
   This file is part of the Tesseract plugin for fly-ocr.

   Copyright (C) 2021 LLC "RusBITech-Astra" (https://astralinux.ru)

   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 the Tesseract plugin for fly-ocr. If not, see
   <https://www.gnu.org/licenses/>.
***/

#define USE_STD_NAMESPACE
#include <tesseract/baseapi.h>
#undef USE_STD_NAMESPAC
#include <tesseract/publictypes.h>
#include "tesseractengine.h"
#include "tesseractfactory.h"

QString TesseractFactory::name() const
{
    return QLatin1String("Tesseract");
}

QString TesseractFactory::shortName() const
{
    return QLatin1String("tesseract");
}

QString TesseractFactory::version() const
{
    QByteArray current = setlocale(LC_ALL, nullptr);
    setlocale(LC_ALL, "C");
    tesseract::TessBaseAPI api;
    setlocale(LC_ALL, current.constData()); //restore locale
    return QString::fromLatin1(api.Version());
}

QList<FlyOcr::Lang> TesseractFactory::languages() const
{
    TesseractEngine engine;
    engine.initialize();
    return engine.availableLanguages();
}

QList<PageSegmentationMode> TesseractFactory::pageSegmentationModes() const
{
    QList<PageSegmentationMode> psmModes = {
        { tesseract::PSM_AUTO, tr("Automatic page segmentation") },
        { tesseract::PSM_SINGLE_COLUMN, tr("Assume single column of text") },
        { tesseract::PSM_SINGLE_BLOCK_VERT_TEXT, tr("Assume single block of vertically aligned text") },
        { tesseract::PSM_SINGLE_BLOCK, tr("Assume a single uniform block of text") },
        { tesseract::PSM_SINGLE_LINE, tr("Assume a line of text") },
        { tesseract::PSM_SINGLE_WORD, tr("Assume a single word") },
        { tesseract::PSM_CIRCLE_WORD, tr("Assume a single word in a circle") },
        { tesseract::PSM_SPARSE_TEXT, tr("Sparse text in no particular order") },

    };

    TesseractEngine engine;
    engine.initialize();

    if (engine.hasOsd()) {
        psmModes << PageSegmentationMode { tesseract::PSM_SPARSE_TEXT_OSD, tr("Sparse text with orientation and script detection") };
        psmModes << PageSegmentationMode { tesseract::PSM_AUTO_OSD, tr("Page segmentation with orientation and script detection") };
    }

    return psmModes;
}

QStringList TesseractFactory::defaultLanguages() const
{
    static const QStringList langs = { QStringLiteral("eng"), QStringLiteral("rus") };
    return langs;
}

int TesseractFactory::defaultPageSegmentationModeId() const
{
    return tesseract::PSM_SINGLE_BLOCK;
}

OcrEngine* TesseractFactory::create(QObject* parent)
{
    return new TesseractEngine(parent);
}

OcrEngineFactory::Formats TesseractFactory::supportedPageDataFormats() const
{
    return OcrEngineFactory::Formats(OcrEngineFactory::TXT | OcrEngineFactory::HOCR);
}

OcrEngineFactory::Formats TesseractFactory::supportedExportFormats() const
{
    return OcrEngineFactory::Formats(OcrEngineFactory::TXT);
}

QList<QAction*> TesseractFactory::createActions(QWidget* parent)
{
    Q_UNUSED(parent);
    return QList<QAction*>();
}

QString TesseractFactory::about() const
{
    return QStringLiteral("<b>") + tr("Tesseract OCR") + QStringLiteral("</b><br>") +
           tr("Version: %1").arg(version()) + QStringLiteral("<br>") +
           tr("License: <a href=\"https://github.com/tesseract-ocr/tesseract#license\">Apache, Version 2.0</a>");
}
