local _,ns = ...
ns.ADDUIEVENT("PLAYER_ENTERING_WORLD", function()
	if not AddUIDB.lfgkg  then return end
	if C_AddOns.IsAddOnLoaded("MeetingStone") then return end
	--配置PGF
	if C_AddOns.IsAddOnLoaded("PremadeGroupsFilter") then
	if UsePGFButtonText then UsePGFButtonText:SetText("PGF") end
	if PremadeGroupsFilterSettings then
		PremadeGroupsFilterSettings.ratingInfo = false	--队长评分
		PremadeGroupsFilterSettings.classBar = false	--职业颜色栏
		PremadeGroupsFilterSettings.leaderCrown = false	--显示队长标记
		PremadeGroupsFilterSettings.oneClickSignUp = false	--点击申请
	end
end

--解决预创建GetPlaystyleString报错问题https://github.com/0xbs/premade-groups-filter/issues/64
function LFMPlus_GetPlaystyleString(playstyle,activityInfo)
  if activityInfo and playstyle ~= (0 or nil) and C_LFGList.GetLfgCategoryInfo(activityInfo.categoryID).showPlaystyleDropdown then
    local typeStr
    if activityInfo.isMythicPlusActivity then
      typeStr = "GROUP_FINDER_PVE_PLAYSTYLE"
    elseif activityInfo.isRatedPvpActivity then
      typeStr = "GROUP_FINDER_PVP_PLAYSTYLE"
    elseif activityInfo.isCurrentRaidActivity then
      typeStr = "GROUP_FINDER_PVE_RAID_PLAYSTYLE"
    elseif activityInfo.isMythicActivity then
      typeStr = "GROUP_FINDER_PVE_MYTHICZERO_PLAYSTYLE"
    end
    return typeStr and _G[typeStr .. tostring(playstyle)] or nil
  else
    return nil
  end
end

C_LFGList.GetPlaystyleString = function(playstyle,activityInfo)
  return LFMPlus_GetPlaystyleString(playstyle, activityInfo)
end
--双击申请
local function HookApplicationClick()
	if LFGListFrame.SearchPanel.SignUpButton:IsEnabled() then
		LFGListFrame.SearchPanel.SignUpButton:Click()
	end
	if (not IsAltKeyDown()) and LFGListApplicationDialog:IsShown() and LFGListApplicationDialog.SignUpButton:IsEnabled() then
		LFGListApplicationDialog.SignUpButton:Click()
	end
end
hooksecurefunc("LFGListSearchEntry_OnLoad", function(entry)
	entry:HookScript("OnDoubleClick", HookApplicationClick)
	LFGListFrame.SearchPanel.FilterButton.ResetButton:SetPoint("TOPLEFT",LFGListFrame.SearchPanel.FilterButton,"BOTTOMRIGHT",-20,2)
end)
--搜索页分数
hooksecurefunc("LFGListSearchEntry_Update", function(entry)
    local resultID = entry.resultID
    local resultInfo = C_LFGList.GetSearchResultInfo(resultID)
    local leaderName = resultInfo.leaderName
	entry.ActivityName:SetScale(.9)
	entry.ActivityName:SetWidth(200)
    entry.leaderMPlusRating = resultInfo.leaderOverallDungeonScore
	if not entry.leaderMPlusRating then return end
	local scoreColor = C_ChallengeMode.GetDungeonScoreRarityColor(entry.leaderMPlusRating) or HIGHLIGHT_FONT_COLOR
	if not entry.score then
	entry.score = entry:CreateFontString(nil, "ARTWORK")
	entry.score:SetFont(STANDARD_TEXT_FONT, 15, 'OUTLINE')
	end
	if entry.score then
	entry.score:SetText(entry.leaderMPlusRating) 
	entry.score:SetTextColor(scoreColor.r, scoreColor.g, scoreColor.b)
	end
	if (LFGListFrame.CategorySelection.selectedCategory == 2) then
	entry.score:SetPoint("LEFT", entry.Name, "LEFT", 162, -7)
	entry.score:Show() 
	elseif (LFGListFrame.CategorySelection.selectedCategory == 3) then
	entry.score:SetPoint("LEFT", entry.Name, "LEFT", 140, -7)
	entry.score:Show() 
	elseif (LFGListFrame.CategorySelection.selectedCategory == 6) then
	entry.score:SetPoint("LEFT", entry.Name, "LEFT", 140, -7)
	entry.score:Show() 
	else
	entry.score:Hide()
    end
end)

-- 隐藏并新建搜索按钮NGA@雨夜独行客https://bbs.nga.cn/read.php?pid=592309998&opt=128
LFGListFrame.CategorySelection.FindGroupButton:HookScript("OnShow", function(self)
	LFGListFrame.CategorySelection.FindGroupButton:Hide()
end)
local bu = CreateFrame("Button", nil, LFGListFrame.CategorySelection, "LFGListMagicButtonTemplate")
bu:SetText(LFG_LIST_FIND_A_GROUP)
bu:SetSize(135,22)
bu:SetPoint("BOTTOMRIGHT", -3, 4)
bu:SetScript("OnClick", function(self)
local panel = self:GetParent()
if not panel.selectedCategory then
return
end

local baseFilters = panel:GetParent().baseFilters
local searchPanel = panel:GetParent().SearchPanel
LFGListSearchPanel_SetCategory(searchPanel, panel.selectedCategory, panel.selectedFilters, baseFilters)
LFGListSearchPanel_DoSearch(searchPanel)
LFGListFrame_SetActivePanel(panel:GetParent(), searchPanel)
end)

--创建浏览队伍按钮
LFGListFrame.ApplicationViewer.BrowseGroupsButton:HookScript("OnShow", function(self)
	LFGListFrame.ApplicationViewer.BrowseGroupsButton:Hide()
end)
local retbu = CreateFrame("Button", nil, LFGListFrame.ApplicationViewer, "LFGListMagicButtonTemplate")
retbu:SetText("浏览队伍")
retbu:SetSize(140,22)
retbu:SetPoint("BOTTOMLEFT", 0, 4)
retbu:SetScript("OnClick", function()
LFGListFrame.ApplicationViewer.BrowseGroupsButton:Click()
end)

--[[修复双击申请导致转圈圈
hooksecurefunc("LFGListSearchPanel_UpdateResultList", function(self)
	if next(self.results) and next(self.applications) then
		for _, value in ipairs_reverse(self.applications) do
			local resultInfo = C_LFGList.GetSearchResultInfo(value)
			local _, status, _, _, role = C_LFGList.GetApplicationInfo(value)
			if status == "none" then
				tDeleteItem(self.applications, value)
			end
		self.totalResults = #self.results
		LFGListSearchPanel_UpdateResults(self)
		end
	end
end)
--预创建blz问题申请bug显示俩https://bbs.nga.cn/read.php?&tid=30832039&pid=591879748&to=1
hooksecurefunc("LFGListSearchPanel_UpdateResultList", function(self)
	if next(self.results) and next(self.applications) then
		for _, value in ipairs(self.applications) do
			tDeleteItem(self.results, value)

		end
		self.totalResults = #self.results
		
		LFGListSearchPanel_UpdateResults(self)
	end
end)
]]
end)
