/***
   This file is part of 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 fly-ocr. If not, see <https://www.gnu.org/licenses/>.
***/

#ifndef SOURCEDOCUMENTMODEL_H
#define SOURCEDOCUMENTMODEL_H

#include <QAbstractListModel>
#include <QObject>
#include <QList>
#include <QImage>
#include <memory>
#include "displayrenderer.h"

struct SourceDocumentInfo {
    QByteArray password;
    //Additional info from original file
    QString author, title, creator, producer, keywords, subject;
    int pdfVersionMajor = -1, pdfVersionMinor = -1;
};

class SourceDocumentPage
{
public:
    SourceDocumentPage() = default;
    ~SourceDocumentPage();

    std::shared_ptr<DisplayRenderer> renderer;
    std::shared_ptr<SourceDocumentInfo> info;
    int page = -1;
    QString path;
    double angle = 0;
    int brightness = 0;
    int contrast = 0;
    int resolution = 150;
    bool invert = false;
    bool temp = false;
    bool recognized = false;
    QString displayName;
};

class SourceDocumentModel : public QAbstractListModel
{
    Q_OBJECT
public:
    explicit SourceDocumentModel(QObject* parent = nullptr);

    void add(const QStringList& paths);
    void add(const QString& path, bool temp = false, std::shared_ptr<SourceDocumentInfo> documentInfo = std::shared_ptr<SourceDocumentInfo>());
    void setRecognized(int p, bool recognized);
    void clearReconized();
    std::shared_ptr<SourceDocumentPage> page(int i);
    QModelIndex findFile(const QString& filePath) const;
    QList<int> findPages(const QString& filePath) const;
    bool isEmpty() const;
    bool hasRecognitionResults() const;
    bool listMode() const;

    QVariant data(const QModelIndex& index, int role) const override;
    int rowCount(const QModelIndex& parent = QModelIndex()) const override;
    bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;

public slots:
    void setListMode(bool enabled);
    void clear();

private:
    QList<std::shared_ptr<SourceDocumentPage>> m_pages;
    bool m_listMode = false;
};

#endif // SOURCEDOCUMENTMODEL_H
