ScriptName _amm_DistributeAncestorStatuesScript Extends ObjectReference

Actor Property acPlayer Auto
GlobalVariable Property gvStabilizerQst Auto
ObjectReference[] Property orChest Auto
MiscObject Property moAncestorStatue Auto
SoulGem Property sgBlack Auto

Int Property iNumStatues = 0 Auto Hidden
Int Property iNumBlackGems = 0 Auto Hidden

Event OnItemAdded(Form akBaseItem, Int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)

	Int iNdx
	Bool bStatues = False
	Bool bBlackGems = False
	
	If (akSourceContainer == acPlayer && akBaseItem == moAncestorStatue As Form)
		iNdx = 0
		iNumStatues += 1
		If (iNumStatues == 5)
			While (iNdx < 5)
				Self.RemoveItem(moAncestorStatue As Form, 1, True, orChest[iNdx])
				iNdx += 1
			EndWhile
			bStatues = True
		EndIf
	ElseIf (akSourceContainer == acPlayer && akBaseItem == sgBlack As SoulGem)
		iNdx = 0
		iNumBlackGems += 1
		If (iNumBlackGems == 5)
			While (iNdx < 5)
				If ((akBaseItem As SoulGem).GetSoulSize() == 5)
					Self.RemoveItem(sgBlack As SoulGem, 1, True, orChest[iNdx])
					iNdx += 1
				EndIf
			EndWhile
			bBlackGems = True
		EndIf
	Else
		;Debug.Notification("All five Ancestor Statues and filled black soul gems should be stored in this container!")
		If (bStatues)
			Self.RemoveItem(akBaseItem, aiItemCount, True, acPlayer)
		ElseIf (bBlackGems)
			Self.RemoveItem(akBaseItem, aiItemCount, True, acPlayer)
		EndIf
	EndIf
	If (bStatues && bBlackGems)
		gvStabilizerQst.SetValue(1)
	EndIf

EndEvent
