--Add level brackets
--Add classification brackets
--[[
/dump WeakAurasSaved.CustomTrash.MeleeMiss
/dump WeakAurasSaved.CustomTrash.CachedLevels
]]
--COMBAT_LOG_EVENT_UNFILTERED PLAYER_FOUND
function(e, ...)
    if e == "COMBAT_LOG_EVENT_UNFILTERED" then
        local se = select(2, ...)
        if se == "SPELL_CAST_SUCCESS" or se == "SWING_DAMAGE" or se == "SWING_MISSED" then
            local guid = select(4, ...)
            local caster = select(5, ...)
            local targetGUID = select(8, ...)
            local targetName = select(9, ...)
            if guid:match("Player") and targetGUID:match("Creature") then
                if not WeakAurasSaved.CustomTrash.CachedLevels[targetName] then
                    WeakAuras.ScanEvents("GET_LEVEL", targetName)
                    return
                else
                    if not WeakAurasSaved.CustomTrash.MeleeMiss[guid] then
                        WeakAurasSaved.CustomTrash.MeleeMiss[guid] = {}
                        WeakAurasSaved.CustomTrash.MeleeMiss[guid].guid = guid
                        WeakAurasSaved.CustomTrash.MeleeMiss[guid].name = caster
                        if not WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]] then WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]] = {} end
                        WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]].hits = 0
                        WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]].miss = 0
                        WeakAuras.ScanEvents("ADD_PLAYER", guid)
                    end
                end
                if WeakAurasSaved.CustomTrash.CachedLevels[targetName] and WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]] then
                    if se == "SWING_DAMAGE" then
                        --Massive clusterfuck; table > playerGUID > guid, name, level bracket > hits, misses
                        WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]].hits = WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]].hits + 1
                    elseif se == "SWING_MISSED" then
                        WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]].miss = WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]].miss + 1
                    end
                else
                    if WeakAurasSaved.CustomTrash.CachedLevels[targetName] then
                        WeakAurasSaved.CustomTrash.MeleeMiss[guid][WeakAurasSaved.CustomTrash.CachedLevels[targetName]] = {["hits"] = 0, ["miss"] = 0}
                    else
                        WeakAuras.ScanEvents("GET_LEVEL", targetName)
                    end
                end
                return true
            end
        end
    elseif e == "PLAYER_FOUND" then
        local guid, class, spec = ...
        if WeakAurasSaved.CustomTrash.MeleeMiss[guid] then
            WeakAurasSaved.CustomTrash.MeleeMiss[guid].class = class
            WeakAurasSaved.CustomTrash.MeleeMiss[guid].spec = spec
        end
    end
end

--INIT
if not WeakAurasSaved.CustomTrash.MeleeMiss then WeakAurasSaved.CustomTrash.MeleeMiss = {} end
aura_env.classColor = function(class)
    if class == 6 then return "\124cFFC41F3B" elseif
    class == 12 then return "\124cFFA330C9" elseif
    class == 11 then return "\124cFFFF7D0A" elseif
    class == 3 then return "\124cFFABD473" elseif
    class == 8 then return "\124cFF40C7EB" elseif
    class == 10 then return "\124cFF00FF96" elseif
    class == 2 then return "\124cFFF58CBA" elseif
    class == 5 then return "\124cFFFFFFFF" elseif
    class == 4 then return "\124cFFFFF569" elseif
    class == 7 then return "\124cFF0070DE" elseif
    class == 9 then return "\124cFF8787ED" elseif
    class == 1 then return "\124cFFC79C6E" else
    return "\124cFF000000" end
end

--DISPLAY
function()
    local output = ""
    for k, v in pairs(WeakAurasSaved.CustomTrash.MeleeMiss) do
        if v.class then
            output = output .. format("%s%s (%s) ", aura_env.classColor(v.class), v.name, v.spec)
            for i = 110, 115 do
                if v[i] and v[i].hits > 0 then
                    v[i].ratio = v[i].miss / v[i].hits
                    output = output .. format("[%d] %d / %d (%4.2f%%)", i, v[i].miss, v[i].hits, v[i].ratio * 100) .. "\n"
                end
            end
            if v[-1] and v[-1].hits > 0 then
                v[-1].ratio = v[-1].miss / v[-1].hits
                output = output .. format("[%d] %d / %d (%4.2f%%)", i, v[-1].miss, v[-1].hits, v[-1].ratio * 100) .. "\n"
            end
            output = output .. "\n"
        end
    end
    return output
end
