class ApplyShaderEffector extends Effector
{
	private var m_overrideMaterialName : CName;
	private var m_overrideMaterialTag : CName;
	private var m_applyToOwner : Bool;
	private var m_applyToWeapon : Bool;
	private var m_owner : weak< GameObject >;
	private var m_ownerWeapons : array< weak< ItemObject > >;
	private var m_isEnabled : Bool;
	default m_isEnabled = false;

	protected override function Initialize( record : TweakDBID, game : GameInstance, parentRecord : TweakDBID )
	{
		m_overrideMaterialName = TweakDBInterface.GetCName( record + T".overrideMaterialName", '' );
		m_overrideMaterialTag = TweakDBInterface.GetCName( record + T".overrideMaterialTag", '' );
		m_applyToOwner = TweakDBInterface.GetBool( record + T".applyToOwner", false );
		m_applyToWeapon = TweakDBInterface.GetBool( record + T".applyToWeapon", false );
	}

	protected override function ActionOn( owner : GameObject )
	{
		var effectInstance : EffectInstance;
		var effectInstanceWeapon : EffectInstance;
		var i : Int32;
		if( m_applyToOwner )
		{
			m_owner = owner;
		}
		if( m_applyToWeapon )
		{
			AIActionHelper.GetItemsFromWeaponSlots( owner, m_ownerWeapons );
		}
		if( !( m_isEnabled ) && IsNameValid( m_overrideMaterialName ) )
		{
			effectInstance = GameInstance.GetGameEffectSystem( m_owner.GetGame() ).CreateEffectStatic( m_overrideMaterialName, m_overrideMaterialTag, m_owner );
			effectInstanceWeapon = GameInstance.GetGameEffectSystem( m_owner.GetGame() ).CreateEffectStatic( m_overrideMaterialName, m_overrideMaterialTag, m_owner );
			if( m_applyToOwner && effectInstance )
			{
				EffectData.SetBool( effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.enable, true );
				EffectData.SetEntity( effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, m_owner );
				effectInstance.Run();
			}
			if( m_applyToWeapon && effectInstanceWeapon )
			{
				EffectData.SetBool( effectInstanceWeapon.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.enable, true );
				for( i = 0; i < m_ownerWeapons.Size(); i += 1 )
				{
					EffectData.SetEntity( effectInstanceWeapon.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, m_ownerWeapons[ i ] );
				}
				effectInstanceWeapon.Run();
			}
			m_isEnabled = true;
		}
	}

	protected override function Uninitialize( game : GameInstance )
	{
		var effectInstance : EffectInstance;
		var effectInstanceWeapon : EffectInstance;
		var i : Int32;
		if( m_isEnabled && IsNameValid( m_overrideMaterialName ) )
		{
			effectInstance = GameInstance.GetGameEffectSystem( game ).CreateEffectStatic( m_overrideMaterialName, m_overrideMaterialTag, m_owner );
			effectInstanceWeapon = GameInstance.GetGameEffectSystem( m_owner.GetGame() ).CreateEffectStatic( m_overrideMaterialName, m_overrideMaterialTag, m_owner );
			if( effectInstance )
			{
				EffectData.SetBool( effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.enable, false );
				EffectData.SetEntity( effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, m_owner );
				EffectData.SetEntity( effectInstanceWeapon.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, m_owner );
				effectInstance.Run();
				m_isEnabled = false;
			}
			if( effectInstanceWeapon )
			{
				EffectData.SetBool( effectInstanceWeapon.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.enable, false );
				EffectData.SetEntity( effectInstanceWeapon.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, m_owner );
				for( i = 0; i < m_ownerWeapons.Size(); i += 1 )
				{
					EffectData.SetEntity( effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, m_ownerWeapons[ i ] );
				}
				effectInstanceWeapon.Run();
				m_isEnabled = false;
			}
		}
	}

}

