#ifndef NWWMAPIMAGE_H
#define NWWMAPIMAGE_H

#include "ReadOnlyMapImage.h"
#include "mapreproject.h"

#include <QCache>
#include <QColor>
#include <QDir>
#include <QImage>
#include <QPair>
#include <QSet>

class InterpolationMethod;

class NwwMapImage : public ReadOnlyMapImage
{
public:
    NwwMapImage(QDir const &baseDirectory, int const tileLevel);

    QRgb pixel(double const lonRad, double const latRad) override;
    QRgb pixel(int const x, int const y) override;

    void setBaseDirectory(QDir const &baseDirectory);
    void setCacheSizeBytes(int const cacheSizeBytes);
    void setInterpolationMethod(InterpolationMethod *const method) override;
    void setTileLevel(int const level);

private:
    enum {
        DefaultCacheSizeBytes = 32 * 1024 * 1024
    };

    static int tileId(int const tileX, int const tileY);
    QPair<QImage, bool> tile(int const tileX, int const tileY);
    double lonRadToPixelX(double const lonRad) const;
    double latRadToPixelY(double const latRad) const;

    // Interpolation methods
    QRgb nearestNeighbor(double const x, double const y);
    QRgb bilinearInterpolation(double const x, double const y);

    int const m_tileEdgeLengthPixel;
    QRgb const m_emptyPixel;

    QDir m_baseDirectory;
    int m_tileLevel;
    int m_mapWidthTiles;
    int m_mapHeightTiles;
    int m_mapWidthPixel;
    int m_mapHeightPixel;

    InterpolationMethod *m_interpolationMethod;

    QSet<int> m_tileMissing;
    QCache<int, QImage> m_tileCache;
};

#endif
