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 SKSE.GetVersion() > 0 && UI.IsMenuOpen("ContainerMenu") == False
			return
		EndIf

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

EndEvent

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

Function UpdateSpells()

	bool bSKSE = (SKSE.GetVersion() > 0)

	Int iIndex
	Int nEquippedPieces = 0
	Form[] setPieces
	
	if bSKSE
		setPieces = __Config_SetList.ToArray()
	else
		int nPieces = __Config_SetList.GetSize()
		if nPieces == 3
			setPieces = new Form[3]
		elseif nPieces == 4
			setPieces = new Form[4]
		elseif nPieces == 5
			setPieces = new Form[5]
		else
			setPieces = new Form[6]
		endif
		iIndex = 0
		while iIndex < nPieces
			setPieces[iIndex] = __Config_SetList.GetAt(iIndex)
			iIndex += 1
		endwhile
	endif
	
	iIndex = 0
	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

	if bSKSE
		bonusSpells = __Config_SetBonusesList.ToArray()
	else
		int nSpells = __Config_SetBonusesList.GetSize()

		if nSpells == 3
			bonusSpells = new Form[3]
		elseif nSpells == 4
			bonusSpells = new Form[4]
		elseif nSpells == 5
			bonusSpells = new Form[5]
		else
			bonusSpells = new Form[6]
		endif
		iIndex = 0
		while iIndex < bonusSpells.Length
			bonusSpells[iIndex] = __Config_SetBonusesList.GetAt(iIndex)
			iIndex += 1
		endwhile
	endif
	
	iIndex = 0
	While iIndex < bonusSpells.Length
		Spell bonusSpell = bonusSpells[iIndex] as Spell
		
		if bonusSpell
			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
		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