--1
function(allstates)
    for _, v in pairs(allstates) do
        v.show = false;
        v.changed = true;
    end
    for unit in aura_env.GroupMembers() do
        if UnitIsVisible(unit) then
            local role = UnitGroupRolesAssigned(unit)
            --local icon = aura_env.roles[role] or ""
            local name = string.sub(UnitName(unit),1,6)
            local _, class = UnitClass(unit)
            local _,_,_,str = GetClassColor(class)
            name = string.format("|c%s%s|r", str, name)
            local inside = WA_GetUnitDebuff(unit, GetSpellInfo(236283)) and "inside"
            local dead = UnitIsDeadOrGhost(unit) and "DEAD"
            local flag = inside or ""
            local torment = UnitPower(unit, 10)
            
            local index = (100 - torment)
            if role == "TANK" then
                index = index - 1000
            elseif role == "HEALER" then
                index = index - 500
            end
            if inside then index = index - 100 end
            
            allstates[unit] = allstates[unit] or {};
            local state = allstates[unit];
            state.index = index
            state.resort = true
            state.ID = unit
            state.show = true;
            state.changed = true;
            state.name = dead or name;
            state.progressType = "static";
            state.value = torment
            state.total = 100
            state.flag = flag
        end
    end
    
    for i = 1,3 do
        local icon = i == 3 and aura_env.roles["TANK"] 
        or i == 2 and aura_env.roles["HEALER"] 
        or aura_env.roles["DAMAGER"]
        local name = i == 3 and "TANKS" or i == 2 and "HEALERS" or "DPS"
        local col = i == 3 and "FF8C00" or i == 2 and "7FFF00" or "DC143C"
        local flag = string.format("%s |cFF%s%s|r %s", icon, col, name, icon)
        
        allstates["spacer"..i] = allstates["spacer"..i] or {};
        local state = allstates["spacer"..i];
        state.index = i * -400
        state.resort = true
        state.ID = "spacer"
        state.show = true;
        state.changed = true;
        state.name = ""
        state.progressType = "static";
        state.value = 0
        state.total = 100
        state.flag = flag
    end
    
    for k, v in pairs(allstates) do
        if v.ID == "spacer" then
            local region = WeakAuras.GetRegion(aura_env.id, k)
            region.timer:ClearAllPoints()
            region.timer:SetPoint("CENTER", region.bar, "CENTER")
            region.bar.bg:SetVertexColor(1,1,1,0)
        end
    end
    
    return true
end


--2
function aura_env.GroupMembers(reversed, forceParty)
    local unit  = (not forceParty and IsInRaid()) and 'raid' or 'party'
    local numGroupMembers = forceParty and GetNumSubgroupMembers()  or GetNumGroupMembers()
    local i = reversed and numGroupMembers or (unit == 'party' and 0 or 1)
    return function()
        local ret 
        if i == 0 and unit == 'party' then 
            ret = 'player'
        elseif i <= numGroupMembers and i > 0 then
            ret = unit .. i
        end
        i = i + (reversed and -1 or 1)
        return ret
    end
end

aura_env.roles = {
    ["TANK"] = "|TInterface\\LFGFrame\\UI-LFG-ICON-PORTRAITROLES:0:0:0:0:64:64:0:19:22:41|t",
    ["HEALER"] = "|TInterface\\LFGFrame\\UI-LFG-ICON-PORTRAITROLES:0:0:0:0:64:64:20:39:1:20|t",
    ["DAMAGER"] = "|TInterface\\LFGFrame\\UI-LFG-ICON-PORTRAITROLES:0:0:0:0:64:64:20:39:22:41|t"
}


--3
function()
    local r,g,b = 0,1,0
    if aura_env and aura_env.statee then
        if aura_env.statee.value > 40 then
            r,g,b =  1,1,0
        end
        if aura_env.statee.value > 80 then
            r,g,b = 1,0.5,0
        end
        if aura_env.statee.value > 95 then
            r,g,b = 1,0,0
        end
        local region = WeakAuras.GetRegion(aura_env.id, aura_env.statee.ID)
        region.text:ClearAllPoints()
        region.text:SetPoint("RIGHT", region.bar, "LEFT", -3,1)
        region.text:SetJustifyH("RIGHT")
        
    end
    return r, g, b, 1
end


--4
function(_,_,prog)
    if aura_env.statee and aura_env.statee.ID == "spacer" then
        return ""
    else 
        return prog
    end
end