class ChargedItemsPrereqListener extends BaseStatPoolPrereqListener
{
	protected var m_state : weak< ChargedItemsPrereqState >;

	public export override function OnStatPoolValueChanged( oldValue : Float, newValue : Float, percToPoints : Float )
	{
		var chargesStatpool : Float;
		var currentCharges : Float;
		var rechargeDuration : Float;
		var maxChargeValue : Float;
		var chargesState : EChargesAmount;
		chargesState = m_state.GetChargesState();
		chargesStatpool = ( ( Float )( RoundMath( newValue * percToPoints ) ) );
		switch( m_state.getTypeOfItem() )
		{
			case EChargesItem.HealingItems:
				rechargeDuration = ( ( Float )( GetPlayer( m_state.m_owner ).GetHealingItemUseCost() ) );
			maxChargeValue = GameInstance.GetStatsSystem( m_state.m_owner ).GetStatValue( GetPlayer( m_state.m_owner ).GetEntityID(), gamedataStatType.HealingItemMaxCharges );
			break;
			case EChargesItem.Grenade:
				rechargeDuration = ( ( Float )( GetPlayer( m_state.m_owner ).GetGrenadeThrowCostClean() ) );
			maxChargeValue = GameInstance.GetStatsSystem( m_state.m_owner ).GetStatValue( GetPlayer( m_state.m_owner ).GetEntityID(), gamedataStatType.GrenadesMaxCharges );
			break;
			case EChargesItem.ProjectileLauncher:
				rechargeDuration = ( ( Float )( GetPlayer( m_state.m_owner ).GetProjectileLauncherShootCost() ) );
			maxChargeValue = GameInstance.GetStatsSystem( m_state.m_owner ).GetStatValue( GetPlayer( m_state.m_owner ).GetEntityID(), gamedataStatType.ProjectileLauncherMaxCharges );
			break;
		}
		currentCharges = chargesStatpool / rechargeDuration;
		if( chargesState == EChargesAmount.Empty )
		{
			m_state.OnChanged( currentCharges < 1.0 );
		}
		else if( chargesState == EChargesAmount.Last )
		{
			m_state.OnChanged( ( currentCharges >= 1.0 ) && ( currentCharges < 2.0 ) );
		}
		else if( chargesState == EChargesAmount.FirstFromTop )
		{
			m_state.OnChanged( ( ( maxChargeValue - 1.0 ) < currentCharges ) && ( currentCharges <= maxChargeValue ) );
		}
		else if( chargesState == EChargesAmount.Max )
		{
			m_state.OnChanged( currentCharges == maxChargeValue );
		}
	}

	public override function RegisterState( state : PrereqState )
	{
		m_state = ( ( ChargedItemsPrereqState )( state ) );
	}

}

class ChargedItemsPrereqState extends PrereqState
{
	var m_chargesState : EChargesAmount;
	var m_typeOfItem : EChargesItem;
	var m_listener : BaseStatPoolPrereqListener;
	var m_owner : GameInstance;

	public function SetChargesState( value : EChargesAmount )
	{
		m_chargesState = value;
	}

	public function GetChargesState() : EChargesAmount
	{
		return m_chargesState;
	}

	public function SetTypeOfItem( value : EChargesItem )
	{
		m_typeOfItem = value;
	}

	public function getTypeOfItem() : EChargesItem
	{
		return m_typeOfItem;
	}

}

class ChargedItemsPrereq extends IScriptablePrereq
{
	var m_chargesToCheck : EChargesAmount;
	var m_typeOfItem : EChargesItem;

	protected export const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var owner : GameObject;
		var castedState : ChargedItemsPrereqState;
		owner = ( ( GameObject )( context ) );
		castedState = ( ( ChargedItemsPrereqState )( state ) );
		castedState.SetChargesState( m_chargesToCheck );
		castedState.SetTypeOfItem( m_typeOfItem );
		castedState.m_listener = new ChargedItemsPrereqListener;
		castedState.m_listener.RegisterState( castedState );
		castedState.m_owner = ( ( GameInstance )( owner.GetGame() ) );
		switch( m_typeOfItem )
		{
			case EChargesItem.HealingItems:
				GameInstance.GetStatPoolsSystem( game ).RequestRegisteringListener( owner.GetEntityID(), gamedataStatPoolType.HealingItemsCharges, castedState.m_listener );
			break;
			case EChargesItem.Grenade:
				GameInstance.GetStatPoolsSystem( game ).RequestRegisteringListener( owner.GetEntityID(), gamedataStatPoolType.GrenadesCharges, castedState.m_listener );
			break;
			case EChargesItem.ProjectileLauncher:
				GameInstance.GetStatPoolsSystem( game ).RequestRegisteringListener( owner.GetEntityID(), gamedataStatPoolType.ProjectileLauncherCharges, castedState.m_listener );
			break;
		}
		return true;
	}

	protected export const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var owner : GameObject;
		var castedState : ChargedItemsPrereqState;
		owner = ( ( GameObject )( context ) );
		castedState = ( ( ChargedItemsPrereqState )( state ) );
		switch( m_typeOfItem )
		{
			case EChargesItem.HealingItems:
				GameInstance.GetStatPoolsSystem( game ).RequestUnregisteringListener( owner.GetEntityID(), gamedataStatPoolType.HealingItemsCharges, castedState.m_listener );
			break;
			case EChargesItem.Grenade:
				GameInstance.GetStatPoolsSystem( game ).RequestUnregisteringListener( owner.GetEntityID(), gamedataStatPoolType.GrenadesCharges, castedState.m_listener );
			break;
			case EChargesItem.ProjectileLauncher:
				GameInstance.GetStatPoolsSystem( game ).RequestUnregisteringListener( owner.GetEntityID(), gamedataStatPoolType.ProjectileLauncherCharges, castedState.m_listener );
			break;
		}
		castedState.m_listener = NULL;
	}

	protected export override function Initialize( recordID : TweakDBID )
	{
		var str : String;
		str = TweakDBInterface.GetString( recordID + T".howManyCharges", "" );
		m_chargesToCheck = ( ( EChargesAmount )( ( ( Int32 )( EnumValueFromString( "EChargesAmount", str ) ) ) ) );
		str = TweakDBInterface.GetString( recordID + T".typeOfItem", "" );
		m_typeOfItem = ( ( EChargesItem )( ( ( Int32 )( EnumValueFromString( "EChargesItem", str ) ) ) ) );
	}

}

enum EChargesAmount
{
	Empty = 0,
	Last = 1,
	FirstFromTop = 2,
	Max = 3,
}

enum EChargesItem
{
	Grenade = 0,
	HealingItems = 1,
	ProjectileLauncher = 2,
}

