--[[
TODO:
Add interrupts / stuns / breaks by looking at friendly spells cast on the GUID's
Add support for default UI (!!!)
Mob deaths remove namepaltes, handle glow maybe?
Rework for GetSpellInfo instead of GetCastingInfo
 if no namepalte found GetSpellinfo!!!!!! BIG IQ
Watch for damage on avengers instead of cast
]]

--TSU
--COMBAT_LOG_EVENT_UNFILTERED HIDE_ALLSTATES_CAST NAME_PLATE_UNIT_REMOVED NAME_PLATE_UNIT_ADDED
function(allstates, e, ...)
    local scan = false
    local casterGUID, caster, spell, se, interrupted = false
    if e == "COMBAT_LOG_EVENT_UNFILTERED" then
        se = select(2, ...)
        if se == "SPELL_CAST_START" or se == "SPELL_CAST_SUCCESS" then
            local casterFlags = select(6, ...)
            if (bit.band(casterFlags, 0x00000840) == 0x00000840) == true then --Hostile NPC
                casterGUID = select(4, ...)
                caster = select(5, ...)
                spell = select(13, ...)
                scan = true
            end
        elseif e == "SPELL_CAST_FAILED" then
            if (bit.band(casterFlags, 0x00000840) == 0x00000840) == true then --Hostile NPC
                casterGUID = select(4, ...)
                if allstates[casterGUID] then
                    allstates[casterGUID].show = true
                    allstates[casterGUID].changed = true
                    allstates[casterGUID].progressType = "static"
                    allstates[casterGUID].value = 1
                    allstates[casterGUID].total = 1
                    C_Timer.After(1.5, function() WeakAuras.ScanEvents("HIDE_ALLSTATES_CAST", casterGUID) end)
                end
            end
        end
    elseif e == "HIDE_ALLSTATES_CAST" then
        local casterGUID = ...
        if allstates[casterGUID] then
            allstates[casterGUID].show = false
            allstates[casterGUID].changed = true
            return true
        end
    elseif e == "NAME_PLATE_UNIT_REMOVED" then
        local u = ...
        local uGUID = UnitGUID(u)
        --print(uGUID, "nameplate removed")
        if allstates[uGUID] then
            aura_env.glow(u, 0)
        end
    elseif e == "NAME_PLATE_UNIT_ADDED" then
        local u = ...
        local uGUID = UnitGUID(u)
        --print(uGUID, "nameplate added")
        if allstates[uGUID] then
            aura_env.glow(u, 1)
        end
    end
    if scan == true then
        for i = 1, 40 do
            local unit = "nameplate" .. i
            if UnitExists(unit) then
                local unitGUID = UnitGUID(unit)
                if unitGUID == casterGUID then
                    if se and (se == "SPELL_CAST_START" or se == "SPELL_CAST_SUCCESS") then
                        if UnitCastingInfo(unit) then
                            if aura_env.spells[spell] then
                                local _, _, _, texture, startTime, endTime = UnitCastingInfo(unit)
                                local remainingTime = math.floor(endTime - (GetTime() * 1000))
                                local castTime = endTime - startTime
                                local castTimeDisplay = math.floor((castTime / 1000) * 10) / 10
                                local castDisplay = math.floor((remainingTime / 1000) * 10) / 10
                                local target = select(9, ...)
                                print(select(8, ...), select(9, ...), select(10, ...))
                                --print(endTime / 1000, castTimeDisplay, GetTime())
                                --print(unit)
                                --print("Adding", casterGUID, "to allstates")
                                if castTime > 1000 then
                                    allstates[casterGUID] = {
                                        changed = true,
                                        show = true,
                                        resort = true,
                                        index = remainingTime,
                                        icon = texture,
                                        
                                        progressType = "timed",
                                        expirationTime = endTime / 1000,
                                        duration = castTimeDisplay,
                                        autoHide = true,
                                        
                                        name = caster,
                                        spell = spell,
                                        unitGUID = unitGUID,
                                        castDisplay = castDisplay,
                                        castTimeDisplay = castTimeDisplay,
                                        unit = unit,
                                    }
                                    if aura_env.instructions[spell] ~= true then
                                        allstates[casterGUID].instruction = aura_env.instructions[spell]
                                    else
                                        allstates[casterGUID].instruction = ""
                                    end
                                end
                            end
                        end
                    elseif se == "SPELL_CAST_SUCCESS" then
                        if aura_env.spells[spell] then
                            if not allstates[casterGUID] then
                                allstates[casterGUID] = {
                                    changed = true,
                                    show = true,
                                    resort = true,
                                    index = 0,
                                    icon = texture,
                                    
                                    progressType = "static",
                                    value = 0,
                                    total = 1,
                                    
                                    name = caster,
                                    spell = spell,
                                    unitGUID = unitGUID,
                                    unit = unit,
                                }
                                if aura_env.instructions[spell] ~= true then
                                    allstates[casterGUID].instruction = aura_env.instructions[spell]
                                else
                                    allstates[casterGUID].instruction = ""
                                end
                                C_Timer.After(1, function() WeakAuras.ScanEvents("HIDE_ALLSTATES_CAST", casterGUID) end)
                            end
                        end
                    end
                end
            else
                break
            end
        end
        return true
    end
end

--[[
DisplayText
Left: %total
Right: %castDisplay / %castTimeDisplay
]]

--ON SHOW?
PlaySoundFile("Interface\\AddOns\\WeakAuras\\Media\\Sounds\\RobotBlip.ogg", "Master")
if aura_env.statee.unit then
    aura_env.glow(aura_env.statee.unit, 1)
end

--ON HIDE
if aura_env.statee.unit then
    aura_env.glow(aura_env.statee.unit, 0)
end

--INIT
aura_env.spells = {
    --["Emblazoned Swipe"] = true,
    ["Surge"] = true,
    ["Stygian Blast"] = true,
    ["Fragment of Despair"] = true,
    ["Ruinous Strike"] = true,
    ["Howling Dark "] = true,
    
    ["Dark Mending"] = true,
    ["Sacrifice Soul"] = true,
    ["Arrow Barrage"] = true,
    ["Knife Dance"] = true,
    ["Bonebreaking Strike"] = true,
    ["Brutal Assault"] = true,
    ["Indigestion"] = true,
    ["Coup de Grace"] = true,
    ["Raven's Dive"] = true,
    
    ["Shadow Wall"] = true,
    ["Demonic Mending"] = true,
    ["Devour Magic"] = true,
    ["Shadow Wave"] = true,
    ["Lumbering Crash"] = true,
    ["Vile Roots"] = true,
    ["Alluring Aroma"] = true,
    ["Blistering Rain"] = true,
    ["Floral Fulmination"] = true,
    ["Throw Arcane Tome"] = true,
    ["Throw Frost Tome"] = true,
    ["Throw Silence Tome"] = true,
    ["Summon Monstrosity"] = true,
    ["Blinding Glare"] = true,
    ["Stifling Satire"] = true,
    ["Beguiling Biography"] = true,
    ["Chaotic Energy"] = true,
    
    ["Hinder"] = true,
    ["Quelling Strike"] = true,
    ["Nightfall Orb"] = true,
    ["Seal Magic"] = true,
    ["Suppress"] = true,
    ["Charging Station"] = true,
    ["Bewitch"] = true,
    ["Fel Detonation"] = true,
    
    ["Unnerving Screech"] = true,
    ["Star Shower"] = true,
    ["Maddening Roar"] = true,
    ["Tormenting Eye"] = true,
    
    ["Storm"] = true,
    ["Arc Lightning"] = true,
    ["Mighty Slam"] = true,
    ["Thundering Stomp"] = true,
    ["Rejuvenating Waters"] = true,
    ["Lightning Prod"] = true,
    ["Restoration"] = true,
    ["Polymorph: Fish"] = true,
    
    ["Etch"] = true,
    ["Holy Radiance"] = true,
    ["Cleansing Flames"] = true,
    ["Unruly Yell"] = true,
    
    ["Soul Leech"] = true,
    ["Terrifying Wail"] = true,
    ["Pennies From Heaven"] = true,
    ["Spotlight"] = true, --Kite away
    ["Poetry Slam"] = true, --Interrupt
    ["Final Curtainaw"] = true, --Run ay
    ["Flashlight "] = true, --Face away
    ["Shadow Rejuvenation"] = true,
    ["Oath of Fealty"] = true,
    
    ["Unstable Energy"] = true,
    ["Fel Bomb"] = true,
    
    ["Bone Chilling Scream"] = true,
    ["Void Snap"] = true,
    ["Brackwater Barrage"] = true,
    ["Corrupted Bellow"] = true,
    
    ["Stone Gaze"] = true,
    ["Piercing Shards"] = true,
    ["Shatter"] = true,
    ["Stone Bolt"] = true,
    
    ["Decimate"] = true,
    ["Dread Screech"] = true,
    ["Howling Dark"] = true,
    ["Eternal Twilight"] = true,
    ["Fragment of Despair"] = true,
    
    ["Prophecies of Doom"] = true,
    ["Siphon Essence"] = true,
    ["Torment"] = true,
    ["Demonic Ascension"] = true,
    ["Arcane Reconstitution"] = true,
    ["Devour"] = true,
    
    ["Nightmares"] = true,
    ["Unleash Fury"] = true,
    ["Drain"] = true,
    ["Scorching"] = true,
    ["Meteor"] = true,
    ["Detonate"] = true,
    
    ["Surge"] = true,
    ["Stygian Blast"] = true,
    ["Fragment of Despair"] = true,
    ["Ruinous Strike"] = true,
    ["Howling Dark"] = true,
    ["Blood Assault"] = true,
    ["Dread Screech"] = true,
    ["Void Trap"] = true,
    ["Ravaging Darkness"] = true,
}
aura_env.instructions = {
    --["Emblazoned Swipe"] = "TEST",
    
    ["Dark Mending"] = "Interrupt",
    ["Sacrifice Soul"] = true,
    ["Arrow Barrage"] = true,
    ["Knife Dance"] = true,
    ["Bonebreaking Strike"] = "Dodge",
    ["Brutal Assault"] = true,
    ["Indigestion"] = true,
    ["Coup de Grace"] = true,
    ["Raven's Dive"] = "Dodge",
    
    ["Shadow Wall"] = true,
    ["Demonic Mending"] = "Interrupt",
    ["Devour Magic"] = true,
    ["Shadow Wave"] = true,
    ["Lumbering Crash"] = "Stun",
    ["Vile Roots"] = true,
    ["Alluring Aroma"] = "Stun",
    ["Blistering Rain"] = "Stun",
    ["Floral Fulmination"] = true,
    ["Throw Arcane Tome"] = "Dodge",
    ["Throw Frost Tome"] = "Dodge",
    ["Throw Silence Tome"] = "Dodge",
    ["Summon Monstrosity"] = "Stun/Kill",
    ["Blinding Glare"] = "Face away",
    ["Stifling Satire"] = true,
    ["Beguiling Biography"] = true,
    ["Chaotic Energy"] = true,
    
    ["Hinder"] = "Interrupt",
    ["Quelling Strike"] = "Dodge",
    ["Nightfall Orb"] = "Interrupt",
    ["Seal Magic"] = "Interrupt",
    ["Suppress"] = "Interrupt",
    ["Charging Station"] = "Interrupt",
    ["Bewitch"] = "Interrupt",
    ["Fel Detonation"] = true,
    
    ["Unnerving Screech"] = "Interrupt",
    ["Star Shower"] = "Interrupt",
    ["Maddening Roar"] = true,
    ["Tormenting Eye"] = true,
    
    ["Storm"] = "Interrupt",
    ["Arc Lightning"] = "Interrupt",
    ["Mighty Slam"] = "Stun",
    ["Thundering Stomp"] = "Interrupt",
    ["Rejuvenating Waters"] = "Interrupt",
    ["Lightning Prod"] = "Interrupt",
    ["Restoration"] = "Interrupt",
    ["Polymorph: Fish"] = "Interrupt",
    
    ["Etch"] = "Interrupt",
    ["Holy Radiance"] = "Interrupt",
    ["Cleansing Flames"] = true,
    ["Unruly Yell"] = "Interrupt",
    
    ["Soul Leech"] = "Interrupt",
    ["Terrifying Wail"] = "Interrupt",
    ["Pennies From Heaven"] = true,
    ["Spotlight"] = "Move", --Kite away
    ["Poetry Slam"] = "Interrupt", --Interrupt
    ["Final Curtainaw"] = "Move", --Run ay
    ["Flashlight "] = "Face away", --Face away
    ["Shadow Rejuvenation"] = true,
    ["Oath of Fealty"] = true,
    
    ["Unstable Energy"] = true,
    ["Fel Bomb"] = "Soak",
    
    ["Bone Chilling Scream"] = "Interrupt",
    ["Void Snap"] = "Interrupt",
    ["Brackwater Barrage"] = "Dodge",
    ["Corrupted Bellow"] = true,
    
    ["Stone Gaze"] = "Interrupt",
    ["Piercing Shards"] = true,
    ["Shatter"] = true,
    ["Stone Bolt"] = "Interrupt",
    
    ["Decimate"] = true,
    ["Dread Screech"] = true,
    ["Howling Dark"] = true,
    ["Eternal Twilight"] = true,
    ["Fragment of Despair"] = true,
    
    ["Prophecies of Doom"] = "Stun",
    ["Siphon Essence"] = true,
    ["Torment"] = true,
    ["Demonic Ascension"] = "Interrupt",
    ["Arcane Reconstitution"] = true,
    ["Devour"] = true,
    
    ["Nightmares"] = true,
    ["Unleash Fury"] = true,
    ["Drain"] = true,
    ["Scorching"] = true,
    ["Meteor"] = true,
    ["Detonate"] = true,
    
    ["Surge"] = "Interrupt",
    ["Stygian Blast"] = "Interrupt",
    ["Fragment of Despair"] = "Soak",
    ["Ruinous Strike"] = "Dodge",
    ["Howling Dark"] = "Interrupt",
    ["Blood Assault"] = "Stun",
    ["Dread Screech"] = "Interrupt",
    ["Void Trap"] = true,
    ["Ravaging Darkness"] = true,
}
aura_env.glow = function(unit, s)
    local nameplate = C_NamePlate.GetNamePlateForUnit(unit)
    if nameplate and s == 1 then
        ActionButton_ShowOverlayGlow(nameplate.UnitFrame.HealthBar)
    else
        ActionButton_HideOverlayGlow(nameplate.UnitFrame.HealthBar)
    end
end
