scriptName magicNightEyeScript2 extends ActiveMagicEffect
{Scripted effect for the Night Eye Spell}

import GlobalVariable
GlobalVariable Property VampireNightEye Auto

PlayerVampireQuestScript Property PlayerVampireQuest  Auto
GlobalVariable Property EnableVampireBloodPoints Auto
GlobalVariable Property VampireBloodPoints Auto
GlobalVariable Property VampireNightEyeStrength Auto

;======================================================================================;
;               PROPERTIES  /
;=============/

float property fDelay = 0.83 auto
{time to wait before switching to constant Imod}
ImageSpaceModifier property IntroFX auto
{IsMod applied at the start of the spell effect}
ImageSpaceModifier property MainFX auto
{main isMod for spell}
ImageSpaceModifier property OutroFX auto
{IsMod applied at the end of the spell effect}
Float Property fImodStrength = 1.0 auto
{IsMod Strength from 0.0 to 1.0}
sound property IntroSoundFX auto ; create a sound property we'll point to in the editor
sound property OutroSoundFX auto ; create a sound property we'll point to in the editor
GlobalVariable Property NightEyeTransitionGlobal auto
Spell Property VampireDetectAll02 Auto
;MagicEffect Property NightEyeEffect auto
;Spell Property NightEyeDispelEffectSpell auto
; Obtain the target of the active fire effect

;Actor CasterActor
;======================================================================================;
;               EVENTS                     /
;=============/

Event OnEffectStart(Actor Target, Actor Caster)

	;fImodStrength == VampireNightEyeStrength.GetValue()

	Game.GetPlayer().DispelSpell(VampireDetectAll02)
	Game.GetPlayer().RemoveSpell(VampireDetectAll02)

	;CasterActor = Caster

	; IntroFX.remove()
	; MainFX.remove()									;Kill Imods from another cast, if running.
	; OutroFX.remove()	
	
	if NightEyeTransitionGlobal.GetValue() == 0.0
		NightEyeTransitionGlobal.setValue(1.0)
		int instanceID = IntroSoundFX.play((target as objectReference))          ; play IntroSoundFX sound from my self
		Sound.SetInstanceVolume(instanceID, 0.3)		
		introFX.apply(VampireNightEyeStrength.GetValue())                                  ; apply isMod at full strength
		utility.wait(fDelay)                            ; NOTE - neccessary? 
		;MainFX.apply()
		if NightEyeTransitionGlobal.GetValue() == 1.0
			introFX.PopTo(MainFX,VampireNightEyeStrength.GetValue())	
			NightEyeTransitionGlobal.setValue(2.0)
			VampireNightEye.SetValue(2)				
		endif
		
		If EnableVampireBloodPoints.GetValue() == 10000
			PlayerVampireQuest.VampireBloodPoints(10)
		EndIf			
		
	elseif NightEyeTransitionGlobal.GetValue() == 1.0
		introFX.PopTo(MainFX,VampireNightEyeStrength.GetValue())	
		NightEyeTransitionGlobal.setValue(2.0)
		VampireNightEye.SetValue(2)			
		self.dispel()
	elseif NightEyeTransitionGlobal.GetValue() > 1.0
		self.dispel()
	endif
	
	;VampireNightEye.SetValue(2)	
	
EndEvent

; Event OnMagicEffectApply(ObjectReference akCaster, MagicEffect NightEyeEffect)
	; NightEyeDispelEffectSpell.Cast(CasterActor)
; endEvent

Event OnEffectFinish(Actor Target, Actor Caster)

	;fImodStrength == VampireNightEyeStrength.GetValue()

	; if NightEyeTransitionGlobal.GetValue() == 1.0
		; utility.wait(fDelay)
	; endif
; 	;debug.trace (self + ": We are killing the spell.  The Globale is: " + NightEyeTransitionGlobal.GetValue())

	if NightEyeTransitionGlobal.GetValue() == 1.0 && VampireBloodPoints.GetValue() <= 0
		introFX.PopTo(MainFX,VampireNightEyeStrength.GetValue())	
		NightEyeTransitionGlobal.setValue(2.0)
		VampireNightEye.SetValue(2)			
		self.dispel()
	endif

	if NightEyeTransitionGlobal.GetValue() == 2.0 || VampireBloodPoints.GetValue() <= 0
		NightEyeTransitionGlobal.setValue(3.0)
; 		;debug.trace (self + ": This is a valid outro play, see because 2.0 = " + NightEyeTransitionGlobal.GetValue())
		int instanceID = OutroSoundFX.play((target as objectReference))         ; play OutroSoundFX sound from my self
		Sound.SetInstanceVolume(instanceID, 0.3)
		utility.wait(fDelay)		
		;MainFX.remove()
		MainFX.PopTo(OutroFX,VampireNightEyeStrength.GetValue())
		introFX.remove()
		NightEyeTransitionGlobal.setValue(0.0)
		VampireNightEye.SetValue(5)		
	endif

	;VampireNightEye.SetValue(5)
	
endEvent
