class ModifyDamageEffectorStatListener extends ScriptStatsListener
{
	var m_effector : weak< ModifyDamageEffector >;

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

}

class ModifyDamageEffector extends ModifyAttackEffector
{
	protected var m_operationType : EMathOperator;
	protected var m_value : Float;
	protected var m_statType : gamedataStatType;
	default m_statType = gamedataStatType.Invalid;
	private var m_ownerID : EntityID;
	private var m_statListener : ModifyDamageEffectorStatListener;
	var m_statBasedValue : Float;

	protected export override function Initialize( record : TweakDBID, game : GameInstance, parentRecord : TweakDBID )
	{
		var str : String;
		var statRecord : Stat_Record;
		str = TDB.GetString( record + T".operationType" );
		m_operationType = ( ( EMathOperator )( ( ( Int32 )( EnumValueFromString( "EMathOperator", str ) ) ) ) );
		m_value = TDB.GetFloat( record + T".value" );
		statRecord = TDB.GetStatRecord( TDB.GetForeignKey( record + T".statForValue" ) );
		if( statRecord )
		{
			m_statType = statRecord.StatType();
			InitializeStatListener( game );
		}
	}

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

	private function InitializeStatListener( game : GameInstance )
	{
		var owner : GameObject;
		var statsSystem : StatsSystem;
		if( m_statType == gamedataStatType.Invalid )
		{
			return;
		}
		if( m_statListener )
		{
			return;
		}
		owner = GetOwner();
		if( !( owner ) )
		{
			return;
		}
		m_ownerID = owner.GetEntityID();
		m_statListener = new ModifyDamageEffectorStatListener;
		m_statListener.m_effector = this;
		m_statListener.SetStatType( m_statType );
		statsSystem = GameInstance.GetStatsSystem( game );
		statsSystem.RegisterListener( m_ownerID, m_statListener );
		m_statBasedValue = 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 export override function RepeatedAction( owner : GameObject )
	{
		var hitEvent : gameHitEvent;
		var valueToUse : Float;
		hitEvent = GetHitEvent();
		if( !( hitEvent ) )
		{
			return;
		}
		if( m_statType != gamedataStatType.Invalid )
		{
			valueToUse = m_statBasedValue;
		}
		else
		{
			valueToUse = m_value;
		}
		ModifyDamage( hitEvent, m_operationType, valueToUse );
	}

	protected function ModifyDamage( hitEvent : gameHitEvent, operationType : EMathOperator, value : Float )
	{
		switch( operationType )
		{
			case EMathOperator.Add:
				hitEvent.attackComputed.AddAttackValue( value );
			break;
			case EMathOperator.Subtract:
				hitEvent.attackComputed.AddAttackValue( -( value ) );
			break;
			case EMathOperator.Multiply:
				hitEvent.attackComputed.MultAttackValue( value );
			break;
			case EMathOperator.Divide:
				hitEvent.attackComputed.MultAttackValue( 1.0 / value );
			break;
			default:
				return;
		}
	}

}

