Scriptname _00E_Phasmalist_ApparationInventory extends ReferenceAlias
{total strength = (speed * base damage) or (base armor rating) or 0 + enchantement strength * magicEffectStrengthWeight + 
	magicEffectStrengthWeight * spellbook spellstrength}
; script that manages the spectralizing process. It is attached to the alias that contains the spectralizing container.
; this alias is filled and the functions of this script are called mainly from _00E_Phasmalist_Workbench.

import EnderalLib

Message Property spectralizingCost auto
Message Property cannotSpectralize auto
Message Property updateCostCannotPay auto
Message Property updateCostCanPay auto

float Property strengthPointsPerSoulGem = 20.0 autoreadonly
float Property strengthPointsPerBonemealOrEctoplasma = 8.0 autoreadonly

Formlist Property soulGemTypesOrderedByStrength auto
Formlist Property bonemealEctoplasmaTypesOrderedByStrength auto

Actor Property PlayerREF Auto

Function RemoveItemsUntilLimitReached(Actor that, Formlist itemList, int amount)
	Int index = 0
	Int maxSize = itemList.GetSize()
	Int amountLeft = amount
	While index < maxSize && amountLeft > 0
		Form item = itemList.GetAt(index)
		Int amountThatHad = that.GetItemCount(item)
		If amountThatHad >= amountLeft
			that.RemoveItem(item, amountLeft)
		Else
			that.RemoveItem(item, amountThatHad)
		EndIf
		amountLeft -= amountThatHad
		index = index + 1
	EndWhile
EndFunction

Bool Function EvaluateAndClose(ObjectReference apparationContainer)
{calculates the strength of all the items in the container and asks the player wether to spectralize them or not. Appropriatly, the spectralizing cost are removed from the player and the items are
returned to him or added to the apparition. The return value is true when any item was added to the apparition}
;returns if an item has been added to the apparition
	ObjectReference selfRef = self.getRef()
	If selfRef.GetNumItems() == 0
		return false
	EndIf
	Float strength = CalculateInventoryStrength()
	int soulGemAmount = Math.floor(strength / strengthPointsPerSoulGem)
	If soulGemAmount <= 0
		soulGemAmount = 1
	EndIf
	int bonemealAmount = Math.ceiling(strength / strengthPointsPerBonemealOrEctoplasma)
	int soulGemAmountPlayerHas = PlayerREF.GetItemCount(soulGemTypesOrderedByStrength)
	int bonemealAmountPlayerHas = PlayerREF.GetItemCount(bonemealEctoplasmaTypesOrderedByStrength)
	bool didAddItems = false
	
	If (soulGemAmountPlayerHas >= soulGemAmount && bonemealAmountPlayerHas >= bonemealAmount && selfRef.GetNumItems() > 0)
		int button = spectralizingCost.show(bonemealAmount, bonemealAmountPlayerHas, soulGemAmount, soulGemAmountPlayerHas)
		If (button == 0)
			didAddItems = true
			selfRef.removeallitems(apparationContainer, false, true)
			RemoveItemsUntilLimitReached(PlayerREF, soulGemTypesOrderedByStrength, soulGemAmount)
			RemoveItemsUntilLimitReached(PlayerREF, bonemealEctoplasmaTypesOrderedByStrength, bonemealAmount)
		Else
			AddItemsToPlayer()
		EndIf
	Else
		cannotSpectralize.Show(bonemealAmount, bonemealAmountPlayerHas, soulGemAmount, soulGemAmountPlayerHas)
		AddItemsToPlayer()
	EndIf
	Return didAddItems
EndFunction

Function AddItemsToPlayer()
	self.getRef().removeallitems(akTransferTo = PlayerREF, abRemoveQuestItems = true)
EndFunction

Float Function CalculateInventoryStrength()
	Return EnderalLib.calculateContentStrength(Self.GetRef())
EndFunction

Function UpdateCost()
	float strength = CalculateInventoryStrength()
	int soulGemAmount = Math.floor(strength / strengthPointsPerSoulGem)
	If soulGemAmount <= 0
		soulGemAmount = 1
	EndIf
	int bonemealAmount = Math.ceiling(strength / strengthPointsPerBonemealOrEctoplasma)
	int soulGemAmountPlayerHas = PlayerREF.GetItemCount(soulGemTypesOrderedByStrength)
	int bonemealAmountPlayerHas = PlayerREF.GetItemCount(bonemealEctoplasmaTypesOrderedByStrength)
	
	If (soulGemAmountPlayerHas >= soulGemAmount && bonemealAmountPlayerHas >= bonemealAmount)
		updateCostCanPay.show(bonemealAmount, bonemealAmountPlayerHas, soulGemAmount, soulGemAmountPlayerHas)
	Else
		updateCostCannotPay.show(bonemealAmount, bonemealAmountPlayerHas, soulGemAmount, soulGemAmountPlayerHas)
	EndIf
EndFunction

Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
	UpdateCost()
EndEvent

Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
	If (self.getRef() == None) || (self.getRef().getNumItems() == 0)
		return
	EndIf
	
	UpdateCost()
EndEvent