ScriptName aaaKimachuuDDCRA Extends Quest

Actor Property PlayerRef Auto
MiscObject Property aaaKimachuuDDCCameraMisc Auto
MiscObject Property aaaKimachuuDDCFilmMisc Auto

aaaKimachuuDDCMCM Property config Auto

Bool bRun

Event OnInit()
	keyUpdate(config.iKey)
EndEvent

Event OnPlayerLoadGame()
	keyUpdate(config.iKey)
EndEvent

Event OnKeyDown(Int aiKeyCode)
	main()
EndEvent

Function main()
	If PlayerRef.GetItemCount(aaaKimachuuDDCCameraMisc) <= 0 || PlayerRef.GetItemCount(aaaKimachuuDDCFilmMisc) <= 0
		Return
	EndIf

	If Utility.IsInMenuMode()
		inMenu()
	Else
		outMenu()
	EndIf
EndFunction

Function inMenu()
	If bRun
		Return
	EndIf

	bRun = True
	String MENU_ROOT = "_root.Menu_mc.inventoryLists.itemList.selectedEntry"
	Form forItem = Game.GetFormEx(UI.GetInt("InventoryMenu", MENU_ROOT + ".formId"))

	If forItem.GetType() == 42
		PlayerRef.AddItem(forItem, 10)
		PlayerRef.RemoveItem(aaaKimachuuDDCFilmMisc, 1)
	ElseIf forItem && forItem != aaaKimachuuDDCCameraMisc && forItem != aaaKimachuuDDCFilmMisc
		PlayerRef.AddItem(forItem, 1)
		PlayerRef.RemoveItem(aaaKimachuuDDCFilmMisc, 1)
	EndIf
	bRun = False
EndFunction

Function outMenu()
	If bRun
		Return
	EndIf

	bRun = True
	ObjectReference TargetRef = Game.GetCurrentCrosshairRef() as ObjectReference
	Int iFormID = TargetRef.GetBaseObject().GetFormID()
	Form forItem = Game.GetFormEx(iFormID)

	If forItem.GetType() == 42
		PlayerRef.AddItem(forItem, 10)
		PlayerRef.RemoveItem(aaaKimachuuDDCFilmMisc, 1)
	ElseIf forItem && forItem != aaaKimachuuDDCCameraMisc && forItem != aaaKimachuuDDCFilmMisc && forItem.GetType() != 30 && forItem.GetType() != 39
		PlayerRef.AddItem(forItem, 1)
		PlayerRef.RemoveItem(aaaKimachuuDDCFilmMisc, 1)
	EndIf
	bRun = False
EndFunction

Function keyUpdate(Int i01)
	RegisterForKey(i01)
EndFunction