--NAME_PLATE_UNIT_ADDED NAME_PLATE_UNIT_REMOVED
--TSU
function(allstates, e, unit)
	if e == "NAME_PLATE_UNIT_ADDED" then
        if UnitExists(unit) then
            if UnitIsPlayer(unit) then
                local name = UnitName(unit)
                allstates[unit] = {
                    show = true,
                    changed = true,
                    unit = unit,
                    name = name,
                    guild = GetGuildInfo(unit),
                    rank = select(2, GetGuildInfo(unit)),
                }
                return true
            end
		end
	elseif e == "NAME_PLATE_UNIT_REMOVED" then
		if allstates[unit] then
			allstates[unit] = {
				show = false,
				changed = true,
			}
			return true
		end
	end
end

--ON SHOW
if aura_env.statee.unit then
    if aura_env.statee.name ~= UnitName(aura_env.statee.unit) then return end
    local guild, rank = GetGuildInfo(aura_env.statee.unit)
    if not guild or not rank then return end
    if not aura_env.region.text then
        local text = aura_env.region:CreateFontString(nil, "OVERLAY")
        aura_env.region.text = text
    end
    aura_env.region:SetHeight(128)
    aura_env.region:SetWidth(512)
    aura_env.region.text:SetFont("Interface\\AddOns\\SharedMedia_MyMedia\\font\\iosevka-fixed-medium.ttf", 14, "OUTLINE")
        aura_env.region.text:SetText(guild .. "\n" .. rank)
    aura_env.region.text:SetJustifyH("CENTER")
    aura_env.region.text:SetJustifyV("CENTER")
    if not aura_env.region.texture then
        local texture = aura_env.region:CreateTexture(nil, "OVERLAY")
        aura_env.region.texture = texture
    end
    aura_env.region.texture:SetTexture("Interface\\Addons\\WeakAuras\\PowerAurasMedia\\Auras\\Aura22")
    aura_env.region.texture:SetHeight(192)
    aura_env.region.texture:SetWidth(192)
    local plate = C_NamePlate.GetNamePlateForUnit(aura_env.statee.unit)

    if guild == "Indecisive" then
        aura_env.region.texture:SetVertexColor(1, 0.85, 0, 1)
    elseif guild == "Warforged" then
        aura_env.region.texture:SetVertexColor(0.75, 0.75, 0.75, 1)
    elseif guild == "Sideway" then
        aura_env.region.texture:SetVertexColor(0.8, 0.5, 0.2, 1)
    end

    if plate then
        aura_env.region.texture:ClearAllPoints()
        aura_env.region.texture:SetPoint("CENTER", plate, "TOP", 0, -16)
        aura_env.region.texture:Show()
        aura_env.region.text:ClearAllPoints()
        aura_env.region.text:SetPoint("CENTER", plate, "CENTER", 0, 16)
        aura_env.region.text:Show()
    end
end

--ON HIDE
if aura_env.region.text then
    aura_env.region.text:Hide()
end
if aura_env.region.texture then
    aura_env.region.texture:Hide()
end