/*********************************************************************
*	HUD ( head up display )
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 1.9.2008
*	Soubor: HUD.cpp
*	Popis: Zde je definovana trida pro predpis vykreslovani a ovladani
*			informacnich panelu ve hre jako napriklad zdravi, pocet naboju
*			zamerovac.
**********************************************************************/

#include "HUD.h"
#include "mathematic.h"
#include "textLibrary.h"
#include "sys_controller.h"
#include "SDL_to_GFG.h"
#include "graphics.h"
#include "HUD_health.h"
#include "HUD_oxygen.h"
#include "HUD_crosshair.h"
#include "HUD_ammo.h"
#include "HUD_energy.h"
#include "HUD_targets.h"
#include "HUD_gametime.h"
#include "HUD_level_report.h"
#include "HUD_fps.h"
using namespace HUD;

int HUD::g_iHUDState = NORMAL;
CHUD HUD::hud;


CHUD::CHUD() : CMainWindow()
{
	m_pCrosshairWindow = NULL;
}

CHUD::~CHUD()
{
	deleteAll();
}

void CHUD::use_this_run()
{
	if(  m_fMyAlpha > 0.0f || m_fTargetAlpha > 0.0f  )
	{
		run();
		for( list<HUDBaseWindow*>::iterator iter = m_windows.begin(); iter != m_windows.end(); iter++ )
			(*iter)->onRun();
	}
}

void HUDButtonCallback( CBaseComponent *pComponent, void * data )
{
	CHUD *pHUDWindow = static_cast<CHUD*>(data);
	if( !pHUDWindow )
		return;

	switch( pComponent->getObjectID() )
	{
		case HUD_BUTTON_OK:
			pHUDWindow->onSave();
			break;
		case HUD_BUTTON_DEFAULT:
			pHUDWindow->onDefault();
			break;
		case HUD_BUTTON_CANCEL:
			pHUDWindow->onCancel();
			break;

	}

}

void CHUD::onSave()
{
	for( list<HUDBaseWindow*>::iterator iter = m_windows.begin(); iter != m_windows.end(); iter++ )
		(*iter)->onSave();
	systemController::setLastRunningLevel();
}

void CHUD::onCancel()
{
	refresh();
	systemController::setLastRunningLevel();
}

void CHUD::onDefault()
{
	for( list<HUDBaseWindow*>::iterator iter = m_windows.begin(); iter != m_windows.end(); iter++ )
		(*iter)->onDefault();
	
}

#include "sys_console.h"
using namespace systemConsole;
void CHUD::init( float width, float height )
{
	console << "Creating HUD environment...";
	
	setSize( width, height );

	m_pHUDWindow = new CSubWindow( 100.0f, height / 2.0f + WINDOW_HEIGHT / 2.0f, BUTTON_WIDTH, WINDOW_HEIGHT, GFG_SUBWINDOW_PACKING );
	m_pHUDWindow->setCaption( textLibrary::getText( TXT_HUD_SETTINGS ) );
	addComponent( m_pHUDWindow );

	GFG_setCallback_default( GFG_newCallback( HUDButtonCallback, this ) );

	ContainerRelativeUpToDown *container = new ContainerRelativeUpToDown();
	m_pHUDWindow->setComponent( container );
	container->setHorizontalPadding( CONTAINER_HOR_PADDING );

	m_pButtonOK = GFG_newButton( textLibrary::getText( TXT_OK ), HUD_BUTTON_OK );
	container->addComponent( m_pButtonOK );	

	m_pButtonDefault = GFG_newButton( textLibrary::getText( TXT_STANDARD ), HUD_BUTTON_DEFAULT );
	container->addComponent( m_pButtonDefault );	
	m_pButtonCancel = GFG_newButton( textLibrary::getText( TXT_CLOSE ), HUD_BUTTON_CANCEL );
	container->addComponent( m_pButtonCancel );	
	
	HUDBaseWindow *newWindow = new HealthWindow();
	newWindow->setVisible( true );
	addWindow( newWindow );
	
	newWindow = new OxygenWindow();
	newWindow->setVisible( true );
	addWindow( newWindow );

	m_pCrosshairWindow = new CrosshairWindow();
	m_pCrosshairWindow->setVisible( true );
	addWindow( m_pCrosshairWindow );

	newWindow = new AmmoWindow();
	newWindow->setVisible( true );
	addWindow( newWindow );

	newWindow = new EnergyWindow();
	newWindow->setVisible( true );
	addWindow( newWindow );

	newWindow = new TargetsWindow();
	newWindow->setVisible( true );
	addWindow( newWindow );

	newWindow = new GameTimeWindow();
	newWindow->setVisible( true );
	addWindow( newWindow );

	newWindow = new LevelReportWindow();
	newWindow->setVisible( true );
	addWindow( newWindow );

	newWindow = new FpsWindow();
	newWindow->setVisible( true );
	addWindow( newWindow );

	setMyAlphaMultiplier( ALPHA_TRANSPARENT );
	m_fMyAlpha = ALPHA_TRANSPARENT;
	console << "done" << endline;
}

void CHUD::setState( int state )
{
	 g_iHUDState = getFromRange( state, NORMAL, SETTING );

	switch( g_iHUDState )
	{
	case NORMAL:
		m_pHUDWindow->setVisible( false );
		break;
	case SETTING:
		m_pHUDWindow->setVisible( true );
		break;
	}
}

void CHUD::HUD_SDL_controller( SDL_Event &event )
{
	/*if( !m_bIsActive )
		return;*/

	if( event.type == SDL_KEYDOWN )
	{
		switch( event.key.keysym.sym )
		{
			case SDLK_ESCAPE:
				systemController::setLastRunningLevel();
				//systemConsole::sys_console.fadeSwitch();
				return;
		}
	}


	conv::input( event, this );
}

void CHUD::use_this_draw()
{
	if( m_fMyAlpha > 0.0f )
	{
		glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
		
		glDisable(GL_DEPTH_TEST);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, m_fWidth, 0, m_fHeight, -1, 1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		draw();
		glPopAttrib();
	}
}

void CHUD::addWindow( HUDBaseWindow *window )
{
	if( window )
	{
		m_windows.push_back( window );	//musime znat vsechny ovladana okna
		addComponent( window );
	}

}
void CHUD::refresh()
{
	for( list<HUDBaseWindow*>::iterator iter = m_windows.begin(); iter != m_windows.end(); iter++ )
		(*iter)->onCancel();	// = refresh
}

void CHUD::setCrosshair( int id )
{
	if( m_pCrosshairWindow )
		reinterpret_cast<CrosshairWindow*>(m_pCrosshairWindow)->setCrosshair( id );
}


/***********************************************************************
*
***********************************************************************/


HUDBaseWindow::HUDBaseWindow()
{
	
}


void HUDBaseWindow::onCancelButtonClick()
{
	onDefault();
}

void HUDBaseWindow::draw( ScissorBox &scissorBox )
{
	if( m_fAlphaMultiplier < GFG_SUBWINDOW_ALPHA_MULTIPLIER_STEP ) return;

	glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_SCISSOR_BIT);
	
	
	
	
	if( g_iHUDState == SETTING )
		windowBasicDraw( scissorBox );

	ScissorBox intersected = m_ScissorBox.intersection( scissorBox );
	intersected.y += m_fPackOffset;
	intersected.h -= min((float)m_fPackOffset, intersected.h);
	glScissor( (int)intersected.x, (int)intersected.y, (int)intersected.w, (int)intersected.h );
	//glEnable(GL_SCISSOR_TEST );
	if( !m_bPacked )
		componentDraw( intersected );

	glPopAttrib();

}


void HUDBaseWindow::HUDBaseWindowInit()
{
	m_iPackingButtonOffset = HUD_WINDOW_PACKING_BUTTON_OFFSET;
	subWindowInit( m_fGlobalXpos, m_fGlobalYpos, m_fWidth, m_fHeight );
}

void HUDBaseWindow::windowTopPanelDraw( ScissorBox &scissorBox )
{
	glBindTexture(GL_TEXTURE_2D, g_panelID );
	glBegin( GL_TRIANGLE_FAN );
		if( m_bFocused )
			setColor( HUD_COLOR_WINDOW_TOP_LIGHT );
		else
			setColor(  HUD_COLOR_WINDOW_TOP_LIGHTUNFOCUSED );
		
		glTexCoord2f( 0.5, 1 );
		glVertex2f( m_fGlobalXpos + m_fWidth/2, m_fGlobalYpos + m_fHeight );
		setColor( HUD_COLOR_WINDOW_TOP_DARK );

		glTexCoord2i( 0, 1 );	glVertex2f( m_fGlobalXpos, m_fGlobalYpos + m_fHeight  );

		setColor( HUD_COLOR_WINDOW_TOP_AVERAGE );
		glTexCoord2i( 0, 0 );	glVertex2f( m_fGlobalXpos, m_fGlobalYpos + m_fHeight - m_iTopPanelHeight );
		glTexCoord2i( 1, 0 );	glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos + m_fHeight - m_iTopPanelHeight );
		setColor( HUD_COLOR_WINDOW_TOP_DARK );
		glTexCoord2i( 1, 1 ); glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos + m_fHeight );
		
	glEnd();
}



