local _,ns = ...
ns.ADDUIEVENT("PLAYER_LOGIN", function()
--龙傲天简易工具箱https://ngabbs.com/read.php?tid=42738039&rand=332
--获取等级职业
local level = UnitLevel("player")
local _,_,class = UnitClass("player")
if level ~= 10 and level ~= 11 then return end
if class ~= 10 then return end

SendChatMessage("AddUI龙傲天排本按钮已启动", "WHISPER", nil, UnitName("player"))

-- 检查随机副本队伍的准备状态
local function GetReadyStatus()
    local numReady = 0

    local _, _, _, _, _, _, _, _, _, _, numPlayers = GetLFGProposal()
    if not numPlayers then
        return -1
    end
    for i = 1, numPlayers do
        local isLeader, role, level, responded, accepted, name, class = GetLFGProposalMember(i)
        if accepted then
            numReady = numReady + 1
        end
    end
    return numPlayers - numReady -- 返回未准备人数
    
end

-- 替换系统进入副本按钮
local function UpdateEnterDungeonButton()
    
    local notReadyCount = GetReadyStatus()
    if notReadyCount == -1 then
        return
    end
    --print("未准备人数",GetReadyStatus())
	if IsInGroup(LE_PARTY_CATEGORY_HOME) then
		if notReadyCount > 1 then
			LFGDungeonReadyDialogEnterDungeonButton:SetText("未准备人数：" .. notReadyCount-1)
			LFGDungeonReadyDialogEnterDungeonButton:Disable()
		elseif notReadyCount <= 1 then
			LFGDungeonReadyDialogEnterDungeonButton:SetText("进入副本")
			LFGDungeonReadyDialogEnterDungeonButton:Enable()
		elseif notReadyCount <=0 then
		 ----前置条件已判断   DZLONGDB.openLongTool and IsInGroup(LE_PARTY_CATEGORY_HOME) and 
			-- --if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then --在自己最后点击会触发0个剩余 如果存在副本队伍则离开 副本以外的队伍
			--     C_PartyInfo.LeaveParty(LE_PARTY_CATEGORY_HOME)---待验证 这种情况是否成立 不确定是否已经有副本队伍再退出会不会晚
			--  --   print('成功离开副本以外小队-')
			C_PartyInfo.LeaveParty(LE_PARTY_CATEGORY_HOME)
		end
	else
		LFGDungeonReadyDialogEnterDungeonButton:SetText("未准备人数：" .. notReadyCount-1)
	end

end

-- 绑定系统进入副本的按钮 用户点击之后离开队伍
local function HandleButtonClick(btn)
    btn:HookScript("OnClick", function()
        --print('HandleButtonClick')
        --是开启龙傲天且存在于副本以外的队伍
        if IsInGroup(LE_PARTY_CATEGORY_HOME) then
            C_PartyInfo.LeaveParty(LE_PARTY_CATEGORY_HOME)
			UIErrorsFrame:AddExternalWarningMessage(ERR_LEFT_GROUP_YOU)--https://wowpedia.fandom.com/wiki/UI_ERROR_MESSAGE
            print('你已成功离开副本以外小队')
        end
    end)
end

-- 需要hook按钮列表
for _, btn in ipairs({LFGDungeonReadyDialogEnterDungeonButton}) do
    HandleButtonClick(btn)
end


-- 注册事件监听副本进入准备状态
local ReadyCheckFrame = CreateFrame("Frame")
ReadyCheckFrame:RegisterEvent("LFG_PROPOSAL_UPDATE")
ReadyCheckFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
ReadyCheckFrame:SetScript("OnEvent", function(_, event)
    -- 龙傲天存在副本以外的队伍才修改系统按钮
    if event == "LFG_PROPOSAL_UPDATE" then
        --更新系统的进入副本按钮
        UpdateEnterDungeonButton()
    end
end)


end)




