#include "HUD.h"
#include "WorldManager.h"
#include "Manager.h"
#include "Event.h"
#include <curses.h>

HUD::HUD(int coins)
{
	WorldManager::getInstance().addEvent("coin");
	WorldManager::getInstance().registerInterest(this,"coin");
	//this->coinsLeft=0;
	this->coinsLeft=coins;
	//this->coinsLeft=this->getCoinCount();
}


HUD::~HUD(void)
{
}

int HUD::eventHandler(Event *e){
	if (e->getType() == "step") {
		//step();
		if(this->coinsLeft==0){
			GameObjectList GOL=WorldManager::getInstance().getAllObjects();
			for(int i=0;i<10000;i++){
				if(GOL.getAt(i)!=NULL){
					if(GOL.getAt(i)->getType()=="blocker"){
						WorldManager::getInstance().markForDelete(GOL.getAt(i));
					}
				}
			}
			this->coinsLeft=-1;
		}


		draw();
		return 1;
	}
	//hero sends a coin event when he collides with a coin
	if(e->getType()=="coin"){
		this->coinsLeft--;
	}
	return 0;
}

void HUD::draw(){
	char buffer[64];
	if(this->coinsLeft>=0){
		int n=sprintf(buffer,"Coins remaining: %d",this->coinsLeft);
	}
	else{
		int n=sprintf(buffer,"Find the exit!");
	}
	//init_pair(254,15,0);
	attron(COLOR_PAIR(0x0F));
	mvaddstr(0,0,buffer);
	attroff(COLOR_PAIR(0x0F));
}