--Text?
function()   
    if not WeakAuras.IsOptionsOpen() then
        WeakAuras.regions[aura_env.id].region.text2:SetText(string.format("Shuffle in %i casts", 10-aura_env.casts))  
        -- return math.floor(((aura_env.rate - aura_env.success) / (10 - aura_env.casts) * 100) + 0.5) .. "%" 
        return aura_env.rate - aura_env.success
    else
        return "0"
    end
end


--ENCOUNTER_START COMBAT_LOG_EVENT_UNFILTERED ENCOUNTER_END
function(event, ...) 
    if event == "ENCOUNTER_START" then
        local id, id2 = ...
        if not id then return end
        --print("COMBAT STARTED")
        
        if GetNumGroupMembers() <= 5 then return end
        local specID = GetSpecializationInfo(GetSpecialization())
        if not aura_env.procRate[specID] then return end
        aura_env.reset()  
        aura_env.rate = aura_env.procRate[specID]
        return true
        
    elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then
        local _, type, _, source, _, _, _, dest, _, _, _, spell, name = ...
        
        if type == "SPELL_CAST_SUCCESS" and source == aura_env.player and aura_env.spells[spell] then
            if not aura_env.checkBuff() then
                
                aura_env.casts = aura_env.casts + 1
            end
            
            if aura_env.casts >= 10 then 
                aura_env.reset()
            end
        elseif (type == "SPELL_AURA_APPLIED" and
            dest == aura_env.player and
            (spell >= 242459 and spell <= 242474)) then
            -- Because Blizzard needed 8 auras for one proc.
            
            aura_env.success = aura_env.success + 1
            if aura_env.casts == 0 then
                aura_env.success = 0
            end
            
            
        end
    end    
end

--Untrigger
function(event, ...)
    if event == "ENCOUNTER_END" then
        local id, id2 = ...
        if not id then return end
        
        print("ENCOUNTER ENDED")
        print(...)
        return true
        
    end
    
    
end

--Name
function()
    return string.format("Shuffle in %i casts", 10-aura_env.casts)
end

--Every Frame
function()
    return (aura_env.rate - aura_env.success) == 0 and not aura_env.checkBuff()
    
end

--INIT
--[[
Author: Voulk (Discord: Voulk#1858)
Wago Link: X

You can read more on the new deck based RNG system here:
https://questionablyepic.com/deck-RNG


]]



aura_env.player = UnitGUID"player"
aura_env.buffID = 0
aura_env.rate = 0
aura_env.reset = function()
    aura_env.deckSize = 10
    aura_env.success = 0
    aura_env.casts = 0
end
aura_env.reset()

aura_env.spells = 
{
    [1064] = true,   -- chain heal
    [48438] = true,  -- wild growth
    [596] = true,    -- prayer of healing
    [85222] = true,  -- light of dawn
    [194509] = true, -- pw:r
    [191837] = true  -- essence font
}

aura_env.procRate = 
{ 
    [105] = 5, -- rdruid
    [270] = 5, -- mw
    [256] = 6, -- disc
    [257] = 4, -- holy
    [264] = 4, -- rsham
    [65]  = 4  -- hpal
}



-- Checks if the aura is active. If it is, we had a proc.
aura_env.checkBuff = function()
    
    local _, _, _, _, _, duration, expTime = UnitBuff("player", "Ocean's Embrace")
    if duration then   
        return true
    end
    
end

