Scriptname _00E_ArmorSetScript extends ObjectReference

;=====================================================================================
;              							EVENTS                 					 
;=====================================================================================

Event OnEquipped(Actor akActor)

	If akActor == PlayerREF
		;this event won't be sent by SKSE / SkyUI when using the Equip Mode of SkyUI to directly equip an item from another container.
		;the same goes for the OnObjectEquipped event, hence we need the work around in OnContainerChanged
		UpdateSpells()
	EndIf

EndEvent
	
Event OnUnequipped(Actor akActor)

	If akActor == PlayerREF
		UpdateSpells()
	EndIf
	
EndEvent

Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)

	If akNewContainer == PlayerREF
		; Take All, no need to check for equipped items
		If UI.IsMenuOpen("ContainerMenu") == False
			return
		EndIf

		If PlayerREF.IsEquipped(__Config_SetList)
			UpdateSpells()
		EndIf
	EndIf

EndEvent

;=====================================================================================
;              							FUNCTIONS                 					  
;=====================================================================================

Function UpdateSpells()

	Int iIndex = 0
	Int nEquippedPieces = 0
	Form[] setPieces = __Config_SetList.ToArray()
	While iIndex < setPieces.Length
		If PlayerREF.IsEquipped(setPieces[iIndex] as Armor)
			nEquippedPieces += 1
		EndIf

		iIndex += 1
	EndWhile

	__Config_SetGlobal.SetValueInt(nEquippedPieces)

	Int nBonusSpells = nEquippedPieces - 1
	Bool bSpellRemoved = False
	Bool bSpellAdded = False

	Form[] bonusSpells = __Config_SetBonusesList.ToArray()
	iIndex = 0
	While iIndex < bonusSpells.Length
		Spell bonusSpell = bonusSpells[iIndex] as Spell
		If iIndex < nBonusSpells
			If PlayerREF.HasSpell(bonusSpell) == False
				PlayerREF.AddSpell(bonusSpell)
				bSpellAdded = True
			EndIf
		Else ; iIndex >= nBonusSpells
			If PlayerREF.HasSpell(bonusSpell)
				PlayerREF.RemoveSpell(bonusSpell)
				bSpellRemoved = True
			EndIf
		EndIf

		iIndex += 1
	EndWhile

	If bSpellAdded
		ArmorSetListener.OnArmorSetBonusAdded(nEquippedPieces >= setPieces.Length)
	ElseIf bSpellRemoved
		ArmorSetListener.OnArmorSetBonusRemoved()
	EndIf

EndFunction


;=====================================================================================
;              							PROPERTIES                 					 
;=====================================================================================

Bool Property __Config_5Pieces Auto
{Does this set have five pieces? Default: False}

_00E_ArmorSetListener Property ArmorSetListener Auto

GlobalVariable Property __Config_SetGlobal Auto
{The global associated with this set. Example _25E_HSet_FallenOneGlobal.}

Formlist Property __Config_SetBonusesList Auto
{The formlist containing the spell bonuses of the set.}

Formlist Property __Config_SetList Auto
{The formlist containing the set pieces.}

Actor Property PlayerREF Auto