//========= Copyright © 1996-2005, Sander Corporation, All rights reserved. ============//
//
// Purpose: hud_rts_unitpanel.cpp
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "hl2wars/rts_shareddefs.h"
#include "hl2wars/hud_rts_buildingpanel.h"
#include <vgui/ISurface.h>
#include <KeyValues.h>
#include "c_ai_basenpc.h"
#include "hl2wars/c_rts_buildings.h"
#include "c_rts_globalresource.h"
#include "hl2wars/c_hl2wars_player.h"
#include "hl2wars/hud_rts_unitpanel.h"
#include "hl2wars/in_buttons_rts.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"


using namespace vgui;

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudRTSBuildingPanel::CHudRTSBuildingPanel( ) : BaseClass( NULL, "BuildingPanel" )
{
	SetProportional( true );
	SetPaintBackgroundEnabled( false );
	SetKeyBoardInputEnabled( false );	
	SetMouseInputEnabled( true );	
	SetScheme("ClientScheme.res");

	SetBounds( 0,0, 256, 256 );
	SetBgColor( Color( 0,0,0,255 ) );  
}

//-----------------------------------------------------------------------------
// Purpose: Deconstructor
//-----------------------------------------------------------------------------
CHudRTSBuildingPanel::~CHudRTSBuildingPanel()
{

}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudRTSBuildingPanel::Init( void )
{
	SetCursor( GetParent()->GetCursor() );

	int flags = F_SHOWAMOUNT|F_ONEIMAGE;
	// create slot buttons
	m_pQueue[0] = new CBitmapButtonRTS( this, "queue_1", NULL);
	CreateButtonQueue( m_pQueue[0], "queue_1", flags);
	m_pQueue[1] = new CBitmapButtonRTS( this, "queue_2", NULL);
	CreateButtonQueue( m_pQueue[1], "queue_2", flags);
	m_pQueue[2] = new CBitmapButtonRTS( this, "queue_3", NULL);
	CreateButtonQueue( m_pQueue[2], "queue_3", flags);
	m_pQueue[3] = new CBitmapButtonRTS( this, "queue_4", NULL);
	CreateButtonQueue( m_pQueue[3], "queue_4", flags);
	m_pQueue[4] = new CBitmapButtonRTS( this, "queue_5", NULL);
	CreateButtonQueue( m_pQueue[4], "queue_5", flags);

	// cycle buttons
	m_pCycle[0] = new CBitmapButtonRTS( this, "cycle_1", NULL);
	CreateButtonQueue( m_pCycle[0], "cycle_1", F_ONEIMAGE);
	m_pCycle[1] = new CBitmapButtonRTS( this, "cycle_2", NULL);
	CreateButtonQueue( m_pCycle[1], "cycle_2", F_ONEIMAGE);
	m_pImageCyles[0].Init( GetVPanel(), "VGUI/buttons/queue_right.vmt");
	m_pImageCyles[1].Init( GetVPanel(), "VGUI/buttons/queue_left.vmt");

	color32 color;
	color.r = color.g = color.b = color.a = 1.0f;
	m_pCycle[0]->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageCyles[0] , color);
	m_pCycle[1]->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageCyles[1] , color);
}

//-----------------------------------------------------------------------------
// Purpose: Paint
//-----------------------------------------------------------------------------
void CHudRTSBuildingPanel::Paint( void )
{
	C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer ) 
		return;

	color32 color;
	color.r = color.g = color.b = color.a = 1.0f;

	bool showprogress = pPlayer->m_HLWLocal.m_bShowProgressQueue;

	if( !pPlayer->GetSelectedBuilding() )
		return;

	if( pPlayer->GetSelectedBuilding()->IsRTSBrush() )
	{
		C_BuildingBrush *m_pSelected = dynamic_cast<C_BuildingBrush *>(pPlayer->GetSelectedBuilding());
		if( !m_pSelected )
			return;

		if( m_pSelected->GetUnitIndex( 0 ) != 0 ) {
			float weigth = ( m_pSelected->GetBuildTime() - gpGlobals->curtime ) / m_pSelected->GetBuildTimeMax();
			m_pQueue[0]->SetProgress( weigth );
		}

		for( int i = 0; i < QUEUE_MAX; i++) {
			m_pQueue[i]->SetAmount( m_pSelected->GetUnitCount(i) );
			if( i == 0 && m_pSelected->GetUnitIndex( i ) != 0 && showprogress )
			{
				m_pQueue[i]->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, m_pUnitPanel->GetImage( m_pSelected->GetUnitType(  m_pSelected->GetUnitIndex( i ) ) ), color);
				m_pQueue[i]->AddButtonFlags( F_SHOWPROGRESS );
			}
			else
			{
				m_pQueue[i]->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, m_pUnitPanel->GetImage( m_pSelected->GetUnitType(  m_pSelected->GetUnitIndex( i ) ) ), color);
				m_pQueue[i]->RemoveButtonFlags( F_SHOWPROGRESS );
			}
		}
	}
	else if( pPlayer->GetSelectedBuilding()->IsRTSModel() )
	{
		C_BaseBuildingModel *m_pSelected = dynamic_cast<C_BaseBuildingModel *>(pPlayer->GetSelectedBuilding());
		if( !m_pSelected )
			return;

		if( m_pSelected->GetTypeQueueAbility( 0 ) != 0 ) {
			float weigth = ( m_pSelected->GetBuildTime() - gpGlobals->curtime ) / m_pSelected->GetBuildTimeMax();
			m_pQueue[0]->SetProgress( weigth );
		}

		for( int i = 0; i < QUEUE_MAX; i++) {
			m_pQueue[i]->SetAmount( m_pSelected->GetQueueCount(i) );
			if( i == 0 && m_pSelected->GetTypeQueueAbility( i ) != 0 && showprogress )
			{
				m_pQueue[i]->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, m_pUnitPanel->GetImage( g_RTSR->GetUnitType(  m_pSelected->GetTypeQueueAbility( i ) ) ), color);
				m_pQueue[i]->AddButtonFlags( F_SHOWPROGRESS );
			}
			else
			{
				m_pQueue[i]->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, m_pUnitPanel->GetImage( g_RTSR->GetUnitType(  m_pSelected->GetTypeQueueAbility( i ) ) ), color);
				m_pQueue[i]->RemoveButtonFlags( F_SHOWPROGRESS );
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Create button template
//-----------------------------------------------------------------------------
void CHudRTSBuildingPanel::CreateButtonQueue( CBitmapButtonRTS *pSlot, const char *command, int flags ) 
{
    pSlot->SetVisible( true );
	pSlot->SetPaintBackgroundEnabled( false );
	pSlot->SetPaintBorderEnabled( false );
	pSlot->SetParent(this);
	pSlot->AddActionSignalTarget(this);
	pSlot->SetCommand(command);
	pSlot->SetHighLightImages( "VGUI/button_hover", "VGUI/button_selected" );
	pSlot->AddButtonFlags(  flags );
}

//-----------------------------------------------------------------------------
// Purpose: Call this function after the size of this panel has been changed
//-----------------------------------------------------------------------------
void CHudRTSBuildingPanel::CalculatePosSize( void )
{
	int width, tall;
	GetSize( width, tall );
	int cyclesize;
	int buttonsize;

	cyclesize = (int)(tall / 2.0f);
	buttonsize = tall;
	m_pQueue[0]->SetSize( buttonsize, buttonsize );
	m_pQueue[0]->SetPos( cyclesize, 0 );
	m_pQueue[1]->SetSize( buttonsize, buttonsize );
	m_pQueue[1]->SetPos( cyclesize + buttonsize, 0 );
	m_pQueue[2]->SetSize( buttonsize, buttonsize );
	m_pQueue[2]->SetPos( cyclesize + buttonsize * 2, 0 );
	m_pQueue[3]->SetSize( buttonsize, buttonsize );
	m_pQueue[3]->SetPos( cyclesize + buttonsize * 3, 0 );
	m_pQueue[4]->SetSize( buttonsize, buttonsize );
	m_pQueue[4]->SetPos( cyclesize + buttonsize * 4, 0 );

	m_pCycle[0]->SetSize( cyclesize, cyclesize );
	m_pCycle[0]->SetPos( 0, 0 );
	m_pCycle[1]->SetSize( cyclesize, cyclesize );
	m_pCycle[1]->SetPos( 0, cyclesize );
}

//-----------------------------------------------------------------------------
// Purpose: Handles the button presses of the unit slots
//-----------------------------------------------------------------------------
void CHudRTSBuildingPanel::OnCommand(const char *command) 
{ 
	C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer ) 
		return;

	int *m_nNewRTSCommands2 = pPlayer->GetRTSCommands2();
	if (!stricmp(command, "queue_1")) 
	{ 
		*m_nNewRTSCommands2 |= IN_QUEUE_1;
	} 
	else if (!stricmp(command, "queue_2")) 
	{ 
		*m_nNewRTSCommands2 |= IN_QUEUE_2;
	}
	else if (!stricmp(command, "queue_3")) 
	{ 
		*m_nNewRTSCommands2 |= IN_QUEUE_3;
	}
	else if (!stricmp(command, "queue_4")) 
	{ 
		*m_nNewRTSCommands2 |= IN_QUEUE_4;
	}
	else if (!stricmp(command, "queue_5")) 
	{ 
		*m_nNewRTSCommands2 |= IN_QUEUE_5;
	}
	else if (!stricmp(command, "cycle_1")) 
	{ 
		*m_nNewRTSCommands2 |= IN_CYCLEQUEUE_1;
	}
	else if (!stricmp(command, "cycle_2")) 
	{ 
		*m_nNewRTSCommands2 |= IN_CYCLEQUEUE_2;
	}

	BaseClass::OnCommand(command);   
}


void CHudRTSBuildingPanel::SetUnitPanel( CHudRTSUnitPanel *pUnitPanel )
{
	m_pUnitPanel = pUnitPanel;
}

void CHudRTSBuildingPanel::OnMousePressed(MouseCode code)
{
	if( GetParent() )	// let our parent handle this
		GetParent()->OnMousePressed( code );
}

void CHudRTSBuildingPanel::OnMouseReleased(MouseCode code)
{
	if( GetParent() )	// let our parent handle this
		GetParent()->OnMouseReleased( code );
}

//-----------------------------------------------------------------------------
// Purpose: Shows the construction progress
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudRTSConstructionPanel::CHudRTSConstructionPanel( ) : BaseClass( NULL, "ConstructionPanel" )
{
	SetProportional( true );
	SetPaintBackgroundEnabled( false );
	SetKeyBoardInputEnabled( false );	
	SetMouseInputEnabled( true );	
	//SetScheme("ClientScheme.res");
	SetScheme( vgui::scheme()->LoadSchemeFromFile("resource/HL2Wars.res", "HL2Wars") );

	SetBounds( 0,0, 256, 256 );
	SetBgColor( Color( 0,0,0,255 ) );  
}

//-----------------------------------------------------------------------------
// Purpose: Deconstructor
//-----------------------------------------------------------------------------
CHudRTSConstructionPanel::~CHudRTSConstructionPanel()
{

}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudRTSConstructionPanel::Init( void )
{
	SetCursor( GetParent()->GetCursor() );

	m_pConstruction = vgui::SETUP_PANEL(new vgui::Label(this,"Progress","Blablabla"));		
	m_pConstruction->SetScheme(vgui::scheme()->LoadSchemeFromFile("resource/ClientScheme.res", "ClientScheme"));	//Set it's scheme...
	m_pConstruction->SetBgColor(Color(0,0,0,0));	//Translucent BG
	m_pConstruction->SetPaintBackgroundEnabled( false );
	m_pConstruction->SetPaintBorderEnabled( false );	
	m_pConstruction->SetContentAlignment( vgui::Label::a_west );
	m_pConstruction->SetCursor( GetParent()->GetCursor() );
	m_pConstruction->SetVisible( true );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudRTSConstructionPanel::ApplySchemeSettings( IScheme *scheme )
{
	BaseClass::ApplySchemeSettings( scheme );

	// set fonts and colors or labels
	HFont font = scheme->GetFont("InterfaceInfoFont", IsProportional() );
	m_pConstruction->SetFont( font );

	m_pConstruction->SetFgColor(GetSchemeColor("hudinterface.TextColor", scheme));
}

//-----------------------------------------------------------------------------
// Purpose: Paint
//-----------------------------------------------------------------------------
void CHudRTSConstructionPanel::OnThink( void )
{
	C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer ) 
		return;

	if( !pPlayer->GetSelectedBuilding() || !pPlayer->GetSelectedBuilding()->IsRTSModel() )
		return;

	C_BaseBuildingModel *m_pSelected = dynamic_cast<C_BaseBuildingModel *>(pPlayer->GetSelectedBuilding());
	if( !m_pSelected )
		return;

	char progress[MAX_PATH];
	Q_snprintf( progress, sizeof( progress ), "Construction progress: %d%%\n", m_pSelected->GetConstructionProgress() );
	m_pConstruction->SetText( progress );
}

//-----------------------------------------------------------------------------
// Purpose: Call this function after the size of this panel has been changed
//-----------------------------------------------------------------------------
void CHudRTSConstructionPanel::CalculatePosSize( void )
{
	int width, tall;
	GetSize( width, tall );

	m_pConstruction->SetPos( 0.20 * width, 0.20 * tall );
	m_pConstruction->SetSize( width, vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 20 )  );
	
}


void CHudRTSConstructionPanel::OnMousePressed(MouseCode code)
{
	if( GetParent() )	// let our parent handle this
		GetParent()->OnMousePressed( code );
}

void CHudRTSConstructionPanel::OnMouseReleased(MouseCode code)
{
	if( GetParent() )	// let our parent handle this
		GetParent()->OnMouseReleased( code );
}