class DisassembleManager 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_transactionSystem : TransactionSystem;
	private var m_root : weak< inkWidget >;
	private var m_animProxy : inkAnimProxy;
	private var m_alpha_fadein : inkAnimDef;
	private var m_AnimOptions : inkAnimOptions;
	private var m_DisassembleCallback : UI_CraftingDef;
	private var m_DisassembleBlackboard : weak< IBlackboard >;
	private var m_DisassembleBBID : CallbackHandle;
	private var m_CraftingBBID : CallbackHandle;

	protected event OnInitialize()
	{
		m_player = ( ( PlayerPuppet )( GetOwnerEntity() ) );
		m_InventoryManager = new InventoryDataManagerV2;
		m_InventoryManager.Initialize( m_player );
		m_transactionSystem = ( ( TransactionSystem )( GameInstance.GetTransactionSystem( m_player.GetGame() ) ) );
		inkCompoundRef.RemoveAllChildren( m_listRef );
		SetupBB();
	}

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

	private function SetupBB()
	{
		m_DisassembleCallback = GetAllBlackboardDefs().UI_Crafting;
		m_DisassembleBlackboard = GetBlackboardSystem().Get( m_DisassembleCallback );
		if( m_DisassembleBlackboard )
		{
			m_DisassembleBBID = m_DisassembleBlackboard.RegisterDelayedListenerVariant( m_DisassembleCallback.lastIngredients, this, 'OnDisassembleComplete', true );
		}
	}

	private function UnregisterFromBB()
	{
		if( m_DisassembleBlackboard )
		{
			m_DisassembleBlackboard.UnregisterDelayedListener( m_DisassembleCallback.lastIngredients, m_DisassembleBBID );
		}
	}

	public function ManagePopups()
	{
		if( m_listOfAddedInventoryItems.Size() > 0 )
		{
			CreatePopup();
			CreatePopupDelay();
		}
	}

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

	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 OnDisassembleComplete( value : Variant )
	{
		var disassembledIngredientData : array< IngredientData >;
		var tempData : InventoryItemData;
		var i : Int32;
		var itemID : ItemID;
		var itemRecord : Item_Record;
		var itemdata : weak< gameItemData >;
		disassembledIngredientData = ( ( array< IngredientData > )value );
		if( disassembledIngredientData.Size() > 0 )
		{
			for( i = 0; i < disassembledIngredientData.Size(); i += 1 )
			{
				if( disassembledIngredientData[ i ].itemAmount > 0 )
				{
					itemRecord = disassembledIngredientData[ i ].id;
					itemID = ItemID.FromTDBID( itemRecord.GetID() );
					itemdata = GameInstance.GetTransactionSystem( m_player.GetGame() ).GetItemData( m_player, itemID );
					tempData = m_InventoryManager.GetInventoryItemData( itemdata );
					m_listOfAddedInventoryItems.PushBack( tempData );
					GameInstance.GetActivityLogSystem( ( ( PlayerPuppet )( GetPlayerControlledObject() ) ).GetGame() ).AddLog( ( GetLocalizedText( "UI-ScriptExports-Looted" ) + ": " ) + UIItemsHelper.GetItemName( itemRecord, itemdata ) );
				}
			}
		}
	}

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

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

}

class DisassemblePopupLogicController 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' );
	}

}

