--=============================================
---
--- GoldCounter (Events.lua) by Hexiatröhm
--- Glow Effects
---
--=============================================

local sub	  = string.sub

--- Events including Glow Effects
local function FlashCoinIconsProcess(MyButton, PosNegColor)
    local flash, core, anim

    -- Si le flash existe déjà, on récupère les références
    if MyButton.CoinFlash then
        anim = MyButton.CoinFlash
        flash = MyButton.CoinFlash.Flash
        core = MyButton.CoinFlash.Core
    else
        -- Création du flash
        flash = MyButton:CreateTexture(nil, "OVERLAY")
        flash:SetTexture("Interface\\Cooldown\\starburst")
        flash:SetSize(MyButton:GetWidth() * 1.5, MyButton:GetHeight() * 1.5)
        flash:SetBlendMode("ADD")
        flash:SetAlpha(0)
        flash:SetPoint("CENTER")
        flash:Hide()

        -- Noyau lumineux
        core = MyButton:CreateTexture(nil, "OVERLAY")
        core:SetTexture("Interface\\Cooldown\\starburst")
        core:SetBlendMode("ADD")
        core:SetAlpha(0)
        core:SetPoint("CENTER")
        core:SetSize(MyButton:GetWidth(), MyButton:GetHeight())
        core:Hide()

        -- Animation
        anim = flash:CreateAnimationGroup()

        local fadeIn = anim:CreateAnimation("Alpha")
        fadeIn:SetFromAlpha(0)
        fadeIn:SetToAlpha(1.3)
        fadeIn:SetDuration(0.12)
        fadeIn:SetOrder(1)

        local scale = anim:CreateAnimation("Scale")
        scale:SetScale(1.25, 1.25)
        scale:SetDuration(0.12)
        scale:SetOrder(1)

        local fadeOut = anim:CreateAnimation("Alpha")
        fadeOut:SetFromAlpha(1.3)
        fadeOut:SetToAlpha(0)
        fadeOut:SetDuration(0.25)
        fadeOut:SetOrder(2)

        anim:SetScript("OnPlay", function()
            flash:Show()
            core:Show()
        end)

        anim:SetScript("OnFinished", function()
            flash:Hide()
            core:Hide()
        end)

        -- Stocker les références pour pouvoir les réutiliser
        MyButton.CoinFlash = anim
        MyButton.CoinFlash.Flash = flash
        MyButton.CoinFlash.Core = core
    end

    -- Définir la couleur juste avant de jouer
    if PosNegColor == false then
        flash:SetVertexColor(1, 0.15, 0.15)  -- rouge
        core:SetVertexColor(1, 0.3, 0.3)
    else
        flash:SetVertexColor(1, 0.85, 0.2)   -- doré
        core:SetVertexColor(1, 0.95, 0.4)
    end

    -- Jouer l'animation
    anim:Stop()
    anim:Play()
end
---------------------------------------
function FlashCoinIcons(PosNegColor)

	if ( My_GC_Settings_Local.IconsFlash == false ) then return end

	--- Data = if PosNegColor == true then Positive Lightgold Color or false = Negative Red Color

        FlashCoinIconsProcess ( GC_GoldButton, PosNegColor )

	if ( GC_CopperButton:IsShown() and GC_SilverButton:IsShown() ) then

	        FlashCoinIconsProcess ( GC_SilverButton, PosNegColor )
	        FlashCoinIconsProcess ( GC_CopperButton, PosNegColor )
	end

end
-------------------

-----------------------------------------------------------
--- TIMERS -

--------
--- Timer for Border Color Fight Red
--------
local ticker1 = nil
local tickRate1 = 0.05    -- fréquence interne du ticker

local function OnTick1()

	local data = GC_BorderColor1
	if not data then return end

	if data.Progress then

		data.A = data.A + data.Speed

		if data.A >= 1 then
			data.A = 1
			data.Progress = false
		end
	else

        	data.A = data.A - data.Speed

		if data.A <= 0.4 then
			data.A = 0.4
			data.Progress = true
		end
	end


	GoldCounter_MainFrame:SetBackdropBorderColor( data.R, data.G, data.B, data.A)

end

function StartLoop1()

	if ticker1 then return end

	GC_BorderColor1 = {
        R = 1,
        G = 0.2,
        B = 0.2,
        A = 0.4,
        Progress = true,
        Speed = 0.030,
    }

	ticker1 = C_Timer.NewTicker(tickRate1, OnTick1)
end

function StopLoop1()

	if ticker1 then
        	ticker1:Cancel()
	        ticker1 = nil
	end

	GC_ResetFrame()

end
--------

--------
--- Timer for Border Color Rest Blue
--------
local ticker2 = nil
local tickRate2 = 0.05 -- fréquence interne du ticker

local function OnTick2()

	local data = GC_BorderColor2
	if not data then return end

	if data.Progress then

		data.A = data.A + data.Speed

		if data.A >= 1 then
			data.A = 1
			data.Progress = false
		end
	else

        	data.A = data.A - data.Speed

		if data.A <= 0.4 then
			data.A = 0.4
			data.Progress = true
		end
	end


	GoldCounter_MainFrame:SetBackdropBorderColor( data.R, data.G, data.B, data.A)

end

function StartLoop2()

	if ticker2 then return end

	GC_BorderColor2 = {
        R = 0.4,
        G = 0.4,
        B = 1,
        A = 0.4,
        Progress = true,
        Speed = 0.030,
    }

	ticker2 = C_Timer.NewTicker(tickRate2, OnTick2)
end

function StopLoop2()

	if ticker2 then
        	ticker2:Cancel()
	        ticker2 = nil
	end

	GC_ResetFrame()
end
-----------------------------------------------------------

function GC_GetAddonMessage( ADDON, DATA, CHANNEL, PlayerName )

		if ADDON ~= "GOLDCOUNTER" then return end

		local dataType = sub( DATA, 1, 7 )
		local New_Ver = ""

			--- Check if new version
		if dataType == "LOGIN1-" and not GC_WarnVersionDone then
		
				New_Ver = sub( DATA, 8 ) --- Start from after LOGIN1- (DATA)

				if GC_CheckNewVersion( GC_Version, New_Ver ) then

						GC_WarnVersionDone = true
						GoldCounter_MainFrame:UnregisterEvent( "GUILD_ROSTER_UPDATE" )
						GoldCounter_MainFrame:UnregisterEvent( "GROUP_ROSTER_UPDATE" )

					DEFAULT_CHAT_FRAME:AddMessage( "|cffffff00GoldCounter:|r ".. GC_Locale_Text.MSG_NEWVERSION.. " ( |cff00FF98"..New_Ver.."|r )" )
				end
		end

			---
		if GC_Beacon and GC_Beacon_LastName ~= PlayerName then
			print("|cff00ff00>|r |cffffff00"..PlayerName.."|r - |cff00ffff"..New_Ver.."|r - |cffffa500"..CHANNEL.."|r" )
			GC_Beacon_LastName = PlayerName
		end
			---
end

function GC_ResetFrame()

		if GoldCounter_MainFrame:IsShown() 			--- La frame est affichée
		and My_GC_Settings_Local.TransView == false		--- Mode non transparent 
		and GC_ToggleSwitch == false then			--- Non cachée manuellement

		GoldCounter_MainFrame:SetBackdrop( {
        		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
			edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
			tile = true,
			tileSize = 16,
			edgeSize = 16,
			insets = { left=2, right=2, top=2, bottom=2 } } )

		GoldCounter_MainFrame:SetBackdropColor(0,0,0,0.7)
		GoldCounter_MainFrame:SetBackdropBorderColor(1,1,1,0.8)

		end

end
