/***
   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 "configsettings.h"
#include "hocrdocument.h"
#include "hocrtextexporter.h"
#include "mainwindow.h"

#include <QDesktopServices>
#include <QFileInfo>
#include <QMessageBox>
#include <QTextStream>
#include <QUrl>

bool HOCRTextExporter::run(const HOCRDocument* hocrdocument, const QString& outname, const ExporterSettings* /*settings*/)
{
    QFile outputFile(outname);
    if (!outputFile.open(QIODevice::WriteOnly)) {
        QMessageBox::warning(MAIN, tr("Export failed"), tr("The text export failed: unable to write output file."));
        return false;
    }

    QString output;
    QTextStream outputStream(&output, QIODevice::WriteOnly);
    for (int i = 0, n = hocrdocument->pageCount(); i < n; ++i) {
        const HOCRPage* page = hocrdocument->page(i);
        if (!page->isEnabled()) {
            continue;
        }
        printItem(outputStream, page);
    }
    outputFile.write(MAIN->config()->useUtf8() ? output.toUtf8() : output.toLocal8Bit());
    outputFile.close();
    bool openAfterExport = ConfigSettings::get<SwitchSetting>("openafterexport")->getValue();
    if (openAfterExport) {
        QDesktopServices::openUrl(QUrl::fromLocalFile(outname));
    }
    return true;
}

void HOCRTextExporter::printItem(QTextStream& outputStream, const HOCRItem* item, bool lastChild)
{
    if (!item->isEnabled()) {
        return;
    }
    QString itemClass = item->itemClass();
    if (itemClass == "ocrx_word") {
        outputStream << item->text();
        if (!lastChild) {
            outputStream << " ";
        }
    } else {
        for (int i = 0, n = item->children().size(); i < n; ++i) {
            printItem(outputStream, item->children()[i], i == n - 1);
        }
    }
    if (itemClass == "ocr_line" || itemClass == "ocr_par") {
        outputStream << "\n";
    }
}
