local _, private = ...

-- [[ Lua Globals ]]
-- luacheck: globals

-- [[ Core ]]
local Aurora = private.Aurora
local Hook = Aurora.Hook

do --[[ SharedXML\FriendsFrame.lua ]]
    local wowFormat = "%s, ".. _G.LEVEL_ABBR .. " %d"
    local bnetFriendColor = _G.FRIENDS_BNET_NAME_COLOR:GenerateHexColor()
    local bnetFormat = "|c"..bnetFriendColor.."%s|r (".. wowFormat .. ")"
    local function GetBNetAccountNameAndStatus(accountInfo, noCharacterName)
        if not accountInfo then
            return
        end

        local nameText, nameColor
        nameText = _G.BNet_GetBNetAccountName(accountInfo)

        -- Blizzard's FriendsFrame_GetBNetAccountNameAndStatus always returns a colour,
        -- keyed off isOnline. Default it here for the same reason: a friend who is
        -- online but not in a game has an empty characterName, which skips the block
        -- below and would otherwise return a nil colour to the caller.
        nameColor = accountInfo.gameAccountInfo.isOnline and _G.FRIENDS_BNET_NAME_COLOR or _G.FRIENDS_GRAY_COLOR

		local characterName = _G.FriendsFrame_GetFormattedCharacterName(accountInfo.gameAccountInfo.characterName, nil, accountInfo.gameAccountInfo.clientProgram, accountInfo.gameAccountInfo.timerunningSeasonID);

        if characterName ~= "" then
            if accountInfo.gameAccountInfo.clientProgram == _G.BNET_CLIENT_WOW and _G.CanCooperateWithGameAccount(accountInfo) then
                local classToken = _G.CUSTOM_CLASS_COLORS:GetClassToken(accountInfo.gameAccountInfo.className)
                nameColor = _G.CUSTOM_CLASS_COLORS[classToken] or nameColor
            else
                if _G.ENABLE_COLORBLIND_MODE == "1" then
                    characterName = accountInfo.gameAccountInfo.characterName.._G.CANNOT_COOPERATE_LABEL
                end
                nameColor = _G.FRIENDS_GRAY_COLOR
            end

            nameText = bnetFormat:format(nameText, characterName, accountInfo.gameAccountInfo.characterLevel)
        end

        return nameText, nameColor
    end

    _G.hooksecurefunc(Hook, "FriendsFrame_UpdateFriendButton", function(button)
        local nameText, nameColor, isFavoriteFriend
        if button.buttonType == _G.FRIENDS_BUTTON_TYPE_WOW then
            local info = _G.C_FriendList.GetFriendInfoByIndex(button.id)
            if info.connected then
                local classToken = _G.CUSTOM_CLASS_COLORS:GetClassToken(info.className)
                nameText = wowFormat:format(info.name, info.level)
                -- GetClassToken returns nil for an unknown/missing class name
                nameColor = _G.CUSTOM_CLASS_COLORS[classToken] or _G.FRIENDS_WOW_NAME_COLOR
            end
        elseif button.buttonType == _G.FRIENDS_BUTTON_TYPE_BNET then
            local accountInfo = _G.C_BattleNet.GetFriendAccountInfo(button.id)
            if accountInfo then
                local gameAccountInfo = accountInfo.gameAccountInfo
                if gameAccountInfo.isOnline and gameAccountInfo.characterName then
                    nameText, nameColor = GetBNetAccountNameAndStatus(accountInfo)
                    isFavoriteFriend = accountInfo.isFavorite
                end
            end
        end

        if nameText and nameColor then
            button.name:SetText(nameText)
            button.name:SetTextColor(nameColor:GetRGB())

            if isFavoriteFriend then
                button.Favorite:SetPoint("TOPLEFT", button.name, "TOPLEFT", button.name:GetStringWidth(), 0)
            end
        end
    end)
end

-- do --[[ SharedXML\FriendsFrame.xml ]]
-- end

-- _G.hooksecurefunc(private.SharedXML, "FriendsFrame", function()
-- end)
