Scriptname wizSafeContainerMarkerScript extends ObjectReference  

FormList Property wizSafeContainerList Auto
Formlist Property wizParentIssues Auto
GlobalVariable Property wizContainerMenuOpen Auto
Message Property wizConvertSafeContainerFailure Auto

objectReference oldContainer
objectReference safeContainer
bool ParentIssues

Event OnInit()
	if !oldContainer

		;Wait for container menu to close
		While wizContainerMenuOpen.GetValueInt() == 1
			utility.wait(1.0)
		endWhile

		;getFormID() was not giving the correct integer for Dawnguard or possibly anything but vanilla

		oldContainer = FindClosestContainer(self)

;debug.messagebox(oldContainer + "base:" + oldContainer.GetBaseObject())

		if oldContainer 
			oldContainer.disable()
			if !oldContainer.IsDisabled()
				;This catches parent problems and add it to an ignore list to prevent
				debug.trace ("********** wizDYNAMIC THINGS - The Enable State Parent error above has been accounted for and will not occur again for this object **********")
				ParentIssues = TRUE
				;Add to parent issues for logging!
				if !(wizParentIssues.HasForm(oldContainer)) 
					wizParentIssues.AddForm(oldContainer)
				endIf
				
				;Display failure notice
				int Failure = wizConvertSafeContainerFailure.show()
				
			Else
;debug.messagebox("Disabled:" + oldContainer.IsDisabled())
				safeContainer = oldContainer.placeatme(oldContainer.GetBaseObject())

;debug.messagebox(safeContainer)

				safeContainer.setScale(oldContainer.getScale())
				safeContainer.SetAngle(oldContainer.GetAngleX(), oldContainer.GetAngleY(), oldContainer.GetAngleZ())
				oldContainer.RemoveAllItems(safeContainer, TRUE, TRUE)
				ActorBase Owner = oldContainer.GetActorOwner()
				if Owner
					safeContainer.setActorOwner(owner)
				endIf
				if !wizSafeContainerList.HasForm(safeContainer)
					wizSafeContainerList.addForm(safeContainer)
				endIf

;debug.messagebox("Owner:" + Owner + "SafeHasForm:" + wizSafeContainerList.HasForm(safeContainer))
			
			endIf

		endIf
	endIf
endEvent


Event OnCellDetach()
	;Re-enable old container - this is mainly for unistallation puposes
	if ParentIssues != TRUE
		oldContainer.enable()
	endIf

	if safeContainer
		;Disable wood pile container
		safeContainer.disable()
	else
		self.disable()
		self.delete()
	endIf

endEvent

Event OnCellAttach()
	if ParentIssues != TRUE && !wizParentIssues.HasForm(oldContainer)
		oldContainer.disable()

		if !oldContainer.IsDisabled()
			;This catches parent problems and add it to an ignore list to prevent
			debug.trace ("********** wizDYNAMIC THINGS - The Enable State Parent error above has been accounted for and will not occur again for this object **********")
			ParentIssues = TRUE
			;Add to parent issues for logging!
			if !(wizParentIssues.HasForm(oldContainer)) 
				wizParentIssues.AddForm(oldContainer)
			endIf
		endIf
	endIf

	if safeContainer
		safeContainer.enable()
	else
		self.disable()
		self.delete()
	endIf
	
endEvent

objectReference Function FindClosestContainer(objectReference Object)
			cell kCell= self.GetParentCell()
			Int iIndex = kCell.GetNumRefs(28) ; kContainer= 28

			;Are we a merchant?
			While iIndex
				iIndex -= 1
				objectReference Cont = kCell.GetNthRef(iIndex, 28) as ObjectReference
				if Cont 
					;How cloase are we
					if self.getDistance(Cont) < 15
						;This one
						return Cont
					endIf
				endIf
			EndWhile		

			return NONE

endFunction
