--PLAYER_REGEN_DISABLED PLAYER_REGEN_ENABLED UPDATE
-- look for auras every frame
-- or maybe look for combatlog ?
-- get time when each aura is applied/expires
-- calculate uptime based on the log ?
-- alternatively sample each frame and add up # of frames aura was active and # of frames combat was ongoing
-- bad solution though
function(e)
	if e == "PLAYER_REGEN_DISABLED" then
		local aura_env = aura_env
        aura_env.ticker = C_Timer.NewTicker(1, function() WeakAuras.ScanEvents("UPDATE") end)
        aura_env.startCombat = debugprofilestop()
        aura_env.buffs = {}
        aura_env.debuffs = {}
		return true
	elseif e == "PLAYER_REGEN_ENABLED" then
		if aura_env.ticker then aura_env.ticker:Cancel() end
    elseif e == "UPDATE" then return true end
end

--CLEU
--COMBAT_LOG_EVENT_UNFILTERED
function(e)
    if not aura_env.buffs then aura_env.buffs = {} end
    if not aura_env.debuffs then aura_env.debuffs = {} end

    local timestamp, subEvent, _, _, caster, _, _, _, target = CombatLogGetCurrentEventInfo()
    -- print(CombatLogGetCurrentEventInfo())

    -- if "buff" is selected in the menu and
    -- there has been an aura applied or removed and
    -- the aura has been cast by the player on the player then
    -- do fancy data stuff pls
    if InCombatLockdown() ~= false then
        local targetUnit = "boss1"
        if UnitExists(targetUnit) == false then targetUnit = "target" end
        if (aura_env.config.menu == 1 or aura_env.config.menu == 3) and (subEvent == "SPELL_AURA_REMOVED" or subEvent == "SPELL_AURA_APPLIED") and (caster == target and caster == UnitName("player")) then
            local spell = select(12, CombatLogGetCurrentEventInfo())
            print("SPell", spell)
            if not aura_env.buffs[spell] then 
                aura_env.buffs[spell] = {
                    ["timeActive"] = 0,
                    ["timeInactive"] = 0,
                    ["active"] = false,
                    ["lastSwitch"] = debugprofilestop(),
                }
                -- print("OPA; NEW SPELL")
            end
            if subEvent == "SPELL_AURA_APPLIED" and aura_env.buffs[spell] then
                aura_env.buffs[spell].active = true
                aura_env.buffs[spell].timeInactive = aura_env.buffs[spell].timeInactive + debugprofilestop() - aura_env.buffs[spell].lastSwitch
                aura_env.buffs[spell].lastSwitch = debugprofilestop()
            elseif subEvent == "SPELL_AURA_REMOVED" and aura_env.buffs[spell] then
                aura_env.buffs[spell].active = false
                aura_env.buffs[spell].timeActive = aura_env.buffs[spell].timeActive + debugprofilestop() - aura_env.buffs[spell].lastSwitch
                aura_env.buffs[spell].lastSwitch = debugprofilestop()
            end
        elseif (aura_env.config.menu == 2 or aura_env.config.menu == 3) and (subEvent == "SPELL_AURA_REMOVED" or subEvent == "SPELL_AURA_APPLIED") and (caster == UnitName("player") and target == UnitName(targetUnit)) then
            local spell = select(12, CombatLogGetCurrentEventInfo())
            if not aura_env.debuffs[spell] then 
                aura_env.debuffs[spell] = {
                    ["timeActive"] = 0,
                    ["timeInactive"] = 0,
                    ["active"] = false,
                    ["lastSwitch"] = debugprofilestop(),
                }
                -- print("OPA; NEW SPELL")
            end
            if subEvent == "SPELL_AURA_APPLIED" and aura_env.debuffs[spell] then
                aura_env.debuffs[spell].active = true
                aura_env.debuffs[spell].timeInactive = aura_env.debuffs[spell].timeInactive + debugprofilestop() - aura_env.debuffs[spell].lastSwitch
                aura_env.debuffs[spell].lastSwitch = debugprofilestop()
            elseif subEvent == "SPELL_AURA_REMOVED" and aura_env.debuffs[spell] then
                aura_env.debuffs[spell].active = false
                aura_env.debuffs[spell].timeActive = aura_env.debuffs[spell].timeActive + debugprofilestop() - aura_env.debuffs[spell].lastSwitch
                aura_env.debuffs[spell].lastSwitch = debugprofilestop()
            end
        end
    end
    
    -- debug print pls
    for k,v in pairs(aura_env.buffs) do
        -- print(k,v)
        if type(v) == "table" then
            for k2,v2 in pairs(v) do
                -- print(k2,v2)
                if type(v2) == "table" then
                    for k3,v3 in pairs(v2) do
                        -- print(k3,v3)
                    end
                end
            end
        end
    end

end 

--DISPLAY
function()
    aura_env.combatTime = (debugprofilestop() - aura_env.startCombat) / 1000
    local output = aura_env.formatTime(aura_env.combatTime) .. "\n"
    aura_env.getColor(1)
    for k,v in pairs(aura_env.buffs) do

        -- DEBUG
        -- print(k)
        -- for k2,v2 in pairs(v) do
            -- print(k2, v2)
        -- end
        -- print("DEBUG TIME", debugprofilestop())
        -- print("END" .. "\n")

        local totalActiveTime = 0
        if v.active == true then
            totalActiveTime = totalActiveTime + debugprofilestop() - v.lastSwitch + v.timeActive
        else
            totalActiveTime = totalActiveTime + v.timeActive
        end
        
        output = string.format("%s %s%20s %10s %10s\n", output, aura_env.getColor(1), GetSpellInfo(k), aura_env.formatTime(totalActiveTime / 1000), string.format("%.2f%%", ((totalActiveTime / 1000) / aura_env.combatTime) * 100))
        -- output = output .. aura_env.getColor(1) .. GetSpellInfo(k) .. " " .. aura_env.formatTime(totalActiveTime / 1000) .. " " .. string.format("%.2f%%", ((totalActiveTime / 1000) / aura_env.combatTime) * 100) .. "\n"
    end

    for k,v in pairs(aura_env.debuffs) do

        -- DEBUG
        -- print(k)
        for k2,v2 in pairs(v) do
            -- print(k2, v2)
        end
        -- print("DEBUG TIME", debugprofilestop())
        -- print("END" .. "\n")

        local totalActiveTime = 0
        if v.active == true then
            totalActiveTime = totalActiveTime + debugprofilestop() - v.lastSwitch + v.timeActive
        else
            totalActiveTime = totalActiveTime + v.timeActive
        end
        output = output .. aura_env.getColor(2)
        if k == 233490 or (k >= 233496 and k <= 233499) then
            if k == 233490 then 
                output = output .. GetSpellInfo(k) .. " 1"
            elseif k >= 233496 and k <= 233499 then
                output = output .. GetSpellInfo(k) .. " " .. (k % 233496) + 2
            end
        else
            output = output .. GetSpellInfo(k)
        end
        output = output .. " " .. aura_env.formatTime(totalActiveTime / 1000) .. " " .. string.format("%.2f%%", ((totalActiveTime / 1000) / aura_env.combatTime) * 100) .. "\n"
    end
    return output
end

--INIT
aura_env.buffs = {}
aura_env.debuffs = {}
aura_env.combatTime = 0
aura_env.startCombat = 0

aura_env.formatTime = function(time)
	local res, m, s = tonumber(string.format("%d", 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

aura_env.getColor = function(mode)
    -- ff78cb00
    local output = "\124c"
    local colors = {}
    if mode == 1 then
        colors = {aura_env.config.bcolor[4], aura_env.config.bcolor[1], aura_env.config.bcolor[2], aura_env.config.bcolor[3]}
    elseif mode == 2 then
        colors = {aura_env.config.dcolor[4], aura_env.config.dcolor[1], aura_env.config.dcolor[2], aura_env.config.dcolor[3]}
    end
    local a, r, g, b = "00", "00", "00", "00"
    
    a = string.format("%x", colors[1] * 255)
    r = string.format("%x", colors[2] * 255)
    g = string.format("%x", colors[3] * 255)
    b = string.format("%x", colors[4] * 255)

    
    if string.len(a) == 1 then a = "0" .. a end
    if string.len(r) == 1 then r = "0" .. r end
    if string.len(g) == 1 then g = "0" .. g end
    if string.len(b) == 1 then b = "0" .. b end

    output = output .. a .. r .. g .. b
    return output
end