local _,ns = ...
ns.tips("好友列表添加搜索好友按钮")
--抄自FriendListHelper
local function UpdateFriendList(searchText)
    local numBNetFriends = BNGetNumFriends()
    local dataProvider = CreateDataProvider()

    for i = 1, numBNetFriends do
        local accountInfo = C_BattleNet.GetFriendAccountInfo(i)
        if accountInfo then
            local battleTag = (accountInfo.battleTag or ""):lower()
            local characterName = (accountInfo.gameAccountInfo.characterName or ""):lower()
            if #searchText == 0 or battleTag:find(searchText, 1, true) or characterName:find(searchText, 1, true) then
                dataProvider:Insert({id = i, buttonType = FRIENDS_BUTTON_TYPE_BNET})
            end
        end
    end

    FriendsListFrame.ScrollBox:SetDataProvider(dataProvider, ScrollBoxConstants.RetainScrollPosition)
end

local function AddSearchBar()
    if not FriendsListFrame then return end
	if FriendListHelper_SearchBar then return end

    local searchBar = CreateFrame("EditBox", "FriendListHelper_SearchBar", FriendsFrame, "InputBoxTemplate")
    searchBar:SetSize(130, 20)--大小
	searchBar:SetScale(1.1)
    searchBar:SetPoint("BOTTOMRIGHT", FriendsFrameInset, "TOPRIGHT", 0, 0)--位置
    searchBar:SetAutoFocus(false)
    searchBar:SetText("")
    searchBar:ClearFocus()
    searchBar:Show()

    local placeholder = searchBar:CreateFontString(nil, "OVERLAY", "GameFontHighlightMedium")
    placeholder:SetPoint("LEFT", searchBar, "LEFT", 5, 0)
    placeholder:SetText(SEARCH)--默认文本
    placeholder:SetTextColor(0.5, 0.5, 0.5, 0.7)

    local function UpdatePlaceholder()
        if searchBar:GetText() == "" then
            placeholder:Show()
        else
            placeholder:Hide()
        end
    end

    local activeSearchText = ""
    local isSearchActive = false

    local frame = CreateFrame("Frame")
    frame:RegisterEvent("FRIENDLIST_UPDATE")
    frame:SetScript("OnEvent", function()
        if isSearchActive and activeSearchText then
            UpdateFriendList(activeSearchText)
        else
            UpdateFriendList("")
        end
    end)
	
	--添加清除按钮
	local ClearBox = CreateFrame("Button", nil, searchBar)
	ClearBox:SetPoint("RIGHT", -5, 0)
	ClearBox:SetSize(13, 13)
	ClearBox:SetNormalTexture("common-search-clearbutton")
	ClearBox:SetHighlightTexture("common-search-clearbutton")
	ClearBox:SetScript("OnClick", function()
		searchBar:SetText("")
		searchBar:GetScript("OnTextChanged")(searchBar)
	end)
	
	searchBar:SetScript("OnEnterPressed", function(self)
		self:ClearFocus()  -- 回车取消焦点
	end)
	
    searchBar:SetScript("OnTextChanged", function(self)
		ClearBox:SetShown(self:GetText() ~= "")
        UpdatePlaceholder()
        activeSearchText = self:GetText():lower()

        isSearchActive = (#activeSearchText > 0)

        UpdateFriendList(activeSearchText)
    end)

    hooksecurefunc("FriendsList_Update", function()
        if isSearchActive and activeSearchText then
            UpdateFriendList(activeSearchText)
        end
    end)

    searchBar:SetScript("OnEditFocusGained", function()
        placeholder:Hide()
    end)

    searchBar:SetScript("OnEditFocusLost", function()
        UpdatePlaceholder()
    end)

    local function ResetSearchBar()
        searchBar:SetText("")
        searchBar:SetAutoFocus(false)
        searchBar:ClearFocus()
        FriendsList_Update(true)
		
		activeSearchText = ""
        isSearchActive = false
        UpdateFriendList("")
    end
	FriendsListFrame:HookScript("OnHide", function()
		--ResetSearchBar()--关闭好友窗口重置搜索内容
    end)

    UpdatePlaceholder()

    return searchBar
end
local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")
frame:SetScript("OnEvent", function()
    AddSearchBar()
end)