class ItemLog extends gameuiMenuGameController
{
	private editable var m_listRef : inkCompoundRef;
	private editable var m_initialPopupDelay : Float;
	default m_initialPopupDelay = 1.0;
	private var m_popupList : array< weak< DisassemblePopupLogicController > >;
	private var m_listOfAddedInventoryItems : array< InventoryItemData >;
	private var m_player : weak< PlayerPuppet >;
	private var m_InventoryManager : InventoryDataManagerV2;
	private var m_data : ItemLogUserData;
	private var m_onScreenCount : Int32;
	private var m_animProxy : inkAnimProxy;
	private var m_alpha_fadein : inkAnimDef;
	private var m_AnimOptions : inkAnimOptions;

	protected event OnInitialize()
	{
		m_player = ( ( PlayerPuppet )( GetOwnerEntity() ) );
		m_InventoryManager = new InventoryDataManagerV2;
		m_InventoryManager.Initialize( m_player );
		inkCompoundRef.RemoveAllChildren( m_listRef );
		m_data = ( ( ItemLogUserData )( GetRootWidget().GetUserData( 'ItemLogUserData' ) ) );
		m_data.token.RegisterListener( this, 'OnItemAdded' );
	}

	protected event OnUninitialize()
	{
		m_InventoryManager.UnInitialize();
	}

	public function ManagePopups()
	{
		var userData : ItemLogUserData;
		if( ( m_listOfAddedInventoryItems.Size() == 0 ) && ( m_onScreenCount <= 0 ) )
		{
			userData = new ItemLogUserData;
			userData.itemLogQueueEmpty = true;
			m_data.token.TriggerCallback( userData );
		}
		if( ( ( m_listOfAddedInventoryItems.Size() > 0 ) && ( m_onScreenCount <= 3 ) ) && !( m_animProxy.IsPlaying() ) )
		{
			CreatePopup();
			CreatePopupDelay();
		}
	}

	private function CreatePopup()
	{
		var popup : weak< ItemLogPopupLogicController >;
		popup = ( ( ItemLogPopupLogicController )( SpawnFromLocal( inkCompoundRef.Get( m_listRef ), 'itemLog_popup' ).GetController() ) );
		popup.SetupData( m_listOfAddedInventoryItems.PopBack() );
		popup.RegisterToCallback( 'OnPopupComplete', this, 'OnRemovePopup' );
		m_onScreenCount += 1;
	}

	private function CreatePopupDelay()
	{
		var alphaInterpolator : inkAnimTransparency;
		m_alpha_fadein = new inkAnimDef;
		alphaInterpolator = new inkAnimTransparency;
		alphaInterpolator.SetDuration( m_initialPopupDelay );
		alphaInterpolator.SetStartTransparency( 1.0 );
		alphaInterpolator.SetEndTransparency( 1.0 );
		alphaInterpolator.SetType( inkanimInterpolationType.Linear );
		alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		m_alpha_fadein.AddInterpolator( alphaInterpolator );
		m_animProxy = inkCompoundRef.PlayAnimation( m_listRef, m_alpha_fadein );
		m_animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnDelayComplete' );
	}

	protected event OnItemAdded( data : inkGameNotificationData )
	{
		var userData : ItemLogUserData;
		var tempData : InventoryItemData;
		var itemID : ItemID;
		var itemdata : weak< gameItemData >;
		userData = ( ( ItemLogUserData )( data ) );
		itemID = userData.itemID;
		if( userData.itemLogQueueEmpty )
		{
			return false;
		}
		if( ItemID.IsValid( itemID ) )
		{
			itemdata = GameInstance.GetTransactionSystem( m_player.GetGame() ).GetItemData( m_player, itemID );
			tempData = m_InventoryManager.GetInventoryItemData( itemdata );
			if( !( InventoryDataManagerV2.IsItemBlacklisted( itemdata ) ) )
			{
				m_listOfAddedInventoryItems.PushBack( tempData );
				ManagePopups();
			}
		}
	}

	protected event OnRemovePopup( widget : weak< inkWidget > )
	{
		inkCompoundRef.RemoveChild( m_listRef, widget );
		m_onScreenCount -= 1;
		ManagePopups();
	}

	protected event OnDelayComplete( anim : inkAnimProxy )
	{
		ManagePopups();
	}

}

class ItemLogPopupLogicController extends inkLogicController
{
	private editable var m_quantity : inkTextRef;
	private editable var m_icon : inkImageRef;
	private editable var m_label : inkTextRef;
	private editable var m_duration : Float;
	default m_duration = 3.0f;
	private var m_animProxy : inkAnimProxy;
	private var m_alpha_fadein : inkAnimDef;
	private var m_AnimOptions : inkAnimOptions;

	protected event OnInitialize() {}

	public function SetupData( const itemData : ref< InventoryItemData > )
	{
		inkTextRef.SetText( m_label, InventoryItemData.GetName( itemData ) );
		inkTextRef.SetText( m_quantity, "x" + ( ( String )( InventoryItemData.GetQuantity( itemData ) ) ) );
		inkImageRef.SetTexturePart( m_icon, StringToName( InventoryItemData.GetIconPath( itemData ) ) );
		m_animProxy = PlayLibraryAnimation( 'AddPopup' );
		m_animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnAddPopupComplete' );
	}

	protected event OnAddPopupComplete( anim : inkAnimProxy )
	{
		var alphaInterpolator : inkAnimTransparency;
		m_alpha_fadein = new inkAnimDef;
		alphaInterpolator = new inkAnimTransparency;
		alphaInterpolator.SetDuration( m_duration );
		alphaInterpolator.SetStartTransparency( 1.0 );
		alphaInterpolator.SetEndTransparency( 1.0 );
		alphaInterpolator.SetType( inkanimInterpolationType.Linear );
		alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		m_alpha_fadein.AddInterpolator( alphaInterpolator );
		m_animProxy = inkTextRef.PlayAnimation( m_quantity, m_alpha_fadein );
		m_animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnPopupDurationComplete' );
	}

	protected event OnPopupDurationComplete( anim : inkAnimProxy )
	{
		CallCustomCallback( 'OnPopupComplete' );
	}

}

