#ifndef IMAGERINGLIST_H
#define IMAGERINGLIST_H

#include <QObject>
#include <QFileSystemWatcher>
#include <QList>
#include <QPixmap>
#include <QDir>
#include <memory>
#include "imageinfodata.h"
#include "rawimage.h"
#include <QAbstractItemModel>

class ImageRingList;
class QThreadPool;

class Image : public QObject
{
    Q_OBJECT
    bool m_loading;
    bool m_released;
    bool m_current;
    int m_number;
    std::shared_ptr<RawImage> m_rawImage;
    std::shared_ptr<RawImage> m_thumbnail;
    QString m_name;
    ImageInfoData m_info;
    ImageRingList *m_ringList;
public:
    explicit Image(const QString name, int number, ImageRingList *ringList);
    void load(int index, QThreadPool *pool);
    void loadThumbnail(QThreadPool *pool);
    void release();
    QString name() const;
    std::shared_ptr<RawImage> rawImage();
    const RawImage* thumbnail() const;
    ImageInfoData info() const;
    bool isCurrent() const;
    int number() const;
    void clearThumbnail();
    bool isLoading() const;
signals:
    void pixmapLoaded(Image *ptr);
    void thumbnailLoaded(Image *ptr);
protected slots:
    void imageLoaded(std::shared_ptr<RawImage> rawImage, ImageInfoData info);
    void thumbnailLoadFinish(std::shared_ptr<RawImage> rawImage);
};

typedef std::shared_ptr<Image> ImagePtr;

class Database;

class ImageRingList : public QAbstractListModel
{
    Q_OBJECT
    int m_width;
    QList<ImagePtr> m_images;
    QList<ImagePtr>::iterator m_firstImage;
    QList<ImagePtr>::iterator m_currImage;
    QList<ImagePtr>::iterator m_lastImage;
    QFileSystemWatcher m_fileSystemWatcher;
    bool m_liveMode;
    QDir::SortFlag m_sort = QDir::Name;
    bool m_reversed = false;
    AnalyzeLevel m_analyzeLevel;
    QThreadPool *m_loadPool;
    QThreadPool *m_thumbPool;
    Database *m_database;
    QStringList m_nameFilter;
    QStringList m_fileSuffix;
    QTimer *m_slideShowTimer;
    QTimer *m_dirChangeDelay;
    QString m_currentDir;
public:
    explicit ImageRingList(Database *database, const QStringList &nameFilter, QObject *parent = 0);
    ~ImageRingList() override;
    bool setDir(const QString path, const QString &currentFile = QString(), bool recursive = false);
    void setFile(const QString &file);
    void setFiles(QStringList files);
    ImagePtr currentImage();
    QString currentDir() const;
    void setLiveMode(bool live);
    void setCalculateStats(bool stats);
    void setFindPeaks(bool findPeaks);
    void setFindStars(bool findStars);
    AnalyzeLevel analyzeLevel() const;
    void loadFile(int row);
    void loadThumbnails();
    void stopLoading();
    int imageCount() const;
    QStringList imageNames() const;
    void updateMark();
    void clearThumbnails();

    //QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    //QModelIndex parent(const QModelIndex &child) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    //int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
public slots:
    void setPreload(int width);
    void setSort(QDir::SortFlag sort);
    void reverseSort();
    void toggleSlideshow(bool start);
    void increment();
    void decrement();
    void prevSubImage();
    void nextSubImage();
    void setMarked();
    void reloadImage();
protected:
    void setFilesPrivate(const QStringList files, const QString &currentFile = QString());
    QList<ImagePtr>::iterator increment(QList<ImagePtr>::iterator iter);
    QList<ImagePtr>::iterator decrement(QList<ImagePtr>::iterator iter);
signals:
    void pixmapLoaded(Image *image);
    void thumbnailLoaded(Image *image);
    void infoLoaded(ImageInfoData info);
    void currentImageChanged(int index);
protected slots:
    void imageLoaded(Image *image);
    void dirChanged(QString);
    void reloadDir();
};

#endif // IMAGERINGLIST_H
