#ifndef IMAGERINGLIST_H
#define IMAGERINGLIST_H

#include <QObject>
#include <QFileSystemWatcher>
#include <QList>
#include <QPixmap>
#include <memory>
#include "imageinfo.h"
#include "rawimage.h"

class ImageRingList;

class Image : public QObject
{
    Q_OBJECT
    bool m_loading;
    bool m_released;
    bool m_current;
    std::unique_ptr<RawImage> m_rawImage;
    QString m_name;
    ImageInfoData m_info;
    ImageRingList *m_ringList;
public:
    explicit Image(const QString name, ImageRingList *ringList);
    void load();
    void release();
    QString name() const;
    RawImage* rawImage();
    ImageInfoData info() const;
    bool isCurrent() const;
signals:
    void pixmapLoaded(Image *ptr);
protected slots:
    void imageLoaded(void *rawImage, ImageInfoData info);
};

typedef std::shared_ptr<Image> ImagePtr;

class ImageRingList : public QAbstractItemModel
{
    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;
    AnalyzeLevel m_analyzeLevel;
public:
    explicit ImageRingList(QObject *parent = 0);
    ~ImageRingList();
    bool setDir(const QString path, const QString &currentFile = QString());
    void setFile(const QString &file);
    ImagePtr currentImage();
    void increment();
    void decrement();
    void setLiveMode(bool live);
    void setCalculateStats(bool stats);
    void setFindPeaks(bool findPeaks);
    void setFindStars(bool findStars);
    AnalyzeLevel analyzeLevel() const;
    void loadFile(int row);

    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;
protected:
    void setFiles(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 infoLoaded(ImageInfoData info);
    void currentImageChanged(int index);
protected slots:
    void imageLoaded(Image *image);
    void dirChanged(QString dir);
};

#endif // IMAGERINGLIST_H
