#include "resource.h"
#include "config.h"
#include "sprite.h"
#include <stdio.h>
#include <qimage.h>

#include <ksimpleconfig.h>
#include <kdebug.h>

static QString s_theme = QString::null;
static int s_tilesize = 0;
static int s_optimization = 1;
QMap<QString, QCanvasPixmapArray*> Resource::m_pixmaparrayhash;
QMap<QString, QPixmap*> Resource::m_pixmaphash;

void Resource::setTheme(QString theme)
{
	if(theme != s_theme)
	{
		clean();
		s_theme = theme;
	}
}

void Resource::setTilesize(int tilesize)
{
	if(tilesize != s_tilesize)
	{
		clean();
		s_tilesize = tilesize;
		Sprite::setTilesize(tilesize);
	}
}

QString Resource::path(QString item)
{
	if(item == "item_bomb") return Resource::load("item_bomb%1.png", Resource::pic);
	if(item == "item_live") return Resource::load("item_live%1.png", Resource::pic);
	if(item == "item_power") return Resource::load("item_power%1.png", Resource::pic);
	if(item == "item_scree") return Resource::load("item_scree%1.png", Resource::pic);
	if(item == "item_grail") return Resource::load("item_grail%1.png", Resource::pic);
	if(item == "item_speed") return Resource::load("item_speed%1.png", Resource::pic);
	if(item == "item_invisible") return Resource::load("item_invisible%1.png", Resource::pic);
	if(item == "item_flash") return Resource::load("item_flash%1.png", Resource::pic);

	if(item == "ground") return Resource::load("ground.png", Resource::pic);
	if(item == "wall") return Resource::load("wall.png", Resource::pic);
	if(item == "bridge") return Resource::load("bridge.png", Resource::pic);
	if(item == "exit") return Resource::load("item_exit%1.png", Resource::pic);

	if(item == "bomb") return Resource::load("bomb%1.png", Resource::pic);
	if(item == "timebomb") return Resource::load("timebomb%1.png", Resource::pic);
	if(item == "moveablebomb") return Resource::load("moveablebomb%1.png", Resource::pic);
	if(item == "blast") return Resource::load("blast%1.png", Resource::pic);

	if(item == "menu_power") return Resource::load("item_power0001.png", Resource::pic);
	if(item == "menu_lives") return Resource::load("item_live0001.png", Resource::pic);
	if(item == "menu_bombs") return Resource::load("item_bomb0001.png", Resource::pic);

	/*if(item == "level") return Resource::load("level.txt", Resource::level);
	if(item == "level1") return Resource::load("level1.txt", Resource::level);
	if(item == "level2") return Resource::load("level2.txt", Resource::level);
	if(item == "level3") return Resource::load("level3.txt", Resource::level);
	if(item == "level4") return Resource::load("level4.txt", Resource::level);
	if(item == "level5") return Resource::load("level5.txt", Resource::level);
	if(item == "level6") return Resource::load("level6.txt", Resource::level);
	if(item == "level7") return Resource::load("level7.txt", Resource::level);
	if(item == "level8") return Resource::load("level8.txt", Resource::level);
	if(item == "level9") return Resource::load("level9.txt", Resource::level);
	if(item == "level10") return Resource::load("level10.txt", Resource::level);*/

	if(item == "background") return Resource::load("bg.png", Resource::pic);

	if(item == "guy1") return Resource::load("guy%1.png", Resource::pic);
	if(item == "guy2") return Resource::load("guy2%1.png", Resource::pic);
	if(item == "guy3") return Resource::load("guy3%1.png", Resource::pic);
	if(item == "guy4") return Resource::load("guy4%1.png", Resource::pic);
	if(item == "monster1") return Resource::load("monster%1.png", Resource::pic);
	if(item == "monster2") return Resource::load("monster2%1.png", Resource::pic);

	fprintf(stderr, "Resource: '%s' unknown\n", item.latin1());

	return QString::null;
}

QString Resource::load(QString item, int type)
{
	switch(type)
	{
		case pic:
			//return QString("../pics/theme-") + s_theme + "/" + item;
//fprintf(stderr, "Return: * /pics/theme-%s/%s\n", s_theme.latin1(), item.latin1());
			return QString(DATADIR "/pics/theme-") + s_theme + "/" + item;
			break;
		case level:
			//return QString("../../common/levels/") + item;
			return QString(DATADIR "/levels/") + item + ".txt";
			break;
	}
	return QString::null;
}

QPixmap Resource::pixmap(QString item, int count)
{
	Q_UNUSED(count);

	QString key = QString("%1:%2:%3").arg(item).arg(s_theme).arg(s_tilesize);

	if(m_pixmaphash[key]) return *(m_pixmaphash[key]);

	QPixmap pic(path(item));
	QImage im = pic.convertToImage();
	QImage im2 = im.smoothScale(tilesize(), tilesize());
	pic.convertFromImage(im2);

	m_pixmaphash[key] = new QPixmap(pic);

	return pic;
}

QCanvasPixmapArray *Resource::pixmaparray(QString item, int count)
{
	QString key = QString("%1:%2:%3").arg(item).arg(s_theme).arg(s_tilesize);

	if(m_pixmaparrayhash[key]) return m_pixmaparrayhash[key];

	KSimpleConfig conf(DATADIR "/pics/themes.registry");
	conf.setGroup(s_theme);
	int count2 = conf.readNumEntry(item + ".frames", 0);
	if(count2 > 0)
	{
		//kdDebug() << "Resource: overriding count for " << item << " from " << count << " to " << count2 << endl;
		count = count2;
	}
	
	QCanvasPixmapArray *picar = new QCanvasPixmapArray(path(item), count);
	for(unsigned int i = 0; i < picar->count(); i++)
	{
		QCanvasPixmap *pic = picar->image(i);
		if(pic)
		{
			QImage im = pic->convertToImage();
			QImage im2 = im.smoothScale(tilesize(), tilesize());
			//QPixmap p;
			pic->convertFromImage(im2);
			//QCanvasPixmap *pic2 = new QCanvasPixmap(p, QPoint(0, 0));
			//picar->setImage(i, pic);
		}
	}

	m_pixmaparrayhash[key] = picar;

	return picar;
}

void Resource::clean()
{
	if(!s_optimization)
	{
		m_pixmaparrayhash.clear();
		m_pixmaphash.clear();
	}
}

int Resource::tilesize()
{
	return s_tilesize;
}

QString Resource::theme()
{
	return s_theme;
}

void Resource::optimizeForSpeed(int optimize)
{
	s_optimization = optimize;
}

