/* ***** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/LGPL 2.1/GPL 2.0

The contents of this file are subject to the Mozilla Public License Version 
1.1 (the "License"); you may not use this file except in compliance with
...
for the specific language governing rights and limitations under the
License.

The Original Code is for Luminous Forts.

The Initial Developer of the Original Code is Hekar Khani.
Portions created by the Hekar Khani are Copyright (C) 2010
Hekar Khani. All Rights Reserved.

Contributor(s):
Hekar Khani <hekark@gmail.com>

Alternatively, the contents of this file may be used under the terms of
either of the GNU General Public License Version 2 or later (the "GPL"),
...
the terms of any one of the MPL, the GPL or the LGPL.

***** END LICENSE BLOCK ***** */

#include "cbase.h"
#include "Dom_CaptureZone.h"

BEGIN_DATADESC( CDomCaptureZone )
	DEFINE_KEYFIELD( m_iSecondsToCapture, FIELD_INTEGER, "SecondsToCapture" ),
	DEFINE_KEYFIELD( m_bDisableInBuild, FIELD_INTEGER, "DisableInBuild" ),
	DEFINE_KEYFIELD( m_ParentZone1, FIELD_INTEGER, "ParentZone1" ),
	DEFINE_KEYFIELD( m_ParentZone2, FIELD_INTEGER, "ParentZone2" ),
	DEFINE_KEYFIELD( m_ParentZone3, FIELD_INTEGER, "ParentZone3" ),
	
	DEFINE_INPUTFUNC( FIELD_VOID, "DisableCaptures", InputDisableCaptures ),
	DEFINE_INPUTFUNC( FIELD_VOID, "EnableCaptures", InputEnableCaptures ),

	DEFINE_OUTPUT( m_Capture, "OnCaptured" ),
	DEFINE_OUTPUT( m_CapturedBy, "OnCapturedBy" ),
	DEFINE_OUTPUT( m_Lost, "OnLost" ),
	
	DEFINE_THINKFUNC( Think ),
END_DATADESC()


IMPLEMENT_SERVERCLASS_ST( CDomCaptureZone, DT_DomCaptureZone )
	SendPropInt(SENDINFO(m_CapState), 8, SPROP_UNSIGNED),
	SendPropInt(SENDINFO(m_iCapturePercentage), 8, SPROP_UNSIGNED),
	SendPropInt(SENDINFO(m_iCapturingCount), 8, SPROP_UNSIGNED),
	SendPropInt(SENDINFO(m_iCapturingTeam), 8, SPROP_UNSIGNED),
END_SEND_TABLE()

CDomCaptureZone::CDomCaptureZone()
{
	m_bDisabled = false;
	m_bDisableInBuild = true;
	m_CapState = CAP_NONE;
	m_iCapturingCount = 0;
	m_iCapturingTeam = TEAM_SPECTATOR;

	m_TouchingPlayers = new CUtlVector< CBasePlayer * >(0, 15);
	for ( int i = 0; i < m_TouchingPlayers->Size(); i++ )
	{
		m_TouchingPlayers->Element( i ) = NULL;
	}
}

CDomCaptureZone::~CDomCaptureZone()
{
	delete m_TouchingPlayers;
}

void CDomCaptureZone::Spawn()
{
	BaseClass::Spawn();

	SetNextThink( gpGlobals->curtime + 0.1f );
}

void CDomCaptureZone::OnPhase( int phase )
{
	if ( phase == PHASE_BUILD )
	{
		if ( m_bDisableInBuild )
		{
			m_bDisabled = true;
		}
	}
	else if ( phase == PHASE_COMBAT )
	{
		m_bDisabled = false;
	}
}

void CDomCaptureZone::StartTouch( CBaseEntity *pOther )
{
	if ( m_bDisabled || m_bDisableCaptures )
		return;

	if ( pOther->IsPlayer() )
	{
		CBasePlayer *pPlayer = ToBasePlayer( pOther );
		if ( !pPlayer )
		{
			return;
		}

		m_TouchingPlayers->AddToTail( pPlayer );
	}
}

void CDomCaptureZone::EndTouch( CBaseEntity *pOther )
{
	if ( pOther->IsPlayer() )
	{
		CBasePlayer *pPlayer = ToBasePlayer( pOther );
		if ( !pPlayer )
		{
			return;
		}

		for ( int i = 0; i < m_TouchingPlayers->Size(); i++ )
		{
			if ( m_TouchingPlayers->Element( i ) == pPlayer )
			{
				m_TouchingPlayers->Remove( i );
			}
		}
	}
}

void CDomCaptureZone::Think()
{
	CaptureThink();
	SetNextThink( gpGlobals->curtime + 0.1f );
}

void CDomCaptureZone::CaptureThink()
{
	if ( m_TouchingPlayers->Size() < 0 )
	{
		int redTouching = 0;
		int blueTouching = 0;
		for ( int i = 0; i < m_TouchingPlayers->Size(); i++ )
		{
			CBasePlayer *pPlayer = ToBasePlayer( m_TouchingPlayers->Element( i ) );
			if ( !pPlayer )
				continue;

			int team = pPlayer->GetTeamNumber();
			if ( team == TEAM_RED )
			{
				redTouching++;
			}
			else if ( team == TEAM_BLUE )
			{
				blueTouching++;
			}
		}

		bool bothTeamsTouching = false;
		if ( ( redTouching > 0 ) && ( blueTouching > 0 ) )
		{
			bothTeamsTouching = true;
		}

		if ( !bothTeamsTouching )
		{
			if ( redTouching > 0 )
			{
				if ( GetTeamNumber() == TEAM_RED )
				{
					m_CapState = CAP_OWNED_RED;
				}
				else
				{
					m_iCapturingTeam = TEAM_RED;
					m_iCapturingCount = redTouching;
					m_CapState = CAP_CAPTURE_RED;
					if ( !m_CaptureTimer.HasStarted() )
					{
						//m_CaptureTimer.Start();
					}
				}
			}
			else if ( blueTouching > 0 )
			{
				if ( GetTeamNumber() == TEAM_BLUE )
				{
					m_CapState = CAP_OWNED_BLUE;
				}
				else
				{
					m_iCapturingTeam = TEAM_BLUE;
					m_iCapturingCount = blueTouching;
					m_CapState = CAP_CAPTURE_BLUE;
					if ( !m_CaptureTimer.HasStarted() )
					{
						//m_CaptureTimer.Start();
					}
				}
			}
		}
		else
		{
			if ( m_iSecondsToCapture < m_CaptureTimer.GetElapsedTime() )
			{
				// Calculate the percentage
			}

			// Stop the timer if both teams are on the point
			m_CaptureTimer.Reset();

			if ( GetTeamNumber() == TEAM_RED )
			{
				m_iCapturingCount = blueTouching;
				m_CapState = CAP_STALLED_BLUE;
			}
			else if ( GetTeamNumber() == TEAM_BLUE )
			{
				m_iCapturingCount = redTouching;
				m_CapState = CAP_STALLED_RED;
			}	
		}
	}
	else
	{
		//HODO: Check the timer
		if ( m_iCapturingTeam == TEAM_RED )
		{
			m_CapState = CAP_DRAIN_RED;
		}
		else if ( m_iCapturingTeam == TEAM_BLUE )
		{
			m_CapState = CAP_DRAIN_BLUE;
		}
	}
}

void CDomCaptureZone::InputEnableCaptures( inputdata_t& inputdata )
{
	m_bDisableCaptures = false;
}

void CDomCaptureZone::InputDisableCaptures( inputdata_t& inputdata )
{
	m_bDisableCaptures = true;
}

void CDomCaptureZone::OnBuild()
{

}

void CDomCaptureZone::OnCombat()
{

}
