--DISPLAY
function()
    if aura_env.statee then
        return aura_env.statee.string
    end
end

--COMBAT_LOG_EVENT_UNFILTERED GROUP_ROSTER_UPDATE COOLDOWN_TICKER PLAYER_REGEN_DISABLED PLAYER_REGEN_ENABLED COMBAT_TICK
function(allstates, e, ...)
    --if not aura_env.ticker then
    --    aura_env.ticker = C_Timer.NewTicker(0.05, function() WeakAuras.ScanEvents("COOLDOWN_TICKER") end)
    --end
    --if e == "COOLDOWN_TICKER" then
    --    if allstates[1] then
    --        if allstates[1].time + 2 < GetTime() then
    --            allstates[1].show = false
    --            allstates[1].changed = true
    --            table.remove(allstates, 1)
    --            WeakAuras.ScanEvents("COOLDOWN_TICKER")
    --            return true
    --        end
    --    end
    if e == "COMBAT_LOG_EVENT_UNFILTERED" then
        local se = select(2, ...)
        if not allstates[1] or not allstates[1].string then
            allstates[1] = {
                string = "[" .. aura_env.formatTime(aura_env.combatTime) .. "]",
                show = true,
                changed = true,
            }
        end
        if se == "SPELL_CAST_SUCCESS" then
            local caster = select(5, ...) or 0
            if aura_env.raid[caster] then
                local spell = select(13, ...)
                if aura_env.spells[spell] then
                    allstates[#allstates + 1] = {
                        caster = caster,
                        spell = spell,
                        string = "[" .. aura_env.formatTime(aura_env.combatTime) .. "] " .. caster .. " - " .. spell,
                        class = aura_env.raid[caster],
                        time = GetTime(),
                        show = true,
                        changed = true,
                    }
                    if #allstates > 8 then
                        allstates[2].show = false
                        allstates[2].changed = true
                        table.remove(allstates, 2)
                    end
                    return true
                end
            end
        end
    elseif e == "GROUP_ROSTER_UPDATE" then
        local prefix = "party"
        if IsInRaid() == true then
            prefix = "raid"
        end
        aura_env.raid = {}
        aura_env.raid[UnitName("player")] = select(3, UnitClass("player"))
        for i = 1, GetNumGroupMembers() do
            if UnitName(prefix .. i) and not aura_env.raid[UnitName(prefix .. i)] then
                aura_env.raid[UnitName(prefix .. i)] = select(3, UnitClass(prefix .. i))
                print("YEET", UnitName(prefix .. i), select(3, UnitClass(prefix .. i)))
            end
        end
    elseif e == "COMBAT_TICK" then
        aura_env.combatTime = aura_env.combatTime + 1
        if allstates[1] then
            allstates[1].string = "[" .. aura_env.formatTime(aura_env.combatTime) .. "]"
            allstates[1].changed = true
            allstates[1].show = true
        end
    elseif e == "PLAYER_REGEN_DISABLED" then
        aura_env.combatTime = 0
        for i = 2, 10 do
            if allstates[i] then
                allstates[i].show = false
                allstates[i].changed = true
            end
        end
        if not aura_env.combatTicker then
            aura_env.combatTicker = C_Timer.NewTicker(1, function() WeakAuras.ScanEvents("COMBAT_TICK") end)
        end
    elseif e == "PLAYER_REGEN_ENABLED" then
        if aura_env.combatTicker then
            aura_env.combatTicker:Cancel()
            aura_env.combatTicker = nil
        end
    end
    --DevTools_Dump(aura_env.raid)
    return true
end

--INIT
aura_env.raid = {}
aura_env.combatTime = 0
aura_env.spells = {
    ["Divine Shield"] = true,
    ["Blessing of Spellwarding"] = true,
    ["Blessing of Protection"] = true,
    ["Blessing of Sacrifice"] = true,
    ["Avenging Wrath"] = true,
    ["Aura Mastery"] = true,
    ["Guardian Spirit"] = true,
    ["Divine Hymn"] = true,
    ["Raise Ally"] = true,
    ["Tranquility"] = true,
    ["Stampeding Roar"] = true,
    ["Rebirth"] = true,
    ["Ice Block"] = true,
    ["Deterrence"] = true,
    ["Survival Instincts"] = true,
    ["Anti-Magic Shell"] = true,
    ["Vampiric Blood"] = true,
    ["Revival"] = true,
    ["Life Cocoon"] = true,
    ["Diffuse Magic"] = true,
    ["Lay on Hands"] = true,
    ["Dispersion"] = true,
    ["Evasion"] = true,
    ["Cloak of Shadows"] = true,
    ["Bloodlust"] = true,
    ["Healing Tide Totem"] = true,
    ["Soulstone"] = true,
    ["Rallying Cry"] = true,
    ["Power Word: Barrier"] = true,
    ["Spirit Link Totem"] = true,
    ["Wind Rush Totem"] = true,
    ["Ancestral Guidance"] = true,
    --["Hand of the Protector"] = true,
    --["Healing Rain"] = true,
    --["Shimmer"] = true,
    --["Death and Decay"] = true,
}
aura_env.formatTime = function(time)
    local res, m, s = time, 0, 0
    while res >= 60 do
        m = m + 1
        res = res - 60
    end
    s = res
    if s < 10 then
        s = string.format("0%d", s)
    end
    if type(s) ~= "string" then tostring(s) end
    return string.format("%d:%s", m, s)
end
