--------------------------------------------------------------------------------
-- GLOBALS
--------------------------------------------------------------------------------

-- CONSTANTS
Global( "MESSAGE_FADE_IN_TIME", 350 )
Global( "MESSAGE_FADE_SOLID_TIME", 2000 )
Global( "MESSAGE_FADE_OUT_TIME", 1000 )

Global( "WIDGET_FADE_TRANSPARENT", 1 )
Global( "WIDGET_FADE_IN", 2 )
Global( "WIDGET_FADE_SOLID", 3 )
Global( "WIDGET_FADE_OUT", 4 )

-- WIDGETS
Global( "wtMessageOne", nil )
Global( "wtMessageTwo", nil )
Global( "wtMessageThree", nil )

-- OTHER
Global( "OneFadeStatus", WIDGET_FADE_TRANSPARENT )
Global( "TwoFadeStatus", WIDGET_FADE_TRANSPARENT )
Global( "ThreeFadeStatus", WIDGET_FADE_TRANSPARENT )

--------------------------------------------------------------------------------
-- EVENT HANDLERS
--------------------------------------------------------------------------------

-- EVENT_AVATAR_CLIENT_ZONE_CHANGED
function OnEventAvatarClientZoneChanged( params )
	local zone = cartographer.GetCurrentZone()
		
	if not common.IsEmptyWString( zone.subZoneName ) then
		wtMessageOne:SetVal( "value", zone.subZoneName )
		wtMessageTwo:SetVal( "value", zone.zoneName )
		wtMessageThree:SetVal( "value", zone.allod )
	else
		wtMessageOne:SetVal( "value", zone.zoneName )
		wtMessageTwo:SetVal( "value", zone.allod )
		wtMessageThree:SetVal( "value", common.GetEmptyWString() )
	end
	
	
	wtMessageOne:Show( true )
	wtMessageTwo:Show( true )
	wtMessageThree:Show( true )
	
	wtMessageOne:PlayFadeEffect( 0.0, 1.0, MESSAGE_FADE_IN_TIME, EA_MONOTONOUS_INCREASE )
	wtMessageTwo:PlayFadeEffect( 0.0, 1.0, MESSAGE_FADE_IN_TIME, EA_MONOTONOUS_INCREASE )
	wtMessageThree:PlayFadeEffect( 0.0, 1.0, MESSAGE_FADE_IN_TIME, EA_MONOTONOUS_INCREASE )
	OneFadeStatus = WIDGET_FADE_IN
	TwoFadeStatus = WIDGET_FADE_IN
	ThreeFadeStatus = WIDGET_FADE_IN
end

-- EVENT_EFFECT_FINISHED
function OnEventEffectFinished ( params )
	
	if params.wtOwner:IsEqual( wtMessageOne ) then
		OneFadeStatus = AnnounceFader( params.wtOwner, OneFadeStatus)
	elseif params.wtOwner:IsEqual( wtMessageTwo ) then
		TwoFadeStatus = AnnounceFader( params.wtOwner, TwoFadeStatus)
	elseif params.wtOwner:IsEqual( wtMessageThree ) then
		ThreeFadeStatus = AnnounceFader( params.wtOwner, ThreeFadeStatus)
	end

end

function AnnounceFader( wtMessage, fadeStatus )
	if fadeStatus == WIDGET_FADE_IN then
		wtMessage:PlayFadeEffect( 1.0, 1.0, MESSAGE_FADE_SOLID_TIME, EA_MONOTONOUS_INCREASE )
		fadeStatus = WIDGET_FADE_SOLID		
	elseif fadeStatus == WIDGET_FADE_SOLID then
		wtMessage:PlayFadeEffect( 1.0, 0.0, MESSAGE_FADE_OUT_TIME, EA_MONOTONOUS_INCREASE )
		fadeStatus = WIDGET_FADE_OUT		
	elseif fadeStatus == WIDGET_FADE_OUT then
		fadeStatus = WIDGET_FADE_TRANSPARENT
		wtMessage:Show( false )
	end
	return fadeStatus
end
--------------------------------------------------------------------------------
-- INITIALIZATION
--------------------------------------------------------------------------------
function Init()
	common.RegisterEventHandler( OnEventAvatarClientZoneChanged, "EVENT_AVATAR_CLIENT_ZONE_CHANGED" )
	common.RegisterEventHandler( OnEventEffectFinished, "EVENT_EFFECT_FINISHED" )
	
	wtMessageOne = mainForm:GetChildChecked( "AnnounceOne", false )
	wtMessageTwo = mainForm:GetChildChecked( "AnnounceTwo", false )
	wtMessageThree = mainForm:GetChildChecked( "AnnounceThree", false )
	wtMessageOne:Show( false )
	wtMessageTwo:Show( false )
	wtMessageThree:Show( false )

	common.StateUnloadManagedAddon( "ZoneAnnounce" )
end
--------------------------------------------------------------------------------
Init()
--------------------------------------------------------------------------------
