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(self)
	self:HookScript("OnDoubleClick", HookApplicationClick)
	LFGListFrame.SearchPanel.FilterButton.ResetButton:SetPoint("TOPLEFT",LFGListFrame.SearchPanel.FilterButton,"BOTTOMRIGHT",-20,2)
end)

--搜索页分数
hooksecurefunc("LFGListSearchEntry_Update", function(self)
	if not PVEFrame:IsShown() then return end
    local resultID = self.resultID
	if not resultID or not C_LFGList.HasSearchResultInfo(resultID) then
		return;
	end
    local resultInfo = C_LFGList.GetSearchResultInfo(resultID)
    local leaderName = resultInfo.leaderName
	self.ActivityName:SetScale(.9)
	self.ActivityName:SetWidth(200)
    self.leaderMPlusRating = resultInfo.leaderOverallDungeonScore
	if not self.leaderMPlusRating then return end
	local scoreColor = C_ChallengeMode.GetDungeonScoreRarityColor(self.leaderMPlusRating) or HIGHLIGHT_FONT_COLOR
	if not self.score then
		self.score = self:CreateFontString(nil, "ARTWORK")
		self.score:SetFont(STANDARD_TEXT_FONT, 15, 'OUTLINE')
	end
	if self.score then
		self.score:SetText(self.leaderMPlusRating) 
		self.score:SetTextColor(scoreColor.r, scoreColor.g, scoreColor.b)
	end
	if (LFGListFrame.CategorySelection.selectedCategory == 2) then
		self.score:SetPoint("LEFT", self.Name, "LEFT", 162, -7)
		self.score:Show() 
	elseif (LFGListFrame.CategorySelection.selectedCategory == 3) then
		self.score:SetPoint("LEFT", self.Name, "LEFT", 140, -7)
		self.score:Show() 
	elseif (LFGListFrame.CategorySelection.selectedCategory == 6) then
		self.score:SetPoint("LEFT", self.Name, "LEFT", 140, -7)
		self.score:Show() 
	else
		self.score:Hide()
    end
end)

--打印邀请的队伍信息
local oldtime = GetTime()
hooksecurefunc("LFGListSearchEntry_Update", function(self)
	local resultID = self.resultID
	local _, appStatus, pendingStatus, appDuration = C_LFGList.GetApplicationInfo(resultID);

	if ( appStatus == "inviteaccepted" ) and GetTime() > (oldtime+3) then
		local searchResultInfo = C_LFGList.GetSearchResultInfo(resultID);
		local activityName = C_LFGList.GetActivityFullName(searchResultInfo.activityID, nil, searchResultInfo.isWarMode);
		local LFGleader = " |cffFFFFFF"..searchResultInfo.leaderName.."|r "
		local LFGtitle = " |cffFF7F00"..searchResultInfo.name.."|r "
		local LFGmap = " |cffEEDC82"..activityName.."|r"
		print("|cff00BFFFLFG: |r|cff00FF00已加入|r"..LFGleader..LFGtitle..LFGmap)
		oldtime = GetTime()
	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)
