//-----------------------------------------------------
// Name, first name: Hoet, Guntran
// Group: 1DAE3
//-----------------------------------------------------

//---------------------------
// Includes
//---------------------------
#include "EntityList.h"

//---------------------------
// Defines
//---------------------------
#define GAME_ENGINE (GameEngine::GetSingleton())
#define COLLISION_LIST (CollisionList::GetSingleton())
#define MARIO (CollisionList::GetSingleton())

EntityList* EntityList::m_EntityListPtr=nullptr;

//---------------------------
// Constructor & Destructor
//---------------------------
EntityList::EntityList()
	:m_BmpCoinPtr(nullptr)
	,m_BmpCoinBlockPtr(nullptr)
	,m_BmpFlowerPtr(nullptr)
	,m_BmpMushroomPtr(nullptr)
	,m_BmpStarPtr(nullptr)
{
	// bitmaps
	m_BmpCoinPtr = new Bitmap("Resources/Images/Coin.bmp");
	m_BmpCoinPtr->SetTransparencyColor(255,0,255);
	m_BmpCoinBlockPtr = new Bitmap("Resources/Images/CoinBlock.bmp");
	m_BmpCoinBlockPtr->SetTransparencyColor(255,0,255);
	m_BmpFlowerPtr = new Bitmap("Resources/Images/Flower.bmp");
	m_BmpFlowerPtr->SetTransparencyColor(255,0,255);
	m_BmpMushroomPtr = new Bitmap("Resources/Images/Mushroom.bmp");
	m_BmpMushroomPtr->SetTransparencyColor(255,0,255);
	m_BmpStarPtr = new Bitmap("Resources/Images/Star.bmp");
	m_BmpStarPtr->SetTransparencyColor(255,0,255);

	// arrays
	//for (int i = 0; i < NR_OF_COINS; ++i)
	//{
	//	m_EntityPtrArr[i] = nullptr;
	//}
	//for (int i = 0; i < NR_OF_MUSHROOMS; ++i)
	//{
	//	m_EntityPtrArr[i] = nullptr;
	//}
	//m_EntityPtrArr.reserve(100);
	for (EntityMaster* &itPtr: m_EntityPtrArr)
		itPtr = nullptr;
}

EntityList::~EntityList()
{
	delete m_BmpCoinPtr;
	m_BmpCoinPtr = nullptr;

	delete m_BmpCoinBlockPtr;
	m_BmpCoinBlockPtr = nullptr;

	delete m_BmpFlowerPtr;
	m_BmpFlowerPtr = nullptr;

	delete m_BmpMushroomPtr;
	m_BmpMushroomPtr = nullptr;

	delete m_BmpStarPtr;
	m_BmpStarPtr = nullptr;

	DeleteEntities();
}

EntityList* EntityList::GetSingleton()
{
	if (m_EntityListPtr == nullptr)
	{
		m_EntityListPtr = new EntityList();
	}
	return m_EntityListPtr;
}

//---------------------------
// Methods - Member functions
//---------------------------

void EntityList::Create()
{
	// general locals
	int i = 0;
	int lowRow = 136;
	int highRow = 72;
	int width = 16;
	// coin locals
	int upper = 298;
	int mid = 330;
	int lower = 362;
	int start = 64;
	int spacing = 16;

	//m_EntityPtrArr.reserve(NR_OF_COINS);
	//for (EntityMaster* &itPtr: m_EntityPtrArr)
	//{
	// COINS in secret room
	// upper coin row
	for (int j = 1; j < 6; ++j)
		m_EntityPtrArr.push_back(new Coin(start+(spacing*j), upper, m_BmpCoinPtr));

	// mid coin row
	for (int j = 0; j < 7; ++j)
		m_EntityPtrArr.push_back(new Coin(start+(spacing*j), mid, m_BmpCoinPtr));

	// lower coin row
	for (int j = 0; j < 7; ++j)
		m_EntityPtrArr.push_back(new Coin(start+(spacing*j), lower, m_BmpCoinPtr));
}

void EntityList::Tick(double deltaTime)
{
	for (EntityMaster* &itPtr: m_EntityPtrArr)
		if (itPtr != nullptr)
			itPtr->Tick(deltaTime);

	// check for deletables
	for (EntityMaster* &itPtr: m_EntityPtrArr)
	{
		if (itPtr != nullptr)
			if (itPtr->GetPos().y >= 500)
			{
				delete itPtr;
				itPtr = nullptr;
			}
	}
}

void EntityList::Paint(MATRIX3X2 matView)
{
	for (EntityMaster* &itPtr: m_EntityPtrArr)
		if (itPtr != nullptr)
			itPtr->Paint(matView);
}

void EntityList::DeleteEntities()
{
	for (EntityMaster* &itPtr: m_EntityPtrArr)
	{
		if (itPtr != nullptr)
		{
			delete itPtr;
			itPtr = nullptr;
		}
	}
	//for (int i = 0; i < NR_OF_ENTITIES; ++i)
	//{
	//	delete m_EntityPtrArr[i];
	//	m_EntityPtrArr[i] = nullptr;
	//}

	//for (int i = 0; i < NR_OF_MUSHROOMS; ++i)
	//{
	//	delete m_EntityPtrArr[i];
	//	m_EntityPtrArr[i] = nullptr;
	//}
}

void EntityList::DrawDebug(MATRIX3X2 matView)
{
	for (EntityMaster* &itPtr: m_EntityPtrArr)
		if (itPtr != nullptr)
			itPtr->DrawDebug(matView);
	//for (int i = 0; i < NR_OF_ENTITIES; ++i)
	//{
	//	m_EntityPtrArr[i]->DrawDebug(matView);
	//}
}

RECT2 EntityList::GetAABBRect(int index)
{
	RECT2 zero;
	if (m_EntityPtrArr[index] != nullptr)
		return m_EntityPtrArr[index]->GetAABBRect();
	else 
		return zero;
}

int EntityList::ReturnReward(int index)
{
	if (m_EntityPtrArr[index] != nullptr)
		return m_EntityPtrArr[index]->ReturnReward();
	else 
		return 0;
}

void EntityList::DeleteThisEntity(int index)
{
	m_EntityPtrArr[index]->Delete();
}

DOUBLE2 EntityList::GetPosition(int index)
{

	if (m_EntityPtrArr[index] != nullptr)
		return m_EntityPtrArr[index]->GetPos();
	else 
		return DOUBLE2 (0,0);
}

void EntityList::AddNewEntity(DOUBLE2 pos, int type, int mushroomType)
{
	int posX = int(pos.x);
	int posY = int(pos.y)+14;

	switch (type)
	{
	case 1:
		m_EntityPtrArr.push_back(new CoinBlock(posX, posY, m_BmpCoinBlockPtr));
		break;

	case 2:
		m_EntityPtrArr.push_back(new Flower(posX, posY, m_BmpFlowerPtr));
		break;

	case 3:
		m_EntityPtrArr.push_back(new Mushroom(posX, posY, m_BmpMushroomPtr, mushroomType));
		break;

	case 4:
		m_EntityPtrArr.push_back(new Star(posX, posY, m_BmpStarPtr));
		break;

	default:
		// do nothing.
		break;
	}
}

int EntityList::GetSize()
{
	return m_EntityPtrArr.size();
}

void EntityList::Move(int index, DOUBLE2 dir)
{
	if (m_EntityPtrArr[index] != nullptr)
		m_EntityPtrArr[index]->Move(dir);
}

void EntityList::ZeroVelocity(int index, bool xVelocity, bool yVelocity)
{
	if (m_EntityPtrArr[index] != nullptr)
		m_EntityPtrArr[index]->ZeroVelocity(xVelocity, yVelocity);
}

int EntityList::GetType(int index)
{
	if (m_EntityPtrArr[index] != nullptr)
		return m_EntityPtrArr[index]->GetType();
	else 
		return -1;
}

void EntityList::SetStage(int index, int stage)
{
	if (m_EntityPtrArr[index] != nullptr)
		return m_EntityPtrArr[index]->SetStage(stage);
}

int EntityList::GetMushroomType(int index)
{
	if (m_EntityPtrArr[index] != nullptr)
	{
		if (m_EntityPtrArr[index]->GetType() == 3)
			return m_EntityPtrArr[index]->GetMushroomType();
	}

	return -1;
}


