Scriptname _00E_BlueprintCollectionItem extends ObjectReference Hidden 

Event OnEquipped(Actor akActor)

	If akActor != PlayerREF || ! (GetBaseObject() as MiscObject)
		return
	endif
	
	bInventoryMenu = SKSE.GetVersion() > 0 && UI.IsMenuOpen("InventoryMenu")
	_00E_FS_BlueprintContainerActorREF.SetPlayerTeammate(True, True)
	
	Int iButton = _00E_FS_Blueprint_Message_SE.Show()
	If iButton == 0
		AddBlueprints()
	ElseIf iButton == 1
		StoreSingleCopy()
	ElseIf iButton == 2
		TakeBlueprints()
	EndIf

EndEvent

Function AddBlueprints()
	if bInventoryMenu
		; The DisablePlayerControls method does not remember last inventory tab to return where we were
		Input.TapKey(Input.GetMappedKey("Tween Menu"))
	endif
	Game.DisablePlayerControls(abMenu = true)
	Game.EnablePlayerControls(abMenu = true)
	Utility.Wait(0.05)
	_00E_FS_BlueprintContainerActorREF.ShowGiftMenu(True, _00E_FS_BlueprintList, True, False)
	if bInventoryMenu
		Utility.wait(0.2)
		Input.TapKey(Input.GetMappedKey("Quick Inventory"))
	endif
EndFunction

function StoreSingleCopy()
;/ Too slow
	int iItemsToStore = PlayerREF.GetItemCount(Blueprint) - _00E_FS_BlueprintContainerActorREF.GetItemCount(Blueprint)
	bool bStoreSilently = iItemsToStore > 3
	Int iFormIndex = PlayerREF.GetNumItems()
	While iFormIndex > 0
		iFormIndex -= 1
		Form kForm = PlayerREF.GetNthForm(iFormIndex)
		If kForm as MiscObject && kForm.HasKeyword(Blueprint) && _00E_FS_BlueprintContainerActorREF.GetItemCount(kForm) == 0
			PlayerREF.RemoveItem(kForm, 1, bStoreSilently, _00E_FS_BlueprintContainerActorREF)
		EndIf
	EndWhile
	if bStoreSilently
		_00E_FS_BlueprintItemsStored.Show(iItemsToStore)
	endif
/;
	int iCount = EnderalFunctions.MoveItemsToCountByKeyword(PlayerREF, _00E_FS_BlueprintContainerActorREF, Blueprint, 1)

	if iCount > 0
		ITMBookClose.Play(PlayerREF)
		
		; Moving items is fast, refreshing inventory is not
		Utility.WaitMenuMode(0.5)
		Form item = Game.GetForm(0x1FE82) ; non-playable Draugr Skin
		PlayerREF.RemoveItem(item, 1, true)
		
		if iCount > 150
			Utility.WaitMenuMode(0.5)
			PlayerREF.RemoveItem(item, 1, true) ; doing this twice seems to reliably refresh inventory
		endif
	endif
	
	_00E_FS_BlueprintItemsStored.Show(iCount)
endfunction

Function TakeBlueprints()
	if bInventoryMenu
		; The DisablePlayerControls method does not remember last inventory tab to return where we were
		Input.TapKey(Input.GetMappedKey("Tween Menu"))
	endif
	Game.DisablePlayerControls(abMenu = true)
	Game.EnablePlayerControls(abMenu = true)
	Utility.Wait(0.05)
	_00E_FS_BlueprintContainerActorREF.ShowGiftMenu(False, _00E_FS_BlueprintList, True, False)
	if bInventoryMenu
		Utility.wait(0.2)
		Input.TapKey(Input.GetMappedKey("Quick Inventory"))
	endif
EndFunction

bool bInventoryMenu

Actor Property PlayerRef  Auto  
Actor Property _00E_FS_BlueprintContainerActorREF  Auto  

Message Property _00E_FS_Blueprint_Message_SE  Auto  
Message Property _00E_FS_BlueprintItemsStored  Auto  

FormList Property _00E_FS_BlueprintList  Auto  

Keyword Property Blueprint  Auto  

Sound Property ITMBookClose Auto