/***
   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/>.
***/

#ifndef SOURCEMANAGER_H
#define SOURCEMANAGER_H

#include <QMap>
#include <QMetaType>
#include <QStringList>
#include <QItemSelection>
#include "sourcedocumentmodel.h"
#include "common.h"

class QMenu;
class QPixmap;
class MainWindowUi;

class SourceManager : public QObject
{
    Q_OBJECT
public:
    SourceManager(const MainWindowUi* _ui);
    ~SourceManager();
    std::shared_ptr<SourceDocumentPage> selectedPage() const;
    std::shared_ptr<SourceDocumentPage> page(int p) const;

    void add(const QImage& image);
    bool add(const QStringList& paths, bool suppressWarnings = false);
    bool add(const QList<QUrl>& urls, bool suppressWarnings = false);
    bool add(const QString& paths, bool suppressWarnings = false);
    QList<int> findPages(const QString& filePath) const;
    QList<int> selectedPages() const;
    void selectPage(int p);
    void setRecognized(int p, bool recognized);
    void clearReconized();
    void rotateAll(double delta);

signals:
    void selectedPageChanged();

private slots:
    void currentSourceChanged();
    void addFolder();
    void openRecentItem();
    void openSources();
    void pasteClipboard();
    void removeSelected();
    void takeScreenshot();
    void updateActions();

private:
    enum class PdfWithTextAction {Ask, Add, Skip};
    const MainWindowUi* m_ui;

    SourceDocumentModel* m_sourceDocumentModel;
    bool checkPdfSource(std::shared_ptr<SourceDocumentInfo> info, const QString& path, PdfWithTextAction* textAction) const;
    void savePixmap(const QPixmap& pixmap);
    bool eventFilter(QObject* object, QEvent* event) override;
    QStringList findImageFiles(const QString& path) const;
};

#endif // SOURCEMANAGER_H
