class TriggerAttackByChanceStatListener extends ScriptStatsListener
{
	var m_effector : weak< TriggerAttackByChanceEffector >;

	public override function OnStatChanged( ownerID : StatsObjectID, statType : gamedataStatType, diff : Float, total : Float )
	{
		m_effector.m_statBasedChance = total;
	}

}

class TriggerAttackByChanceEffector extends Effector
{
	protected var m_attackTDBID : TweakDBID;
	protected var m_selfStatusEffectID : TweakDBID;
	protected var m_chance : Float;
	protected var m_statType : gamedataStatType;
	default m_statType = gamedataStatType.Invalid;
	private var m_ownerID : EntityID;
	private var m_statListener : TriggerAttackByChanceStatListener;
	var m_statBasedChance : Float;

	protected override function Initialize( record : TweakDBID, game : GameInstance, parentRecord : TweakDBID )
	{
		var effectorRecord : TriggerAttackEffector_Record;
		var statRecord : Stat_Record;
		effectorRecord = TweakDBInterface.GetTriggerAttackEffectorRecord( record );
		m_attackTDBID = effectorRecord.AttackRecord().GetID();
		m_chance = TDB.GetFloat( record + T".chance" );
		statRecord = TDB.GetStatRecord( TDB.GetForeignKey( record + T".statForChance" ) );
		m_selfStatusEffectID = TDB.GetForeignKey( record + T".selfStatusEffect" );
		if( statRecord )
		{
			m_statType = statRecord.StatType();
		}
	}

	protected override function Uninitialize( game : GameInstance )
	{
		UninitializeStatListener( game );
	}

	private function InitializeStatListener( owner : GameObject )
	{
		var statsSystem : StatsSystem;
		if( !( m_statListener ) )
		{
			m_ownerID = owner.GetEntityID();
			m_statListener = new TriggerAttackByChanceStatListener;
			m_statListener.m_effector = this;
			m_statListener.SetStatType( m_statType );
			statsSystem = GameInstance.GetStatsSystem( owner.GetGame() );
			statsSystem.RegisterListener( m_ownerID, m_statListener );
			m_statBasedChance = statsSystem.GetStatValue( m_ownerID, m_statType );
		}
	}

	private function UninitializeStatListener( game : GameInstance )
	{
		if( m_statListener )
		{
			GameInstance.GetStatsSystem( game ).UnregisterListener( m_ownerID, m_statListener );
			m_statListener = NULL;
		}
	}

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

	protected override function ActionOn( owner : GameObject )
	{
		var rand, chanceToCheck : Float;
		var i : Int32;
		var attack : Attack_GameEffect;
		var tempArr : array< String >;
		var flag : SHitFlag;
		var hitFlags : array< SHitFlag >;
		if( m_statType != gamedataStatType.Invalid )
		{
			InitializeStatListener( owner );
			chanceToCheck = m_statBasedChance;
		}
		else
		{
			chanceToCheck = m_chance;
		}
		rand = RandF();
		if( chanceToCheck >= rand )
		{
			tempArr = TweakDBInterface.GetAttackRecord( m_attackTDBID ).HitFlags();
			for( i = 0; i < tempArr.Size(); i += 1 )
			{
				flag.flag = ( ( hitFlag )( ( ( Int32 )( EnumValueFromString( "hitFlag", tempArr[ i ] ) ) ) ) );
				flag.source = 'Attack';
				hitFlags.PushBack( flag );
			}
			attack = RPGManager.PrepareGameEffectAttack( owner.GetGame(), owner, owner, m_attackTDBID, , hitFlags, owner );
			attack.StartAttack();
			if( TDBID.IsValid( m_selfStatusEffectID ) )
			{
				GameInstance.GetStatusEffectSystem( owner.GetGame() ).ApplyStatusEffect( owner.GetEntityID(), m_selfStatusEffectID );
			}
		}
	}

}

