class ApplyShaderOnEquipmentEffector extends Effector
{
	private var m_overrideMaterialName : CName;
	private var m_overrideMaterialTag : CName;
	private var m_effectInstance : EffectInstance;
	private var m_owner : weak< GameObject >;
	private var m_ownerEffect : EffectInstance;

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

	protected override function ActionOn( owner : GameObject )
	{
		var i : Int32;
		var ts : TransactionSystem;
		var slots : array< TweakDBID >;
		var item : weak< ItemObject >;
		var evt : SetUpEquipmentOverlayEvent;
		m_owner = owner;
		ts = GameInstance.GetTransactionSystem( owner.GetGame() );
		slots = GetAttachmentSlotsForEquipment();
		if( ( ( PlayerPuppet )( m_owner ) ) )
		{
			evt = new SetUpEquipmentOverlayEvent;
			evt.meshOverlayEffectName = m_overrideMaterialName;
			evt.meshOverlayEffectTag = m_overrideMaterialTag;
			evt.meshOverlaySlots = slots;
			m_owner.QueueEvent( evt );
		}
		for( i = 0; i < slots.Size(); i += 1 )
		{
			item = ts.GetItemInSlot( owner, slots[ i ] );
			if( item )
			{
				m_effectInstance = GameInstance.GetGameEffectSystem( m_owner.GetGame() ).CreateEffectStatic( m_overrideMaterialName, m_overrideMaterialTag, m_owner );
				if( m_effectInstance && IsNameValid( m_overrideMaterialName ) )
				{
					EffectData.SetBool( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.enable, true );
					EffectData.SetBool( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.clearMaterialOverlayOnDetach, true );
					EffectData.SetEntity( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, item );
					m_effectInstance.Run();
				}
			}
		}
		m_effectInstance = GameInstance.GetGameEffectSystem( m_owner.GetGame() ).CreateEffectStatic( m_overrideMaterialName, m_overrideMaterialTag, m_owner );
		if( m_effectInstance && IsNameValid( m_overrideMaterialName ) )
		{
			EffectData.SetBool( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.enable, true );
			EffectData.SetBool( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.clearMaterialOverlayOnDetach, true );
			EffectData.SetEntity( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, m_owner );
			m_effectInstance.Run();
		}
	}

	protected override function Uninitialize( game : GameInstance )
	{
		var i : Int32;
		var ts : TransactionSystem;
		var slots : array< TweakDBID >;
		var item : weak< ItemObject >;
		var evt : SetUpEquipmentOverlayEvent;
		if( ( ( PlayerPuppet )( m_owner ) ) )
		{
			evt = new SetUpEquipmentOverlayEvent;
			evt.meshOverlayEffectName = '';
			evt.meshOverlayEffectTag = '';
			evt.meshOverlaySlots = slots;
			m_owner.QueueEvent( evt );
		}
		ts = GameInstance.GetTransactionSystem( game );
		slots = GetAttachmentSlotsForEquipment();
		for( i = 0; i < slots.Size(); i += 1 )
		{
			item = ts.GetItemInSlot( m_owner, slots[ i ] );
			if( item )
			{
				m_effectInstance = GameInstance.GetGameEffectSystem( game ).CreateEffectStatic( m_overrideMaterialName, m_overrideMaterialTag, m_owner );
				if( m_effectInstance && IsNameValid( m_overrideMaterialName ) )
				{
					EffectData.SetBool( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.enable, false );
					EffectData.SetEntity( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, item );
					m_effectInstance.Run();
				}
			}
		}
		m_effectInstance = GameInstance.GetGameEffectSystem( game ).CreateEffectStatic( m_overrideMaterialName, m_overrideMaterialTag, m_owner );
		if( m_effectInstance && IsNameValid( m_overrideMaterialName ) )
		{
			EffectData.SetBool( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.enable, false );
			EffectData.SetEntity( m_effectInstance.GetSharedData(), GetAllBlackboardDefs().EffectSharedData.entity, m_owner );
			m_effectInstance.Run();
		}
	}

	private const function GetAttachmentSlotsForEquipment() : array< TweakDBID >
	{
		var slots : array< TweakDBID >;
		slots.PushBack( T"AttachmentSlots.Underwear" );
		slots.PushBack( T"AttachmentSlots.Chest" );
		slots.PushBack( T"AttachmentSlots.Torso" );
		slots.PushBack( T"AttachmentSlots.Head" );
		slots.PushBack( T"AttachmentSlots.Face" );
		slots.PushBack( T"AttachmentSlots.Legs" );
		slots.PushBack( T"AttachmentSlots.Feet" );
		slots.PushBack( T"AttachmentSlots.RightArm" );
		return slots;
	}

}

