--## Author:阿朱
local Premade = {}

local SignUp = false
local AutoAcceptInvite = true
local currentMessageFrame = nil
local processedInvites = {}

local function ShowCentralMessage(text, duration)
    if currentMessageFrame then
        currentMessageFrame:Hide()
        currentMessageFrame = nil
    end

    local frame = CreateFrame("Frame", nil, UIParent, "BackdropTemplate")
    frame:SetSize(500, 225)
    frame:SetPoint("CENTER", UIParent, "CENTER", 0, 200)
    frame:SetFrameStrata("DIALOG")

    local bg = frame:CreateTexture(nil, "BACKGROUND")
    bg:SetAtlas("midnight-scenario-titlebg")
    bg:SetAllPoints()

    local fontString = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    fontString:SetFont("Fonts\\FRIZQT__.TTF", 30, "OUTLINE")
    fontString:SetTextColor(1, 1, 1)
    fontString:SetPoint("CENTER", frame, "CENTER")
    fontString:SetText(text)

    frame:Show()
    currentMessageFrame = frame

    C_Timer.After(duration or 10, function()
        if currentMessageFrame == frame then
            frame:Hide()
            currentMessageFrame = nil
        end
    end)
end

local function OnDoubleClick(self)
    local button = self:GetParent():GetParent():GetParent()
    if button.selectedResult and (not UnitInParty("player") or UnitIsGroupLeader("player")) then
        SignUp = true
        LFGListSearchPanel_SignUp(button)
    end
end

hooksecurefunc("LFGListSearchPanel_SignUp", function()
    if SignUp then
        SignUp = false
        LFGListApplicationDialog.SignUpButton:Click()
    end
end)

local function OnLFGListSearchEntryUpdate(self)
    if not self.resultID then return end

    local searchResultInfo = C_LFGList.GetSearchResultInfo(self.resultID)
    if searchResultInfo and not searchResultInfo.isDelisted then
        self:SetScript('OnDoubleClick', OnDoubleClick)
    else
        self:SetScript('OnDoubleClick', nil)
    end
end

local function SetupAutoAccept()
    if not AutoAcceptInvite then return end

    LFDRoleCheckPopup:SetScript("OnShow", function()
        if AutoAcceptInvite then
            LFDRoleCheckPopupAcceptButton:Click()
            local desc = LFDRoleCheckPopupDescriptionText:GetText()
            print("|cff00BFFFLFG增强:|r |cff00FF00自动接受 |r" .. desc)
            ShowCentralMessage("自动接受 " .. desc, 5)
        end
    end)

    LFGListInviteDialog:SetScript("OnShow", function(self)
        if not AutoAcceptInvite then return end

        local resultID = self.resultID
        local _, status, _, _, role = C_LFGList.GetApplicationInfo(self.resultID)
        local groupName = self.GroupName:GetText() or ""
        local activityName = self.ActivityName:GetText() or ""

        if status == "invited" then
            self.AcceptButton:Click()
            if not processedInvites[resultID] then
                local msg = string.format("|cff00FF00已加入队伍|r |cffFF0000%s|r |cffEEAEEE%s|r", groupName, activityName)
                print("|cff00BFFFLFG增强:|r " .. msg)
                ShowCentralMessage(groupName .. " " .. activityName, 5)
                processedInvites[resultID] = true
            end
        elseif status == "inviteaccepted" then
            self.AcknowledgeButton:Click()
            if not processedInvites[resultID] then
                local msg = string.format("|cff00FF00已加入队伍|r |cffFF0000%s|r |cffEEAEEE%s|r", groupName, activityName)
                print("|cff00BFFFLFG增强:|r " .. msg)
                ShowCentralMessage(groupName .. " " .. activityName, 5)
                processedInvites[resultID] = true
            end
        end
    end)
end

local frame = CreateFrame('Frame')
frame:RegisterEvent('ADDON_LOADED')
frame:SetScript('OnEvent', function(_, event, arg)
    if event == 'ADDON_LOADED' and arg == "OrzUI" then
        hooksecurefunc('LFGListSearchEntry_Update', OnLFGListSearchEntryUpdate)
        SetupAutoAccept()
    end
end)