class ApplyStatusEffectByChanceEffector extends Effector
{
	var m_targetEntityID : EntityID;
	var m_applicationTarget : CName;
	var m_record : TweakDBID;
	var m_removeWithEffector : Bool;
	var m_effectorChanceMods : array< weak< StatModifier_Record > >;

	protected override function Initialize( record : TweakDBID, game : GameInstance, parentRecord : TweakDBID )
	{
		m_record = TweakDBInterface.GetApplyStatusEffectEffectorRecord( record ).StatusEffect().GetID();
		m_applicationTarget = TweakDBInterface.GetCName( record + T".applicationTarget", '' );
		m_removeWithEffector = TweakDBInterface.GetBool( record + T".removeWithEffector", true );
		TweakDBInterface.GetApplyStatusEffectByChanceEffectorRecord( record ).EffectorChance( m_effectorChanceMods );
	}

	protected override function Uninitialize( game : GameInstance )
	{
		if( m_removeWithEffector )
		{
			RemoveStatusEffect( game );
		}
	}

	protected override function RepeatedAction( owner : GameObject )
	{
		var rand : Float;
		var chance : Float;
		rand = RandF();
		chance = RPGManager.CalculateStatModifiers( m_effectorChanceMods, owner.GetGame(), owner, owner.GetEntityID() );
		if( rand <= chance )
		{
			if( !( GetApplicationTarget( owner, m_applicationTarget, m_targetEntityID ) ) )
			{
				return;
			}
			ApplyStatusEffect( owner.GetGame() );
		}
	}

	protected override function ActionOff( owner : GameObject )
	{
		if( m_removeWithEffector )
		{
			RemoveStatusEffect( owner.GetGame() );
		}
	}

	private function ApplyStatusEffect( gameInstance : GameInstance )
	{
		var ses : StatusEffectSystem;
		if( !( EntityID.IsDefined( m_targetEntityID ) ) || !( TDBID.IsValid( m_record ) ) )
		{
			return;
		}
		ses = GameInstance.GetStatusEffectSystem( gameInstance );
		ses.ApplyStatusEffect( m_targetEntityID, m_record );
	}

	private function RemoveStatusEffect( gameInstance : GameInstance )
	{
		var ses : StatusEffectSystem;
		if( !( EntityID.IsDefined( m_targetEntityID ) ) || !( TDBID.IsValid( m_record ) ) )
		{
			return;
		}
		ses = GameInstance.GetStatusEffectSystem( gameInstance );
		ses.RemoveStatusEffect( m_targetEntityID, m_record );
	}

}

