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

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

//---------------------------
// Defines
//---------------------------
#define GAME_ENGINE (GameEngine::GetSingleton())
#define SOUND		(Sound::GetSingleton())
#define MARIO		(Mario::GetSingleton())

Level* Level::m_LevelPtr=nullptr;
//---------------------------
// Constructor & Destructor
//---------------------------
Level::Level()
	:m_BmpLevelPtr(nullptr)
	,m_BmpCastleHalf(nullptr)
	,m_HitLevelPtr(nullptr)
	,m_BmpEnterancePipePtr(nullptr)
	,m_BmpExit1PipePtr(nullptr)
	,m_BmpExit2PipePtr(nullptr)
	,m_BmpFlagPtr(nullptr)

	,m_FlagCanDrop(false)
	,m_IsFlagDown(false)

{
	// double2's
	m_PipeEnterancePos = DOUBLE2 (914,136);
	m_PipeExit1Pos = DOUBLE2 (208,392);
	m_PipeExit2Pos = DOUBLE2 (2610,168);

	//bitmap
	m_BmpLevelPtr = new Bitmap("Resources/Images/World1-1.png");
	m_BmpCastleHalf = new Bitmap("Resources/Images/CastleHalf.png");

	m_BmpEnterancePipePtr = new Bitmap("Resources/Images/PipeVerticalBig.bmp");
	m_BmpExit1PipePtr = new Bitmap("Resources/Images/PipeHorizontal.bmp");
	m_BmpExit2PipePtr = new Bitmap("Resources/Images/PipeVerticalSmall.bmp");

	m_BmpFlagPtr = new Bitmap("Resources/Images/Flag.bmp");
	m_BmpFlagPtr->SetTransparencyColor(255,0,255);

	// hitregion
	m_HitLevelPtr = new HitRegion();
	m_HitLevelPtr->CreateFromSVG("Resources/Images/World1-1_Collision.svg");

	// AABB's
	// enterance
	int side = 3;
	m_EnteranceRect.bottom = int(m_PipeEnterancePos.y);
	m_EnteranceRect.top = m_EnteranceRect.bottom - 1;
	m_EnteranceRect.left = int(m_PipeEnterancePos.x + side);
	m_EnteranceRect.right = int(m_PipeEnterancePos.x) + m_BmpEnterancePipePtr->GetWidth() - side;

	// exit
	m_ExitRect.bottom = int(m_PipeExit1Pos.y) + m_BmpExit1PipePtr->GetHeight();
	m_ExitRect.top = m_ExitRect.bottom - 1;
	m_ExitRect.right = int(m_PipeExit1Pos.x);
	m_ExitRect.left = m_ExitRect.right - 9;

	// flag pos
	m_FlagPos = DOUBLE2 ( 3176 - m_BmpFlagPtr->GetWidth(), 41 );	// 3176 is center of pole

	// flag pole
	m_FlagPolePos = DOUBLE2 ( 3175, 40 );
	m_FlagPoleRect.top = m_FlagPolePos.y;
	m_FlagPoleRect.bottom = 184;
	m_FlagPoleRect.left = m_FlagPolePos.x;
	m_FlagPoleRect.right = m_FlagPoleRect.left + 2;
}

Level::~Level()
{
	delete m_BmpLevelPtr;
	m_BmpLevelPtr = nullptr;

	delete m_BmpCastleHalf;
	m_BmpCastleHalf = nullptr;

	delete m_HitLevelPtr;
	m_HitLevelPtr = nullptr;

	delete m_BmpEnterancePipePtr;
	m_BmpEnterancePipePtr = nullptr;

	delete m_BmpExit1PipePtr;
	m_BmpExit1PipePtr = nullptr;

	delete m_BmpExit2PipePtr;
	m_BmpExit2PipePtr = nullptr;

	delete m_BmpFlagPtr;
	m_BmpFlagPtr = nullptr;
}

Level* Level::GetSingleton()
{
	if (m_LevelPtr == nullptr)
	{
		m_LevelPtr = new Level();
	}
	return m_LevelPtr;
}

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

void Level::Tick(double deltaTime)
{
	if (m_FlagCanDrop)
	{
		int speed = 120;
		int lowestPoint = 162;

		if (m_FlagPos.y < lowestPoint)
			m_FlagPos.y += speed * deltaTime;
		else
		{
			m_FlagPos.y = lowestPoint;
			m_IsFlagDown = true;
		}
	}

	if (m_IsFlagDown)
	{
		MARIO->StartPipeAnimation(2);
		SOUND->Theme(4);
	}
}

void Level::Paint(MATRIX3X2 matView)
{
	GAME_ENGINE->SetTransformMatrix(matView);
	GAME_ENGINE->DrawBitmap(m_BmpLevelPtr);

	// draw flag
	MATRIX3X2  matTranslate, matTransform;
	matTranslate.SetAsTranslate(m_FlagPos);

	matTransform = matTranslate * matView;
	GAME_ENGINE->DrawBitmap(m_BmpFlagPtr, m_FlagPos);

	// for other paints 
	GAME_ENGINE->SetTransformMatrix(matView);
}

void Level::DrawHitRegion(MATRIX3X2 matView)
{
	GAME_ENGINE->SetTransformMatrix(matView);
	// level svg
	GAME_ENGINE->SetColor(COLOR(255,0,0,150));
	GAME_ENGINE->FillHitRegion(m_HitLevelPtr);
	// flag pole rect
	GAME_ENGINE->SetColor(COLOR(0,0,255,150));
	GAME_ENGINE->FillRect(m_FlagPoleRect);
}

HitRegion* Level::GetHitRegion()
{
	return m_HitLevelPtr;
}

int Level::GetWidth()
{
	return m_BmpLevelPtr->GetWidth();
}

int Level::GetHeight()
{
	return m_BmpLevelPtr->GetHeight();
}

void Level::PaintCastleHalf(MATRIX3X2 matView)
{
	MATRIX3X2 matTranslate, matTransform;
	matTranslate.SetAsTranslate(3272,0);
	matTransform = matTranslate * matView;
	GAME_ENGINE->SetTransformMatrix(matTransform);
	GAME_ENGINE->DrawBitmap(m_BmpCastleHalf);
}

void Level::PaintPipeParts(MATRIX3X2 matView)
{
	MATRIX3X2 matTransform, matTranslate;

	// enterance pipe
	matTranslate.SetAsTranslate(m_PipeEnterancePos);
	matTransform = matTranslate * matView;
	GAME_ENGINE->SetTransformMatrix(matTransform);
	GAME_ENGINE->DrawBitmap(m_BmpEnterancePipePtr);

	// exit pipe 1
	matTranslate.SetAsTranslate(m_PipeExit1Pos);
	matTransform = matTranslate * matView;
	GAME_ENGINE->SetTransformMatrix(matTransform);
	GAME_ENGINE->DrawBitmap(m_BmpExit1PipePtr);

	// exit pipe 2
	matTranslate.SetAsTranslate(m_PipeExit2Pos);
	matTransform = matTranslate * matView;
	GAME_ENGINE->SetTransformMatrix(matTransform);
	GAME_ENGINE->DrawBitmap(m_BmpExit2PipePtr);
}

RECT2 Level::GetEnterancePipeAABB()
{
	return m_EnteranceRect;
}

RECT2 Level::GetExitPipeAABB()
{
	return m_ExitRect;
}

void Level::DrawDebug(MATRIX3X2 matView)
{
	GAME_ENGINE->SetTransformMatrix(matView);
	GAME_ENGINE->SetColor(COLOR(0,0,255,100));

	// draw aabb enterance
	GAME_ENGINE->FillRect(m_EnteranceRect);

	// draw aabb exit
	GAME_ENGINE->FillRect(m_ExitRect);
}

RECT2 Level::GetFlagPoleAABB()
{
	return m_FlagPoleRect;
}

void Level::FlagCanDrop()
{
	m_FlagCanDrop = true;
}

bool Level::FlagIsDown()
{
	return m_IsFlagDown;
}



