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

//---------------------------
// Includes
//---------------------------
#include "HUD.h"
#include "SuperMarioBros.h"

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

//---------------------------
// Defines
//---------------------------
#define GAME_ENGINE		(GameEngine::GetSingleton())
#define COLLISION_LIST	(CollisionList::GetSingleton())
HUD* HUD::m_HUDPtr = nullptr;

//---------------------------
// Constructor & Destructor
//---------------------------
HUD::HUD()
	//int
	:m_Time(TIME_START)
	,m_TickCount()
	,m_Score()
	,m_BitmapWidth()
	,m_BitmapHeight()
	,m_CoinCount()
	,m_CoinWidth()
	,m_CoinHeight()
	,m_DeltaTimeMaxDelay(30)
	,m_CoinAnimationStep()
	//double
	,m_DeltaTime()
	,m_DeltaTimeMax()
	//bitmap
	,m_BmpFontPtr(nullptr)
	,m_BmpCoinIconPtr(nullptr)
{
	// init bitmap
	m_BmpFontPtr = new Bitmap("Resources/Images/Font.bmp");
	m_BmpFontPtr->SetTransparencyColor(0,0,1);
	m_BitmapWidth = m_BmpFontPtr->GetWidth();
	m_BitmapHeight = m_BmpFontPtr->GetHeight();

	m_BmpCoinIconPtr = new Bitmap("Resources/Images/CoinIcon.bmp");
	m_BmpCoinIconPtr->SetTransparencyColor(255,0,255);
	m_CoinWidth = m_BmpCoinIconPtr->GetWidth();
	m_CoinHeight = m_BmpCoinIconPtr->GetHeight();
}

HUD::~HUD()
{
	delete m_BmpFontPtr;
	m_BmpFontPtr = nullptr;

	delete m_BmpCoinIconPtr;
	m_BmpCoinIconPtr = nullptr;
}

HUD* HUD::GetSingleton()
{
	if (m_HUDPtr == nullptr)
	{
		m_HUDPtr = new HUD();
	}
	return m_HUDPtr;
}

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

void HUD::Tick(double deltaTime)
{
	++m_TickCount;
	--m_DeltaTimeMaxDelay;
	if (m_DeltaTimeMaxDelay < 0) m_DeltaTimeMaxDelay = 0;
	m_DeltaTime = deltaTime;

	// timer count down
	if ((m_TickCount % 30) == 0)		// 1 sec IRL == 2 secs InGame
		--m_Time;

	// coin icon tick
	if (m_TickCount % ANIMATION_INTERVAL == 0)
	{
		++m_CoinAnimationStep;
		m_CoinAnimationStep = m_CoinAnimationStep % ANIMATION_STEPS;
	}

}

void HUD::Paint()
{
	// view
	GAME_ENGINE->SetTransformMatrix(MATRIX3X2::CreateIdentityMatrix());

	// paint
	int fontSize = 26;
	int row1Height = 20;	//20
	int row2Height = row1Height + fontSize;		
	int spacing = 270;
	int posScore = 50;
	int posCoins = posScore + spacing;
	int posWorld = posCoins + spacing;
	int posTime = posWorld + spacing;

	DrawBitmapString(String("MARIO"), posScore,row1Height);

	String score = NumberWithLeadingZeros(m_Score, 6);
	DrawBitmapString(score, posScore, row2Height);

	DrawBitmapString(String("x ")+m_CoinCount, posCoins, row2Height);

	DrawBitmapString(String("WORLD"), posWorld, row1Height);
	DrawBitmapString(String("1-1"), posWorld + fontSize, row2Height);

	DrawBitmapString(String("TIME"), posTime, row1Height);
	String time = NumberWithLeadingZeros(m_Time, 3);
	DrawBitmapString(String(" ")+time, posTime, row2Height);

	// draw delta time
	DrawDeltaTimeBar(false);	// paint in debug method in MainGame file

	// draw coin icon
	DrawCoinIcon(posCoins - (m_CoinWidth/2),row2Height);
	//DrawCoinIcon(0,0);
}

void HUD::DrawBitmapString(String input, int x, int y)
{
	int stringLength = input.GetLength();

	for (int i = 0; i < stringLength; ++i)
	{
		int index = input.CharAt(i) - 32;
		int col = index % NR_LETTERS_PER_ROW;
		int row = index / NR_LETTERS_PER_ROW;
		int xPos = x + (LETTER_WIDTH*HUD_SCALE)*i;
		int yPos = y;

		RECT r;
		r.top = LETTER_HEIGHT*row;
		r.bottom = r.top + LETTER_HEIGHT;
		r.left = LETTER_WIDTH*col;
		r.right = r.left + LETTER_WIDTH;

		MATRIX3X2 matScale, matTranslate, matTransform;
		matTranslate.SetAsTranslate(xPos, yPos);
		matScale.SetAsScale(HUD_SCALE);
		matTransform = matScale * matTranslate;
		GAME_ENGINE->SetTransformMatrix(matTransform);

		GAME_ENGINE->DrawBitmap(m_BmpFontPtr, 0, 0, r);
	}
}

void HUD::DrawDeltaTimeBar(bool show)
{
	if (show)
	{
		GAME_ENGINE->SetTransformMatrix(MATRIX3X2::CreateIdentityMatrix());

		// set dT max
		if (m_DeltaTimeMax < m_DeltaTime && m_DeltaTimeMaxDelay <= 0)	// delay is used so it does not count the delta time from the startup :)
			m_DeltaTimeMax = m_DeltaTime;

		int barHeight = 11;
		int barWidth = GAME_ENGINE->GetWidth();
		double multiplier = barWidth / 0.05;	// 0.05 is the max value
		double deltaTimeSize = m_DeltaTime*multiplier;
		double deltaTimeMaxSize = m_DeltaTimeMax*multiplier;

		GAME_ENGINE->SetColor(COLOR(210,105,30));
		GAME_ENGINE->FillRect(0,0, int(deltaTimeMaxSize), barHeight);

		GAME_ENGINE->SetColor(COLOR(178,34,34));
		GAME_ENGINE->FillRect(0,0, int(deltaTimeSize), barHeight);		// draw red bar

		GAME_ENGINE->SetColor(COLOR(0,0,0));
		GAME_ENGINE->DrawRect(0,0, barWidth, barHeight);			// draw bar frame

		GAME_ENGINE->SetDefaultFont();
		GAME_ENGINE->DrawString(String("deltaTime: ")+m_DeltaTime 
			+"					Peak: " +m_DeltaTimeMax +"					Max: 0.05",0,-2);		// draw numbers
	}
}

void HUD::AddCoin()
{
	++m_CoinCount;
	AddScore(COLLISION_LIST->SCORE_PICKUP_COIN);
}

void HUD::AddScore(int addValue)
{
	m_Score += addValue;
}

void HUD::DrawCoinIcon(int x, int y)
{
	RECT r;
	r.top = 0;
	r.bottom = r.top + m_CoinHeight;
	r.left = (m_CoinWidth/ANIMATION_STEPS) * m_CoinAnimationStep;
	r.right = r.left + m_CoinWidth/ANIMATION_STEPS;

	MATRIX3X2 matTranslate, matScale, matTransform;
	matTranslate.SetAsTranslate(x,y);
	matScale.SetAsScale(HUD_SCALE);
	matTransform = matScale * matTranslate;
	GAME_ENGINE->SetTransformMatrix(matTransform);

	GAME_ENGINE->DrawBitmap(m_BmpCoinIconPtr, 0, 0, r);
}

String HUD::NumberWithLeadingZeros(int number, int max)
{
	// create stringstream obj
	ostringstream input;
	input << right << setw(max) << setfill('0') << number << endl;

	// convert to String
	String output = input.str().c_str();
	return output;
}

int HUD::GetTime()
{
	return m_Time;
}

void HUD::DrawScorePopUp(DOUBLE2 position, int popUpVlaue)
{
	//MATRIX3X2 matScale, matTransform;
	//double scale = .3;
	//matScale.SetAsScale(scale);
	//matTransform = matScale;
	//GAME_ENGINE->SetTransformMatrix(matTransform);

	GAME_ENGINE->SetTransformMatrix(MATRIX3X2::CreateIdentityMatrix());

	String text = String("")+popUpVlaue;
	int lenght = text.GetLength();

	DrawBitmapString(text, (GAME_ENGINE->GetWidth()+lenght)/2, 100);
}






