---@diagnostic disable: undefined-field, undefined-global
---@type string
local AddonName = ...
---@class Private
local Private = select(2, ...)

-- AutoQueue (QUICK-V6N-01): auto-confirms the LFG role-check popup with the
-- role the player already set in the LFG tool, by driving Blizzard's own
-- accept-button click path. The module writes nothing -- it neither submits a
-- role itself nor ticks the popup's checkboxes; it only clicks the popup's own
-- accept button, so whatever role the Dungeon Finder holds is what gets
-- submitted, and a battleground role check still resolves through Blizzard's
-- own branch. Fully local-only: no comm, no broadcast, no UI creation in this
-- file.
--
-- Because nothing here ever calls SetChecked, Blizzard's accept-button enabled
-- flag is always its own derivation -- the module simply never clicks a button
-- Blizzard left disabled. No role selected in the LFG tool therefore degrades
-- to "do nothing", which is also the honest behaviour: there is no role to
-- confirm.
--
-- Private.*-Abhängigkeiten:
--   Private.safecall ist die einzige Abhängigkeit. Sie muss beim Laden aus
--   Utility.lua vorhanden sein; die Event-Dispatch-Funktion verwendet sie
--   später zur Laufzeit.
--
-- Der TOC-Slot ist load-bearing: Diese Datei muss nach Init.lua
-- (Private.safecall) und nach Migrations.lua (der autoqueue-SavedVars-
-- Blattknoten) geladen werden. Sie liegt unterhalb der do-not-package-Grenze
-- und bleibt deshalb in jedem Build enthalten; der Solo-Test bleibt damit
-- ebenfalls ungated.

-- Read-only role -> popup-button-global map. Used ONLY by the /wt queuetest
-- harness to report which role the LFG tool currently holds -- no code path
-- writes through it. DAMAGER intentionally maps to the DPS-named button global
-- -- the naming mismatch that would otherwise index a nil.
local ROLE_BUTTONS = {
    TANK = "LFDRoleCheckPopupRoleButtonTank",
    HEALER = "LFDRoleCheckPopupRoleButtonHealer",
    DAMAGER = "LFDRoleCheckPopupRoleButtonDPS",
}

-- Guard: false unless every popup global the module needs is present, so no
-- path below ever indexes a missing frame.
local function PopupGlobalsPresent()
    if not LFDRoleCheckPopup then return false end
    if not LFDRoleCheckPopupAcceptButton then return false end
    return true
end

local frame = CreateFrame("Frame")

function frame:OnEvent(event, ...)
    Private.safecall(self[event], self, event, ...)
end

frame:SetScript("OnEvent", frame.OnEvent)
frame:RegisterEvent("LFG_ROLE_CHECK_SHOW")

function frame:LFG_ROLE_CHECK_SHOW(event, requeue)
    -- Cheap bail before deferring: WildThingsSavedVars is guaranteed populated
    -- by the time this event can ever fire (long after wt:OnInitialize()), so
    -- this is a real settings read, not the load-time race the module
    -- deliberately avoids by registering unconditionally.
    local settings = WildThingsSavedVars and WildThingsSavedVars.autoqueue
    if not settings or not settings.enabled then return end

    -- One-frame deferral: Blizzard shows/populates the popup from its own
    -- LFG_ROLE_CHECK_SHOW handler, and the accept button's enabled state is
    -- only correct after that ran. Re-guard everything inside the closure --
    -- the setting, the Shift escape hatch, the popup globals and the popup's
    -- shown state may all have changed in that one frame.
    C_Timer.After(0, function()
        local liveSettings = WildThingsSavedVars and WildThingsSavedVars.autoqueue
        if not liveSettings or not liveSettings.enabled then return end
        if IsShiftKeyDown() then return end
        if not PopupGlobalsPresent() then return end
        if not LFDRoleCheckPopup:IsShown() then return end

        -- The accept button's own enabled flag is the only gate -- never click
        -- a button Blizzard left disabled, and never enable it ourselves.
        -- Blizzard disables it exactly when the LFG tool holds no valid role,
        -- which is the case where there is nothing to confirm.
        if LFDRoleCheckPopupAcceptButton:IsEnabled() then
            LFDRoleCheckPopupAcceptButton:Click()
        end
    end)
end

-- Private:AutoQueueTest(): unadvertised /wt queuetest solo harness, on the
-- /wt restest precedent. Prints the setting, the popup's shown state, which
-- role the LFG tool currently holds, whether this is a battleground role check
-- and whether Blizzard has the accept button enabled. Pure read-out: it clicks
-- nothing, ticks nothing and restores nothing, because the module itself no
-- longer writes either.
function Private:AutoQueueTest()
    local settings = WildThingsSavedVars and WildThingsSavedVars.autoqueue or {}
    print("|cff99ff00Wild Things:|r queuetest -- enabled=" .. tostring(settings.enabled))

    local popupExists = LFDRoleCheckPopup ~= nil
    local popupShown = popupExists and LFDRoleCheckPopup:IsShown()
    print("  popup: exists=" .. tostring(popupExists) .. " shown=" .. tostring(popupShown))

    for role, buttonName in pairs(ROLE_BUTTONS) do
        local button = _G[buttonName]
        local exists = button ~= nil
        local hasCheck = exists and button.checkButton ~= nil
        local enabled = hasCheck and button.checkButton:IsEnabled()
        local checked = hasCheck and button.checkButton:GetChecked()
        print("  " .. role .. ": exists=" .. tostring(exists)
            .. " checkButton=" .. tostring(hasCheck)
            .. " enabled=" .. tostring(enabled)
            .. " checked=" .. tostring(checked))
    end

    local isBGRoleCheck = select(6, GetLFGRoleUpdate())
    print("  isBGRoleCheck=" .. tostring(isBGRoleCheck))

    local acceptExists = LFDRoleCheckPopupAcceptButton ~= nil
    local acceptEnabled = acceptExists and LFDRoleCheckPopupAcceptButton:IsEnabled()
    print("  acceptButton: exists=" .. tostring(acceptExists)
        .. " enabled=" .. tostring(acceptEnabled))
    print("  -> would click: " .. tostring(
        (settings.enabled and popupShown and acceptEnabled) == true))
end
