-- Copyright © 2008, 2009 Xianghar  <xian@zron.de>
-- All Rights Reserved.
-- This code is not to be modified or distributed without written permission by the author.
-- Current distribution permissions only include curse.com, wowui.worldofwar.net, wowinterface.com and their respective addon updaters

local SpellIDs = TotemTimers.SpellIDs
local SpellTextures = TotemTimers.SpellTextures
local SpellNames = TotemTimers.SpellNames
local AvailableSpells = TotemTimers.AvailableSpells

local cds = {}
local spells = {SpellNames[SpellIDs.StormStrike], SpellNames[SpellIDs.EarthShock], SpellNames[SpellIDs.LavaLash], }
local maelstrom = CreateFrame("StatusBar", "TotemTimers_MaelstromBar", UIParent, "XiTimersTimerBarTemplate, SecureActionButtonTemplate")
local maelstrombutton = CreateFrame("Button", "TotemTimers_MaelstromBarButton", TotemTimers_MaelstromBar, "ActionButtonTemplate, SecureActionButtonTemplate")
local es

function TotemTimers_CreateEnhanceCDs()   
    local stormstrike = XiTimers:new(1)
    stormstrike.button.icons[1]:SetTexture(SpellTextures[SpellIDs.StormStrike])
    stormstrike.events[1] = "SPELL_UPDATE_COOLDOWN"
    stormstrike.events[2] = "PLAYER_REGEN_ENABLED"
    stormstrike.events[3] = "PLAYER_REGEN_DISABLED"
    stormstrike.button.anchorframe = TotemTimers_EnhanceCDsFrame
    stormstrike.dontAlpha = true
    stormstrike.dontFlash = true
    stormstrike.timeStyle = "sec"
    stormstrike.button:SetAttribute("*type1", "spell")
    stormstrike.button:SetAttribute("*spell1", SpellNames[SpellIDs.StormStrike])
    stormstrike.activate = function(self)
        XiTimers.activate(self)
        if not InCombatLockdown() and TotemTimers_Settings.HideEnhanceCDsOOC then stormstrike:Hide() end
    end
    stormstrike:SetPoint("CENTER", TotemTimers_EnhanceCDsFrame, "CENTER")
    stormstrike.button:SetScript("OnEvent", TotemTimers.EnhanceCDEvents)
    stormstrike.button.icons[1]:SetAlpha(1)
    cds[1] = stormstrike
    stormstrike.button:RegisterForClicks("LeftButtonUp")
    stormstrike.reverseAlpha = true
    stormstrike.RangeCheck = SpellNames[SpellIDs.StormStrike]
	stormstrike.ManaCheck = SpellNames[SpellIDs.StormStrike]
 
    es = XiTimers:new(1)
    es.button.icons[1]:SetTexture(SpellTextures[SpellIDs.EarthShock])
    es.button:RegisterForClicks("LeftButtonUp", "RightButtonUp", "MiddleButtonUp")
    es.button.anchorframe = TotemTimers_EnhanceCDsFrame
    es.dontAlpha = true
    es.dontFlash = true
    es.timeStyle = "sec"
    es.button:SetAttribute("*type*", "spell")
    es.activate = function(self)
        XiTimers.activate(self)
        if not InCombatLockdown() and TotemTimers_Settings.HideEnhanceCDsOOC then es:Hide() end
    end
    es.button:SetAttribute("*spell1", SpellNames[SpellIDs.EarthShock])
    es.button:SetAttribute("*spell2", SpellNames[SpellIDs.FrostShock])
    es.button:SetAttribute("*spell3", SpellNames[SpellIDs.FlameShock])
    maelstrombutton:SetAttribute("*spell1", SpellNames[SpellIDs.LightningBolt])
    maelstrombutton:SetAttribute("*spell2", SpellNames[SpellIDs.ChainLightning])
    es.RangeCheck = SpellNames[SpellIDs.EarthShock]
    es.ManaCheck = SpellNames[SpellIDs.EarthShock]
    es:Anchor(stormstrike, "LEFT")
    es.button.icons[1]:SetAlpha(1)
    cds[2] = es
    es.reverseAlpha = true

    local ll = XiTimers:new(1)
    ll.button.icons[1]:SetTexture(SpellTextures[SpellIDs.LavaLash])
    ll.button.anchorframe = TotemTimers_EnhanceCDsFrame
    ll.dontAlpha = true
    ll.dontFlash = true
    ll.timeStyle = "sec"
    ll.button:SetAttribute("*type1", "spell")
    ll.button:SetAttribute("*spell1", SpellNames[SpellIDs.LavaLash])
    ll.activate = function(self)
        XiTimers.activate(self)
        if not InCombatLockdown() and TotemTimers_Settings.HideEnhanceCDsOOC then ll:Hide() end
    end
    ll:Anchor(es, "LEFT")
    ll.button.icons[1]:SetAlpha(1)
    cds[3] = ll
    ll.reverseAlpha = true
	ll.RangeCheck = SpellNames[SpellIDs.LavaLash]
	ll.ManaCheck = SpellNames[SpellIDs.LavaLash]
   
    maelstrom:SetParent(es.button)
    maelstrom:SetPoint("TOP", es.button, "BOTTOM", 0, -5)
    maelstrom:Show()
    maelstrom.icon = getglobal("TotemTimers_MaelstromBarIcon")
    maelstrom.background = getglobal("TotemTimers_MaelstromBarBackground")
    maelstrom.icon:SetTexture(SpellTextures[SpellIDs.Maelstrom])
    maelstrom.icon:Show()
    maelstrom.background:Show()
    maelstrom.background:SetValue(1)
    maelstrom.background:SetWidth(118)
    maelstrom.background:SetStatusBarColor(1, 0, 0, 0.1)
    maelstrom:SetWidth(118)
    maelstrom:SetScript("OnEvent", TotemTimers_MaelstromEvent)
    maelstrom:SetScript("OnUpdate", TotemTimers_MaelstromBarUpdate)
    maelstrom.text = getglobal("TotemTimers_MaelstromBarTime")
    TotemTimers.maelstrom = maelstrom
    TotemTimers.maelstrombutton = maelstrombutton
    TotemTimers.EnhanceCDEvents(stormstrike.button, "PLAYER_REGEN_ENABLED")
    maelstrombutton:SetWidth(118)
    maelstrombutton:SetHeight(maelstrom.background:GetHeight())
    maelstrombutton:SetPoint("CENTER", maelstrom, "CENTER")
    maelstrombutton:SetNormalTexture(nil)
    maelstrombutton:SetHighlightTexture("Interface\\AddOns\\TotemTimers\\MaelstromHilight")
    maelstrombutton:SetPushedTexture("Interface\\AddOns\\TotemTimers\\MaelstromPushed")
    maelstrombutton:SetAttribute("*type*", "spell")
    maelstrombutton:RegisterForClicks("LeftButtonUp", "RightButtonUp")
    if not InCombatLockdown() and TotemTimers_Settings.HideEnhanceCDsOOC then 
        maelstrom:Hide()
    end
end

function TotemTimers_LayoutEnhanceCDs()
    local vertdist = not (TotemTimers_Settings.TimersOnButtons or TotemTimers_Settings.CDTimersOnButtons) and TotemTimers_Settings.EnhanceCDsTimeHeight or 0
    maelstrom:SetPoint("TOP", es.button, "BOTTOM", 0, -vertdist-5)
end

function TotemTimers_ActivateEnhanceCDs()
    for k,v in pairs(cds) do
        v:activate()
    end
    maelstrom:RegisterEvent("UNIT_AURA")
end

function TotemTimers_DeactivateEnhanceCDs()
    for k,v in pairs(cds) do
        v:deactivate()
    end
    maelstrom:UnregisterEvent("UNIT_AURA")
end

local incombat = false

function TotemTimers.EnhanceCDEvents(self, event, ...)
    local settings = TotemTimers_Settings
    if event == "SPELL_UPDATE_COOLDOWN" then 
        for i = 1,#cds do
             if AvailableSpells[spells[i]] then
                local start, duration, enable = GetSpellCooldown(spells[i])
				if (not start and not duration) or (duration <= 1.5 and not incombat) then
					cds[i]:stop(1)					
                else
                    if duration == 0 then
                        cds[i]:stop(1)
                    elseif duration > 2 and cds[i].timers[1]<=0 then
                        cds[i]:start(1,start+duration-floor(GetTime()),duration)
                    end
                    CooldownFrame_SetTimer(cds[i].button.cooldown, start, duration, enable)
                end 
            end
        end    
    elseif event == "PLAYER_REGEN_ENABLED" then
        if settings.HideEnhanceCDsOOC then
            for i=1,3 do cds[i]:Hide() end
            maelstrom:Hide()
        end
        for i=1,#cds do
            cds[i].button.icons[1]:SetAlpha(settings.EnhanceCDsOOCAlpha)
            --cds[i].button.cooldown:SetAlpha(0.1)
            cds[i].button.hotkey:SetAlpha(settings.EnhanceCDsOOCAlpha)
        end
        maelstrom.icon:SetAlpha(settings.EnhanceCDsOOCAlpha)
        incombat = false
    elseif event == "PLAYER_REGEN_DISABLED" then
        if not maelstrom:IsVisible() then
            for i=1,3 do cds[i]:Show() end
            maelstrom:Show()
        end
        for i=1,#cds do
            if cds[i].timers[1] <= 0 then cds[i].button.icons[1]:SetAlpha(1) end
            --cds[i].button.cooldown:SetAlpha(1)
            cds[i].button.hotkey:SetAlpha(1)
        end
        maelstrom.icon:SetAlpha(1)
        incombat = true
    end
end

local maelstromname = SpellNames[SpellIDs.Maelstrom]

function TotemTimers_MaelstromEvent(self, event, ...) 
    if  select(1,...) == "player"  then
        local name,_,_,count,_,duration,endtime = UnitBuff("player", maelstromname)
        self:SetValue(count or 0)
        self.text:SetText(tostring(count or ""))
        self.count = count
        if count then
            self:SetStatusBarColor(5-count, -0.33+count*0.33, 0, 1.0)
            self.background:SetStatusBarColor(1-count*0.2, count*0.2, 0, 0.1)
        else
            self:SetStatusBarColor(1, 0, 0, 1.0)
            self.background:SetStatusBarColor(1, 0, 0, 0.1)
        end
	end
end

function TotemTimers_MaelstromBarUpdate(self, ...)
    if self.count == 5 then
        self:SetStatusBarColor(1-BuffFrame.BuffAlphaValue, BuffFrame.BuffAlphaValue, 0, 1)
    end
end
