Scriptname wizContainerRespawnScript extends ObjectReference  

GlobalVariable Property wizOptionDTResetTime Auto
FormList Property wizMarkerList Auto
FormList Property wizSafeContainerList Auto

Bool ItemAdded
Float Respawn

Event OnCellDetach()
	Respawn = Utility.GetCurrentGameTime()
endEvent

Event OnCellAttach()
	if ItemAdded != TRUE
		if utility.getCurrentGameTime() >= Respawn + wizOptionDTResetTime.getvalue()
			Self.disable()
			self.delete()
		endIf
	endIf
endEvent

Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
	if akSourceContainer == Game.GetPlayer()
		if ItemAdded == False
			ItemAdded= TRUE
			if !wizSafeContainerList.HasForm(self)
				wizSafeContainerList.AddForm(self)
;				debug.notification(wizSafeContainerList.GetSize())
			endIf
			debug.notification("Dynamic Things: This container is safe for storage")
		endIf
	endIf
endEvent

Event OnActivate(ObjectReference akActionRef)
	if akActionRef == Game.GetPlayer()
		if ItemAdded == TRUE
			if !wizSafeContainerList.HasForm(self)
				wizSafeContainerList.AddForm(self)
;				debug.notification(wizSafeContainerList.GetSize())
			endIf
			debug.notification("Dynamic Things: This container is safe for storage")
		endIf
	endIf
endEvent

Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
	if akDestContainer == game.GetPlayer()
		;Mark respawnable if all items are removed
		if self.getNumItems() == 0 && ItemAdded == TRUE
			ItemAdded = FALSE
			if wizSafeContainerList.HasForm(self)
				wizSafeContainerList.RemoveAddedForm(self)
;				debug.notification(wizSafeContainerList.GetSize())
			endIf
			debug.notification("This empty container will now respawn")
		endif
	endIf
endEvent