#include <fstream>
#include "ResourceManager.h"
using namespace std;

class LevelLoader
{
public:

	Position spos;
	string nextName;
	short bgCol;
	int coinCount;

	LevelLoader(void)
	{
		ResourceManager::getInstance().loadSprite("brick_sprite.txt","brick");
		ResourceManager::getInstance().loadSprite("coin.txt","coin");
		ResourceManager::getInstance().loadSprite("moving_platform.txt","moving_platform");
		ResourceManager::getInstance().loadSprite("spring.txt","spring");
		ResourceManager::getInstance().loadSprite("spring_anim.txt","spring_anim");
		ResourceManager::getInstance().loadSprite("koolaid.txt","koolaid");
		ResourceManager::getInstance().loadSprite("koolaidtop.txt","koolaidtop");
		ResourceManager::getInstance().loadSprite("test_enemy_sprite.txt","spike_ball");
		ResourceManager::getInstance().loadSprite("spike_floor.txt","spike_floor");
		ResourceManager::getInstance().loadSprite("spike_ceiling.txt","spike_ceiling");
		ResourceManager::getInstance().loadSprite("spike_right.txt","spike_right");
		ResourceManager::getInstance().loadSprite("spike_left.txt","spike_left");
		ResourceManager::getInstance().loadSprite("exit_blocker.txt","exit_blocker");
		ResourceManager::getInstance().loadSprite("exit.txt","exit");
		ResourceManager::getInstance().loadSprite("checkpoint.txt","check");

		ResourceManager::getInstance().loadSprite("hero_walking_sprite.txt","hero_walk");
		ResourceManager::getInstance().loadSprite("hero_standing_sprite.txt","hero_stand");
		ResourceManager::getInstance().loadSprite("hero_jump_right_sprite.txt","hero_jump_right");
		ResourceManager::getInstance().loadSprite("hero_jump_left_sprite.txt","hero_jump_left");
		coinCount=0;
	}

	int loadLevel(string fileName){
		ifstream level;
		level.open(fileName);
		string line;
		int temp=0;
		if (level.is_open())
		{
			while (level.good())
			{
				getline(level,line);
				//hacky way to get the background level color
				temp=parseLine(line);
				if(temp!=-1){
					bgCol=temp;
				}
			}
			level.close();
		}
		HUD* hud=new HUD(coinCount);
		hud->setSpriteIndex(0);
		hud->setType("hud");
		hud->setPos(Position(0,0));
		hud->setSpriteSlowdown(-1);
		hud->setSolid(false);
		return 0;
	}

	int parseLine(string line){
		char* pch;
		char* str=(char*)line.c_str();
		pch = strtok (str," ,");
		int itemNum=0;
		int items[256]={0};
		static int bgColor=0;
		string obj="";
		while (pch != NULL)
		{
			string st=pch;
			if(itemNum==0){
				obj=pch;
			}
			else{
				items[itemNum-1]=atoi(pch);
			}

			if(obj=="next"){
				pch = strtok (NULL, " ,");
				nextName=pch;
			}
			pch = strtok (NULL, " ,");
			itemNum++;
		}
		if(obj=="size"){
			WorldManager::getInstance().setBoundary(Box(Position(0,0),items[0],items[1]));
		}
		if(obj=="bg"){
			bgColor=items[0];
			return items[0];
		}
		if(obj=="brick"){
			boxOfSolid(Position(items[0],items[1]),items[2],items[3],items[4],items[5],items[6]);
		}
		if(obj=="koolaid"){
			boxOfKoolaid(Position(items[0],items[1]),items[2],items[3]);
		}
		if(obj=="coin"){
			Coin* coin=new Coin();
			coin->setSpriteIndex(0);
			coin->setType("coin");
			coin->setColor(bgColor,14);
			coin->setPos(Position(items[0],items[1]));
			coin->setSpriteSlowdown(5);
			coin->setSolid(true);
			coin->setSprite(ResourceManager::getInstance().getSprite("coin"),true);
			coinCount++;
		}
		if(obj=="moving"){
			MovingPlatform* plat=new MovingPlatform();
			plat->setType("plat");
			plat->moveDelaySet=items[4];
			plat->setColor(7,0);
			plat->setSpriteIndex(items[5]);
			plat->setSpriteSlowdown(-1);
			plat->setPos(Position(items[0],items[1]));
			plat->xs=items[2];
			plat->ys=items[3];
			plat->setSolid(true);
			plat->setSprite(ResourceManager::getInstance().getSprite("moving_platform"),true);
		}
		if(obj=="spring"){
			Spring* spring=new Spring();
			spring->setSpriteIndex(0);
			spring->setType("spring");
			spring->setColor(bgColor,0);
			spring->setPos(Position(items[0],items[1]));
			spring->setSpriteSlowdown(1);
			spring->setSolid(true);
			spring->setSprite(ResourceManager::getInstance().getSprite("spring"),true);
		}
		if(obj=="spikeball"){
			SpikeBall* spike=new SpikeBall();
			spike->setSpriteIndex(0);
			spike->setType("spike");
			spike->setColor(bgColor,12);
			spike->setPos(Position(items[0],items[1]));
			spike->setSpriteSlowdown(3);
			spike->setSolid(true);
			spike->setSprite(ResourceManager::getInstance().getSprite("spike_ball"),true);
		}
		if(obj=="blocker"){
			ExitBlocker* blocker=new ExitBlocker();
			blocker->setType("blocker");
			blocker->setColor(0,14);
			blocker->setSpriteIndex(0);
			blocker->setSpriteSlowdown(2);
			blocker->setPos(Position(items[0],items[1]));
			blocker->setSolid(true);
			blocker->setSprite(ResourceManager::getInstance().getSprite("exit_blocker"),true);
		}
		if(obj=="checkpoint"){
			Checkpoint* check=new Checkpoint();
			check->setType("check");
			check->setColor(2,15);
			check->setSpriteIndex(0);
			check->setSpriteSlowdown(4);
			check->setPos(Position(items[0],items[1]));
			check->setSolid(true);
			check->setSprite(ResourceManager::getInstance().getSprite("check"),true);
		}
		if(obj=="exit"){
			Exit *exit=new Exit();
			exit->setType("exit");
			exit->setColor(5,10);
			exit->setSpriteIndex(0);
			exit->setSpriteSlowdown(8);
			exit->setPos(Position(items[0],items[1]));
			exit->setSolid(true);
			exit->setSprite(ResourceManager::getInstance().getSprite("exit"),true);
		}
		if(obj=="start"){
			Hero* hero=new Hero();
			hero->setType("hero");
			hero->setColor(bgCol,0);
			hero->setSpriteIndex(0);
			hero->setSpriteSlowdown(2);
			hero->setPos(Position(items[0],items[1]));
			hero->startPos=Position(items[0],items[1]);
			hero->setSolid(true);
			hero->nextLevelName=nextName;
			hero->setSprite(ResourceManager::getInstance().getSprite("hero_stand"),true);
		}
		return -1;
	}

	void boxOfSolid(Position pos, int width, int height, int bg, int fg, int index){
		for(int i=0;i<width;i++){
			for(int j=0;j<height;j++){
				Brick* brick=new Brick();
				brick->setSpriteIndex(index);
				brick->setType("brick");
				brick->setColor(bg,fg);
				brick->setPos(Position(pos.getX()+4*i,pos.getY()+4*j));
				brick->setSpriteSlowdown(-1);
				brick->setSolid(true);
				brick->setSprite(ResourceManager::getInstance().getSprite("brick"),true);
			}
		}
	}

	void boxOfKoolaid(Position pos, int width, int height){
		for(int i=0;i<width;i++){
			for(int j=0;j<height;j++){
				SpikeBall* spike=new SpikeBall();
				spike->setSpriteIndex(0);
				spike->setType("spike");
				spike->setColor(12,4);
				spike->setPos(Position(pos.getX()+4*i,pos.getY()+4*j));
				spike->setSpriteSlowdown(3);
				spike->setSolid(true);
				spike->setSprite(ResourceManager::getInstance().getSprite("koolaid"),true);
			}
		}
		for(int i=0;i<width;i++){
			SpikeBall* spike=new SpikeBall();
			spike->setSpriteIndex(0);
			spike->setType("spike");
			spike->setColor(12,4);
			spike->setPos(Position(pos.getX()+4*i,pos.getY()-2));
			spike->setSpriteSlowdown(4);
			spike->setSolid(false);
			spike->setSprite(ResourceManager::getInstance().getSprite("koolaidtop"),true);
		}
	}

	~LevelLoader(void)
	{
	}
};

