/*
    QGL TextureManager
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GLTEXTUREMANAGER_H
#define GLTEXTUREMANAGER_H

#include "glmaterial.h"
#include "qglengine_core_export.h"

#include <QDir>
#include <QFileInfo>
#include <QImage>
#include <QMap>
#include <QOpenGLExtraFunctions>


class QGLENGINE_CORE_EXPORT TextureManager: public QObject {
	Q_OBJECT

public:
	TextureManager(QOpenGLExtraFunctions * f_): f(f_) {}
	virtual ~TextureManager() {}

	// GLuint loadTexture(const QString & path, bool is_normal = false, MapBakeOptions opts = {});
	//  GLuint loadTexture(const QImage & image, bool ownership = true, bool is_normal = false, MapBakeOptions opts = {});
	QImage loadTextureImage(const QString & path, bool is_normal = false, MapBakeOptions opts = {}, uint * result_hash = nullptr);
	// void reloadTexture(GLuint tid, const QString & path);
	// void reloadTexture(GLuint tid, const QImage & image);
	int textureID(const QString & path, bool is_normal = false, MapBakeOptions opts = {});
	QImage textureImage(const QString & path, bool is_normal = false, MapBakeOptions opts = {});
	QImage textureImage(uint hash) const { return cache_image.value(hash); }
	int textureLayer(uint hash) const { return array_layers.value(hash); }
	// void addTexture(const QString & path) { tex_pathes << path; }
	// bool loadTextures();
	// void deleteTextures();
	// void deleteTexture(const QString & name);
	int texturesCount() const { return cache_image.size(); }
	uint texturesHash() const { return qHash(cache_image.keys()); }
	void clearImageCache();
	void clearMissed();
	const QSet<QString> & missedFiles() const { return missed; }

	void loadToTexture2DArray(Texture2DArray * array, QSize map_size);

	static void addSearchPath(const QString & path);
	static void clearSearchPathes() { search_pathes.clear(); }
	static QStringList searchPathes() { return search_pathes; }
	static void setSearchPathes(const QStringList & pl) { search_pathes = pl; }
	static QString findFile(const QString & path);

protected:
	static void convertToNormal(QImage & im);
	static void applyBakeOption(QImage & im, MapBakeOptions opts);
	static uint mapHash(const QString & path, bool is_normal, MapBakeOptions opts);

	static QStringList search_pathes;

	QOpenGLExtraFunctions * f;
	QMap<uint, GLuint> cache_loaded;
	QMap<uint, QImage> cache_image;
	QMap<uint, int> array_layers;
	QSet<QString> missed;
	QStringList tex_pathes;

signals:
	void loadingDone();
	void filesUsed(int);
};


#endif // GLTEXTUREMANAGER_H
