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

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

#include <iostream>
#include <sstream>
#include <fstream>
//---------------------------
// Defines
//---------------------------
#define GAME_ENGINE (GameEngine::GetSingleton())
#define MARIO		(Mario::GetSingleton())

EnemyList* EnemyList::m_EnemyListPtr=nullptr;
//---------------------------
// Constructor & Destructor
//---------------------------
EnemyList::EnemyList()
	//bitmap
	:m_BmpGoombaPtr(nullptr)
	,m_BmpTroopaPtr(nullptr)
{
	// enemy array
	for (int i = 0; i < NR_OF_ENEMIES; ++i)
		m_EnemyPtrArr[i] = nullptr;

	// bimtap transparency
	m_BmpGoombaPtr = new Bitmap("Resources/Images/Goomba.bmp");
	m_BmpGoombaPtr->SetTransparencyColor(255,0,255);
	m_BmpTroopaPtr = new Bitmap("Resources/Images/Troopa.bmp");
	m_BmpTroopaPtr->SetTransparencyColor(255,0,255);

	// assign enemies
	CreateEnemies();
}

EnemyList::~EnemyList()
{
	// bitmaps
	delete m_BmpGoombaPtr;
	m_BmpGoombaPtr = nullptr;

	delete m_BmpTroopaPtr;
	m_BmpTroopaPtr = nullptr;

	// enemy array
	for (int i = 0; i < NR_OF_ENEMIES; ++i)
	{
		if (m_EnemyPtrArr[i] != nullptr)
		{
			m_EnemyPtrArr[i]->DeleteHitRegion();	// delete hitregions of enemies
			delete m_EnemyPtrArr[i];				// delete the enemy object
			m_EnemyPtrArr[i] = nullptr;
		}
	}
}

EnemyList* EnemyList::GetSingleton()
{
	if (m_EnemyListPtr == nullptr)
	{
		m_EnemyListPtr = new EnemyList;
	}
	return m_EnemyListPtr;
}

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

void EnemyList::CreateEnemies()
{
	// load from file
	LoadFromFile();
}

void EnemyList::Tick(double deltaTime)
{
	for (int i = 0; i < NR_OF_ENEMIES; ++i)
	{
		if (m_EnemyPtrArr[i] != nullptr)
		{
			if (m_EnemyPtrArr[i]->Destory())
			{
				delete m_EnemyPtrArr[i];
				m_EnemyPtrArr[i] = nullptr;
			}
			else
				m_EnemyPtrArr[i]->Tick(deltaTime);
		}
	}
}

void EnemyList::Paint(MATRIX3X2 matView)
{
	for (int i = 0; i < NR_OF_ENEMIES; ++i)
		if (m_EnemyPtrArr[i] != nullptr)
			m_EnemyPtrArr[i]->Paint(matView);
}

void EnemyList::DrawDebug(MATRIX3X2 matView)
{
	for (int i = 0; i < NR_OF_ENEMIES; ++i)
		if (m_EnemyPtrArr[i] != nullptr)
			m_EnemyPtrArr[i]->DrawDebug(matView);
}

RECT2 EnemyList::GetAABBRect(int index)
{
	if (m_EnemyPtrArr[index] != nullptr)
		return m_EnemyPtrArr[index]->GetAABBRect();
	else
	{
		RECT2 empty;
		return empty;
	}
}

void EnemyList::Move(int index, DOUBLE2 moveValue)
{
	if (m_EnemyPtrArr[index] != nullptr)
		m_EnemyPtrArr[index]->Move(moveValue);
}

void EnemyList::SetVelocityToZero(int index, bool velocityX, bool velocityY)
{
	if (m_EnemyPtrArr[index] != nullptr)
		m_EnemyPtrArr[index]->SetVelocityToZero(velocityX, velocityY);
}

void EnemyList::IsJumpedOn(int index)
{
	m_EnemyPtrArr[index]->IsJumpedOn();
}

void EnemyList::IsFiredOn(int index)
{
	if (m_EnemyPtrArr[index] != nullptr)
		m_EnemyPtrArr[index]->IsFiredOn();
}

int EnemyList::GetTypeOfEnemy(int index)
{
	int type;

	type = m_EnemyPtrArr[index]->GetEnemyID();

	return type;
}

void EnemyList::SetDirection(int index, bool marioDir)
{
	m_EnemyPtrArr[index]->SetDirection(marioDir);	
}

DOUBLE2 EnemyList::GetPos(int index)
{
	if (m_EnemyPtrArr[index] != nullptr)
		return m_EnemyPtrArr[index]->GetPos();
	else 
		return DOUBLE2 (0,0);
}

void EnemyList::LoadFromFile()
{
	using namespace std;

	string sFileName = "Resources/Files/Data_Enemies.txt";
	ifstream file(sFileName);

	if (!file)
	{
		GAME_ENGINE->MessageBox(String("File <Data_Enemies.txt> Could not be found!"));
	}
	else
	{
		int posX=0;
		int posY=0;
		int type=0;
		int i=0;

		string sLine;
		while ( !file.eof() )
		{
			getline(file, sLine);
			if (sLine != "")
			{
				string sVar = sLine.substr(0, 15);
				string sValue = sLine.substr(15, sLine.length());

				if (sVar == "PositionX:     ")
				{
					posX = stoi(sValue);
				}
				else if (sVar == "PositionY:     ")
				{
					posY = stoi(sValue);
				}
				else if (sVar == "Type:          ")
				{
					type = stoi(sValue);

					if (posX && posY)
					{
						if (type == 0)
							m_EnemyPtrArr[i++] = new Goomba(posX, posY, m_BmpGoombaPtr);
						if (type == 1)
							m_EnemyPtrArr[i++] = new Troopa(posX, posY, m_BmpTroopaPtr);

						posX=0;
						posY=0;
						type=0;
						sVar.clear();
					}
				}
			}
		}
	}
	file.close();
}
