class ModifyStatPoolValueQuickhackCostEffector extends HitEventEffector
{
	var m_statPoolValue : Float;
	var m_statPoolType : gamedataStatPoolType;
	var m_recoverMemoryAmount : Float;
	var m_skipLastCombatHack : Bool;

	protected export override function Initialize( record : TweakDBID, game : GameInstance, parentRecord : TweakDBID )
	{
		m_statPoolValue = TDB.GetFloat( record + T".statPoolValue" );
		m_statPoolType = ( ( gamedataStatPoolType )( ( ( Int32 )( EnumValueFromString( "gamedataStatPoolType", TweakDBInterface.GetString( record + T".statPoolType", "" ) ) ) ) ) );
		m_recoverMemoryAmount = TDB.GetFloat( record + T".recoverMemoryAmount", 1.0 );
		m_skipLastCombatHack = TDB.GetBool( record + T".skipLastCombatHack", false );
	}

	protected export override function ActionOn( owner : GameObject )
	{
		ProcessEffector( owner );
	}

	protected export override function RepeatedAction( owner : GameObject )
	{
		ProcessEffector( owner );
	}

	private function ProcessEffector( owner : GameObject )
	{
		var poolSys : StatPoolsSystem;
		var statPoolValue : Float;
		var hitEvent : gameHitEvent;
		var appliedEffects : array< StatusEffect >;
		var i, j, k : Int32;
		var activeQHHistory : array< ScriptableDeviceAction >;
		var activeQH : ScriptableDeviceAction;
		var activeQHCost : Int32;
		var actionEffectsCost : array< Int32 >;
		var activeActionEffectTweakDBID : TweakDBID;
		var appliedEffectTweakDBID : TweakDBID;
		var actionRecord : ObjectAction_Record;
		var actionEffects : array< weak< ObjectActionEffect_Record > >;
		var actionEffectsOfEffects : array< array< weak< ObjectActionEffect_Record > > >;
		var foundAppliedEffect : Bool;
		statPoolValue = m_recoverMemoryAmount;
		hitEvent = GetHitEvent();
		if( !( hitEvent ) )
		{
			return;
		}
		poolSys = GameInstance.GetStatPoolsSystem( owner.GetGame() );
		if( !( poolSys ) )
		{
			return;
		}
		GameInstance.GetStatusEffectSystem( owner.GetGame() ).GetAppliedEffectsWithTag( hitEvent.target.GetEntityID(), 'Quickhack', appliedEffects );
		activeQHHistory = GetActiveQuickhackActionHistory( hitEvent.target );
		foundAppliedEffect = false;
		for( i = activeQHHistory.Size() - 1; i >= 0; i -= 1 )
		{
			activeQH = activeQHHistory[ i ];
			activeQHCost = activeQH.GetCost();
			actionRecord = activeQH.GetObjectActionRecord();
			if( actionRecord )
			{
				if( ( m_skipLastCombatHack && !( foundAppliedEffect ) ) && actionRecord.HackCategory().Type() == gamedataHackCategory.DamageHack )
				{
					foundAppliedEffect = true;
					continue;
				}
				actionRecord.CompletionEffects( actionEffects );
				actionEffectsOfEffects.PushBack( actionEffects );
				actionEffectsCost.PushBack( activeQHCost );
				actionEffects.Clear();
			}
		}
		for( i = 0; i < actionEffectsOfEffects.Size(); i += 1 )
		{
			actionEffects = actionEffectsOfEffects[ i ];
			activeQHCost = actionEffectsCost[ i ];
			foundAppliedEffect = false;
			for( j = 0; j < actionEffects.Size(); j += 1 )
			{
				activeActionEffectTweakDBID = actionEffects[ j ].StatusEffect().GetID();
				if( actionEffects[ j ].EffectorToTrigger() && actionEffects[ j ].EffectorToTrigger().EffectorClassName() == 'ApplyLegendaryWhistleEffector' )
				{
					activeActionEffectTweakDBID = T"BaseStatusEffect.WhistleLvl4";
				}
				for( k = 0; k < appliedEffects.Size(); k += 1 )
				{
					appliedEffectTweakDBID = appliedEffects[ k ].GetRecord().GetID();
					if( activeActionEffectTweakDBID == appliedEffectTweakDBID )
					{
						statPoolValue += ( m_statPoolValue * ( ( Float )( activeQHCost ) ) );
						foundAppliedEffect = true;
						break;
					}
				}
				activeActionEffectTweakDBID = TDBID.None();
				if( foundAppliedEffect )
				{
					break;
				}
			}
		}
		poolSys.RequestChangingStatPoolValue( owner.GetEntityID(), m_statPoolType, statPoolValue, owner, false, false );
	}

	private function GetActiveQuickhackActionHistory( target : weak< GameObject > ) : array< ScriptableDeviceAction >
	{
		var scriptableDeviceActionArray : array< ScriptableDeviceAction >;
		if( ( ( ScriptedPuppet )( target ) ) )
		{
			scriptableDeviceActionArray = ( ( ScriptedPuppet )( target ) ).GetActiveQuickhackActionHistory();
		}
		return scriptableDeviceActionArray;
	}

}

