local _, ns = ...
local oUF = ns.oUF or oUF
assert(oUF, "oUF not loaded")

local Update = function(self, event, unit)
    if event == "ARENA_OPPONENT_UPDATE" and unit ~= self.unit then return end
    local specIcon = self.PVPSpecIcon

    local _, instanceType = IsInInstance()
    specIcon.instanceType = instanceType

    if specIcon.PreUpdate then specIcon:PreUpdate(event) end

    if instanceType == "arena" then
        local ID = self.unit:match("arena(%d)") or self:GetID() or 0
        local specID = GetArenaOpponentSpec(tonumber(ID))
        if specID and specID > 0 then
            local _, _, _, icon = GetSpecializationInfoByID(specID)
            specIcon.Icon:SetTexture(icon)
        else
            local _, unitClass, _ = UnitClass(self.unit)
            if unitClass and CLASS_ICON_TCOORDS[unitClass] then
                specIcon.Icon:SetTexture([[INTERFACE\TARGETINGFRAME\UI-Classes-Circles]])
                specIcon.Icon:SetTexCoord(unpack(CLASS_ICON_TCOORDS[unitClass]))
                specIcon.Icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
            else
                specIcon.Icon:SetTexture([[INTERFACE\ICONS\INV_MISC_QUESTIONMARK]])
                specIcon.Icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
            end
        end
    else
        local unitFactionGroup = UnitFactionGroup(self.unit)
        if unitFactionGroup == "Horde" then
            specIcon.Icon:SetTexture([[Interface\Icons\INV_BannerPVP_01]])
            specIcon.Icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
        elseif unitFactionGroup == "Alliance" then
            specIcon.Icon:SetTexture([[Interface\Icons\INV_BannerPVP_02]])
            specIcon.Icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
        else
            local _, unitClass, _ = UnitClass(self.unit)
            if unitClass and CLASS_ICON_TCOORDS[unitClass] then
                specIcon.Icon:SetTexture([[INTERFACE\TARGETINGFRAME\UI-Classes-Circles]])
                specIcon.Icon:SetTexCoord(unpack(CLASS_ICON_TCOORDS[unitClass]))
            else
                specIcon.Icon:SetTexture([[INTERFACE\ICONS\INV_MISC_QUESTIONMARK]])
                specIcon.Icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
            end
        end
    end

    if specIcon.PostUpdate then specIcon:PostUpdate(event) end
end

local Enable = function(self)
    local specIcon = self.PVPSpecIcon
    if specIcon then
        self:RegisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS", Update, true)
        self:RegisterEvent("ARENA_OPPONENT_UPDATE", Update)
        self:RegisterEvent("PLAYER_ENTERING_WORLD", Update, true)

        if not specIcon.Icon then
            specIcon.Icon = specIcon:CreateTexture(nil, "OVERLAY")
            specIcon.Icon:SetAllPoints(specIcon)
            specIcon.Icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
            if specIcon.UseCircle then
                local mask = specIcon:CreateMaskTexture()
                mask:SetTexture("Interface\\CHARACTERFRAME\\TempPortraitAlphaMask", "CLAMPTOBLACKADDITIVE", "CLAMPTOBLACKADDITIVE")
                mask:SetAllPoints(specIcon)
                specIcon.Icon:AddMaskTexture(mask)
            end
        end
        specIcon:Show()
        return true
    end
end

local Disable = function(self)
    local specIcon = self.PVPSpecIcon
    if specIcon then
        self:UnregisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS", Update)
        self:UnregisterEvent("ARENA_OPPONENT_UPDATE", Update)
        self:UnregisterEvent("PLAYER_ENTERING_WORLD", Update)
        specIcon:Hide()
    end
end

oUF:AddElement("PVPSpecIcon", Update, Enable, Disable)
