/** 
 * \file Level2D.cpp
 * \brief This helper file draws 2D GUI while in level mode.
 * While player is is level mode, he can see number of lives, level number, timer, all displayed with 2D sprites.
 * 
 * \author Petar Bajic, MPE (C) All Rights Reserved, Homepage: www.mystic-peanut.com
 * \date January, 16 2007.
 */

#include "Level2D.h"
#include "Language.h"
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include <strsafe.h>
#pragma warning( default : 4996 )

/**
 * \brief Standard constructor.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
CLevel2D::CLevel2D()
{
	m_outTextureSucc = NULL;
	m_outTextureFail = NULL;
	m_DoubleFlower = NULL;
	m_HarmlessFlower = NULL;
	m_GoldenCoin = NULL;
	m_HeartIcon = NULL;
	m_SheepIcon = NULL;
	m_GreenBar = NULL;
	m_YellowPannel = NULL;
	m_WhitePannel = NULL;
	m_pFontBig = NULL;
	m_pFont = NULL;
	m_sprite = NULL;
	m_ScreenWidth = 0;
	m_ScreenHeight = 0;
}

/**
 * \brief Standard destructor.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
CLevel2D::~CLevel2D()
{
	SAFE_RELEASE(m_sprite);
	SAFE_RELEASE(m_pFont);
	SAFE_RELEASE(m_pFontBig);
	SAFE_RELEASE(m_outTextureSucc);
	SAFE_RELEASE(m_outTextureFail);
	SAFE_RELEASE(m_DoubleFlower);
	SAFE_RELEASE(m_HarmlessFlower);
	SAFE_RELEASE(m_GoldenCoin);
	SAFE_RELEASE(m_HeartIcon);
	SAFE_RELEASE(m_SheepIcon);
	SAFE_RELEASE(m_GreenBar);
	SAFE_RELEASE(m_YellowPannel);
	SAFE_RELEASE(m_WhitePannel);
}

/**
 * \brief Creates sprite and fonts and loads all 2d images to be drawn as GUI.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
bool CLevel2D::Init(HWND hWnd, LPDIRECT3DDEVICE9 pd3dDevice, int screenWidth, int screenHeight)
{
	m_ScreenWidth = screenWidth;
	m_ScreenHeight = screenHeight;

	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/intsucc.png", &m_outTextureSucc )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/intsucc.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/intfail.png", &m_outTextureFail )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/intfail.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/dblflower.png", &m_DoubleFlower )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/dblflower.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/hmlflower.png", &m_HarmlessFlower )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/hmlflower.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/coin.png", &m_GoldenCoin )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/coin.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/heart.png", &m_HeartIcon )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/heart.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/sheepicon.png", &m_SheepIcon )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/sheepicon.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/greenbar.png", &m_GreenBar )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/greenbar.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/white.png", &m_WhitePannel )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/white.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateTextureFromFile(pd3dDevice,L"Media/Pics/yellowpannel.png", &m_YellowPannel )))
	{
		MessageBox(hWnd,L"Could not load Media/Pics/yellowpanel.png file",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateFont(pd3dDevice,18, 0, 0, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial",&m_pFont)))
	{
		MessageBox(hWnd,L"Could not create font",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateFont(pd3dDevice,48, 0, 0, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial",&m_pFontBig)))
	{
		MessageBox(hWnd,L"Could not create font",L"Error",MB_OK);
		return false;
	}
	if (FAILED(D3DXCreateSprite(pd3dDevice,&m_sprite)))
	{
		MessageBox(hWnd,L"Could not create sprite EC_MPE_1531",L"Error",MB_OK);
		return false;
	}
	return true;
}

/**
 * \brief Its important to call this function BEFORE device is reset, like when switching to/from fullscreen mode.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CLevel2D::OnLostDevice()
{
	m_sprite->OnLostDevice();
	m_pFont->OnLostDevice();
	m_pFontBig->OnLostDevice();
}

/**
 * \brief Its important to call this function AFTER device is reset, like when switching to/from fullscreen mode.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CLevel2D::OnResetDevice(LPDIRECT3DDEVICE9 pd3dDevice, int screenWidth, int screenHeight)
{
	//Important: this is not game level reset, only 3D device reset.
	//game should continue intact.

	m_ScreenWidth = screenWidth;
	m_ScreenHeight = screenHeight;
	m_sprite->OnResetDevice();
	m_pFont->OnResetDevice();
	m_pFontBig->OnResetDevice();
	
}

/**
 * \brief Render function draws remaining lives icon and number, level number, 
 * various dialogs that pop-up during game, ... everything 2D in game level is rendered here.
 *
 * \param harmlessFlower if true, flower icon is rendered
 * \param doubleFlower if true, flower icon is rendered
 * \param elapsedTime there is no OnFrameMove function here, so everything is moved on Render :)
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void CLevel2D::Render(LPDIRECT3DDEVICE9 pd3dDevice, bool finish, int outcome, float elapsedTime, bool edge_warning, bool doubleFlower, 
					  bool harmlessFlower, int level, int lives, int countdown, bool paused, CLanguage* language)
{
	D3DXMATRIX mat;
	D3DXVECTOR2 spriteCentre=D3DXVECTOR2(0.0f,0.0f);
	D3DXVECTOR2 scaling=D3DXVECTOR2(0.6f,0.3f);
	D3DXVECTOR2 sheepscaling=D3DXVECTOR2(0.8f,0.8f);
	D3DXVECTOR2 fontscaling=D3DXVECTOR2(1.0f,1.0f);
	D3DXVECTOR2 trans=D3DXVECTOR2(0.0f,0.0f);
	D3DXVECTOR2 texTrans=D3DXVECTOR2(0.0f,0.0f);
	D3DXVECTOR2 footerTrans=D3DXVECTOR2(0.0f,0.0f);
	WCHAR wstr[250];
	float rotation=0; //timeGetTime()/500.0f;
	RECT rect;
	rect.top = 0;
	rect.bottom = m_ScreenHeight;
	rect.left = 0;
	rect.right = m_ScreenWidth;
	RECT rectSmall;
	rectSmall.top = 30;
	rectSmall.bottom = 80;
	rectSmall.left = 0;
	rectSmall.right = 105;
	static float timerBlink = 0;
	timerBlink += elapsedTime;

	pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
	m_sprite->Begin(D3DXSPRITE_ALPHABLEND);

	//Icons Rendering:

	//Level number
	trans.x = 30.0f;
	trans.y = 30.0f;
	scaling.x = 0.5f;
	scaling.y = 0.5f;
	my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &trans, m_GoldenCoin, m_sprite, true, NULL, NULL, NULL, NULL);
	texTrans.x = 45.0f;
	texTrans.y = 10.0f;
	StringCchPrintf(&wstr[0], 250,L"%d", level);
	my_DrawText(&mat,&fontscaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFontBig, wstr, &rectSmall, 0xFF880000);

	//Lives number
	trans.x = 130.0f;
	my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &trans, m_HeartIcon, m_sprite, true, NULL, NULL, NULL, NULL);
	texTrans.x = 150.0f;
	texTrans.y = 10.0f;
	StringCchPrintf(&wstr[0], 250,L"%d", lives);
	my_DrawText(&mat,&fontscaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFontBig, wstr, &rectSmall, 0xFF880000);

	//Time and Sheep
	trans.x = m_ScreenWidth/2 - 30.0f;
	trans.y = 0.0f;
	my_DrawSprite(&mat, &sheepscaling, &spriteCentre, rotation, &trans, m_WhitePannel, m_sprite, true, NULL, NULL, NULL, NULL);
	trans.y = 84;
	for ( int a = 5; a < countdown; a+=6)
	{
		//draw filling
		trans.y -= 5;
		my_DrawSprite(&mat, &sheepscaling, &spriteCentre, rotation, &trans, m_GreenBar, m_sprite, true, NULL, NULL, NULL, NULL);
	}
	trans.y = 0.0f;
	sheepscaling.x = 0.45f;
	sheepscaling.y = 0.4f;
	my_DrawSprite(&mat, &sheepscaling, &spriteCentre, rotation, &trans, m_SheepIcon, m_sprite, true, NULL, NULL, NULL, NULL);
	texTrans.x = m_ScreenWidth/2 - 25.0f;
	texTrans.y = 0.0f;
	StringCchPrintf(&wstr[0], 250,L"%d", LEVEL_DURATION - countdown);
	my_DrawText(&mat,&fontscaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFontBig, wstr, &rectSmall, 0xFF880000);

	//Flower Double
	if(doubleFlower)
	{
		trans.x = m_ScreenWidth - 90.0f;
		trans.y = 30.0f;
		//Render Success Interface Screen
		my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &trans, m_DoubleFlower, m_sprite, true, NULL, NULL, NULL, NULL);
	}

	//Flower Harmless
	if(harmlessFlower)
	{
		trans.x = m_ScreenWidth - 170.0f;
		trans.y = 30.0f;
		//Render Success Interface Screen
		my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &trans, m_HarmlessFlower, m_sprite, true, NULL, NULL, NULL, NULL);
	}

	//Render Paused
	if(paused && !finish)
	{
		trans.x = 0.0f;
		trans.y = (float)m_ScreenHeight/2;
		rectSmall.right = 200;
		my_DrawText(&mat,&fontscaling,&spriteCentre,rotation,&trans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_PRESS_SPACE), &rect, 0xFFF9F020);
		if (timerBlink > 0.5)
		{
			trans.x = m_ScreenWidth/2 - 115.0f;
			trans.y = m_ScreenHeight/2 - 100.0f;
			my_DrawText(&mat,&fontscaling,&spriteCentre,rotation,&trans,m_sprite, m_pFontBig, language->GetText(MPE_TEXT_LEVEL_PAUSED), &rectSmall, 0xFF990000);
			if(timerBlink > 1.0)
				timerBlink = 0.0f;
		}
	}

	//Render finish screens
	if(finish)
	{
		switch (outcome)
		{
		case 0: //Sucess
			{
				trans.x = m_ScreenWidth/2 - 175.0f;
				trans.y = m_ScreenHeight/2 - 110.0f;
				scaling.x = 0.7f;
				scaling.y = 0.8f;
				//Render Success Interface Screen
				my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &trans, m_outTextureSucc, m_sprite, true, NULL, NULL, NULL, NULL);
				scaling.x = 1.0f;
				scaling.y = 1.0f;
				texTrans.y = m_ScreenHeight/2 - 35.f;
				texTrans.x = 0.0f;
				my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFontBig, language->GetText(MPE_TEXT_LEVEL_SUCCESS), &rect, 0xFF008800);
				texTrans.y = (float)m_ScreenHeight/2+10;
				my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_SAVED_SHEEPS), &rect, 0xFF008800);
				texTrans.y = m_ScreenHeight/2 + 60.f;
				texTrans.x = 0.0f;
				if (timerBlink > 0.5)
				{
					my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_PRESS_SPACE_EXIT), &rect, 0xFFF8F020);
					if(timerBlink > 1.0)
						timerBlink = 0.0f;
				}
			}
			break;
		case 1: //Sheep stolen
			{
				trans.x = m_ScreenWidth/2 - 175.0f;
				trans.y = m_ScreenHeight/2 - 110.0f;
				scaling.x = 0.7f;
				scaling.y = 0.8f;
				//Render Failure Interface Screen
				my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &trans, m_outTextureFail, m_sprite, true, NULL, NULL, NULL, NULL);
				scaling.x = 1.0f;
				scaling.y = 1.0f;
				texTrans.y = m_ScreenHeight/2 - 35.f;
				texTrans.x = 0.0f;
				my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFontBig, language->GetText(MPE_TEXT_LEVEL_OOOPS), &rect, 0xFF770000);
				texTrans.y = (float)m_ScreenHeight/2+10;
				my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_STOLEN_SHEEP), &rect, 0xFFF8F020);
				if(lives == 0)
				{
					texTrans.y = m_ScreenHeight/2 + 30.f;
					my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_THIRD_TIME), &rect, 0xFFF8F020);
				}
				texTrans.y = m_ScreenHeight/2 + 60.f;
				texTrans.x = 0.0f;
				if (timerBlink > 0.5)
				{
					my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_PRESS_SPACE_EXIT), &rect, 0xFFF8F020);
					if(timerBlink > 1.0)
						timerBlink = 0.0f;
				}
			}
			break;
		case 2: //Sheep shot
			{
				trans.x = m_ScreenWidth/2 - 175.0f;
				trans.y = m_ScreenHeight/2 - 110.0f;
				scaling.x = 0.7f;
				scaling.y = 0.8f;
				//Render Failure Interface Screen
				my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &trans, m_outTextureFail, m_sprite, true, NULL, NULL, NULL, NULL);
				scaling.x = 1.0f;
				scaling.y = 1.0f;
				texTrans.y = m_ScreenHeight/2 - 35.f;
				texTrans.x = 0.0f;
				my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFontBig, language->GetText(MPE_TEXT_LEVEL_OOOPS), &rect, 0xFF770000);
				texTrans.y = (float)m_ScreenHeight/2+10;
				my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_KILLED_SHEEP), &rect, 0xFFF8F020);
				if(lives == 0)
				{
					texTrans.y = m_ScreenHeight/2 + 30.f;
					my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_THIRD_TIME), &rect, 0xFFF8F020);
				}
				texTrans.y = m_ScreenHeight/2 + 60.f;
				texTrans.x = 0.0f;
				if (timerBlink > 0.5)
				{
					my_DrawText(&mat,&scaling,&spriteCentre,rotation,&texTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_PRESS_SPACE_EXIT), &rect, 0xFFF8F020);
					if(timerBlink > 1.0)
						timerBlink = 0.0f;
				}
			}
			break;
		}
	}

	//Render text on the bottom left of the screen
	/*trans.x = 5.0f;
	trans.y = m_ScreenHeight - 110.0f;
	scaling.x = 0.27f;
	scaling.y = 0.4f;
	my_DrawSprite(&mat, &scaling, &spriteCentre, rotation, &trans, m_YellowPannel, m_sprite, false, NULL, NULL, NULL, NULL);
	scaling.x = 1.0f;
	scaling.y = 1.0f;
	footerTrans.x = -m_ScreenWidth/2 +70.0f;
	footerTrans.y = m_ScreenHeight - 90.0f;
	StringCchPrintf(&wstr[0], 250,L"Framerate: %d", framerate);
	my_DrawText(&mat,&scaling,&spriteCentre,rotation,&footerTrans,m_sprite, m_pFont, wstr, &rect, 0xFFB90000);
	footerTrans.x = -m_ScreenWidth/2 + 134.0f;
	footerTrans.y = m_ScreenHeight - 70.0f;
	my_DrawText(&mat,&scaling,&spriteCentre,rotation,&footerTrans,m_sprite, m_pFont, L"Move: WASD, Shoot: Mouse left.", &rect, 0xFFB90000);
	footerTrans.x = -m_ScreenWidth/2 + 126.0f;
	footerTrans.y = m_ScreenHeight - 50.0f;
	my_DrawText(&mat,&scaling,&spriteCentre,rotation,&footerTrans,m_sprite, m_pFont, L"ESC to exit, SPACE to pause.", &rect, 0xFFB90000);*/
	if(edge_warning)
	{
		scaling.x = 1.0f;
		scaling.y = 1.0f;
		footerTrans.x = 0.0f;
		footerTrans.y = m_ScreenHeight - 50.0f;
		my_DrawText(&mat,&scaling,&spriteCentre,rotation,&footerTrans,m_sprite, m_pFont, language->GetText(MPE_TEXT_LEVEL_BORDER_WARNING), &rect, 0xFFB90000);
	}


	m_sprite->End();
	pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );

}

