Scriptname _00E_FS_Dismantle_Workbench extends ObjectReference
{lets the player melt down items to one of its base components, extends the crafting classes}

; properties
Actor Property PlayerREF Auto
FormList Property _00E_DismantleList Auto
FormList Property _00E_DismantleItemList Auto
FormList Property _00E_DismantleResetList Auto
Message Property _00E_CheckForDismantlingMSG Auto
Keyword Property CraftingSmelterDismantling Auto
Keyword Property InvisibleDismantling Auto
Perk Property _00E_Class_Phasmalist_P04_A_ArcaneBreakdown Auto

; events
Event OnInit()
	BlockActivation()
EndEvent

Event OnActivate(ObjectReference akActionRef)

	If Self.IsActivationBlocked()
		RegisterForMenu("Crafting Menu")
		; GenerateMeltDownList() is relatively slow due to using formlists hence we notify the player that something is calculated in the background
		_00E_CheckForDismantlingMSG.Show()
		GenerateMeltDownList()
		Self.Activate(PlayerREF, true)
	EndIf

Endevent

Event OnMenuClose(String MenuName)

	If menuName == ("Crafting Menu")
		UnregisterForMenu("Crafting Menu")
		ResetMeltDownList()
		; only enable the player controls after everything is reset properly
		Game.EnablePlayerControls()
	EndIf

EndEvent

; functions
Function GenerateMeltDownList()
; this function is responsible for filling a list based on the items in the players inventory which can be melted down to crafting material

	Int iTotalItemCount = PlayerREF.GetNumItems()
	Int iItemCount = 0
	Form ItemToCheck
	Bool bPlayerHasArcaneBreakdown = PlayerREF.HasPerk(_00E_Class_Phasmalist_P04_A_ArcaneBreakdown)
	Form equippedItemLeft = PlayerRef.GetEquippedObject(0)
	Form equippedItemRight = PlayerRef.GetEquippedObject(1)
	
	While iItemCount < iTotalItemCount
		ItemToCheck = PlayerREF.GetNthForm(iItemCount)

		If (ItemToCheck as Armor) != None ; armor
			;checks the item if it's equipped and if it's either enchanted or if the player has the perk to dismantle enchantmented items
			If ItemToCheck != equippedItemRight && ItemToCheck != equippedItemLeft && PlayerREF.IsEquipped(ItemToCheck) == False
				If bPlayerHasArcaneBreakdown || (ItemToCheck as Armor).GetEnchantment() == None
					AddItemToDismantleList(ItemToCheck)
				EndIf
			EndIf
		ElseIf (ItemToCheck as Weapon) != None ; weapon
			;checks the item if it's equipped and if it's either enchanted or if the player has the perk to dismantle enchantmented items
			; Note: IsEquipped does not work for any stuff in the left hand when dual wielding
			If ItemToCheck != equippedItemRight && ItemToCheck != equippedItemLeft 
				If bPlayerHasArcaneBreakdown || (ItemToCheck as Weapon).GetEnchantment() == None
					AddItemToDismantleList(ItemToCheck)
				EndIf
			EndIf
		EndIf
		
		iItemCount += 1
	EndWhile
EndFunction

Function AddItemToDismantleList(Form itemToAdd)
	; if the item passes the checks, we need to set the workbenchkeyword of the respective constructible object so it gets displayed properly in the menu
	; it also adds it to the reset list so the keyword gets set back to the default one after exiting the menu
	Int iFoundAtIndex = _00E_DismantleItemList.Find(itemToAdd)
	If iFoundAtIndex >= 0
		ConstructibleObject recipe = _00E_DismantleList.GetAt(iFoundAtIndex) as ConstructibleObject
		recipe.SetWorkBenchKeyword(CraftingSmelterDismantling)
		_00E_DismantleResetList.AddForm(recipe)
	EndIf
EndFunction

Function ResetMeltDownList()
	; this function resets the workbenchkeyword back to default
	Int iTotalRecipesToReset = _00E_DismantleResetList.GetSize()
	Int iRecipesToReset = iTotalRecipesToReset
	
	While iRecipesToReset > 0
		iRecipesToReset -= 1
		(_00E_DismantleResetList.GetAt(iRecipesToReset) as ConstructibleObject).SetWorkBenchKeyword(InvisibleDismantling)
	EndWhile
	_00E_DismantleResetList.Revert()
EndFunction