#ifndef ICONS_H
#define ICONS_H

#include "../misc/fixc11.h"

#include <QIcon>
#include <QImage>
#include <QPainter>
#include <QRgb>

#include <QtSvg/QSvgRenderer>

#include <unordered_map>

class Icons {

public:


	static const QPixmap& getPixmap(const std::string& name, const int size = 32) {

		// caching
		static std::unordered_map<std::string, QPixmap> cache;

		// try to get the image from the cache
		const std::string cacheKey = std::to_string(size) + name;
		auto it = cache.find(cacheKey);

		// not in cache?
		if (it == cache.end()) {

			// build
			const QColor fill = Qt::transparent;
			const std::string file = "://res/icons/" + name + ".svg";
			QSvgRenderer renderer(QString(file.c_str()));
			QPixmap pm(size, size);
			pm.fill(fill);
			QPainter painter(&pm);
			renderer.render(&painter, pm.rect());

			// add to cache
			cache[cacheKey] = pm;

		}

		// done
		return cache[cacheKey];

	}

	static const QPixmap& getPixmapColored(const std::string& name, const QColor color, const int size = 32) {

		// caching
		static std::unordered_map<std::string, QPixmap> cache;

		// try to get the image from the cache
		const QString hex = color.name();
		const std::string cacheKey = hex.toStdString() + "_" + std::to_string(size) + "_" + name;
		auto it = cache.find(cacheKey);

		// not in cache?
		if (it == cache.end()) {

			// copy
			QPixmap colored = getPixmap(name, size);
			QPainter painter(&colored);
			painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
			painter.fillRect(colored.rect(), color);
			painter.end();

			// add to cache
			cache[cacheKey] = colored;

		}

		// done
		return cache[cacheKey];

	}

	static QIcon getIcon(const std::string& name, const int size = 32) {

		return QIcon(getPixmap(name, size));

	}

};


#endif // ICONS_H
