local _, addonTable = ...
local WipedQoL = addonTable.WipedQoL
local TRANSLATE = addonTable.TRANSLATE or function(msg) return msg end

local raidToolsParkingFrame

local function GetRaidToolsParkingFrame()
    if not raidToolsParkingFrame then
        raidToolsParkingFrame = CreateFrame("Frame")
        raidToolsParkingFrame:Hide()
    end

    return raidToolsParkingFrame
end

local function CanHideFrameNow(frame)
    if not frame then
        return false
    end

    if not InCombatLockdown or not InCombatLockdown() then
        return true
    end

    return not (frame.IsProtected and frame:IsProtected())
end

local function KeepRaidToolsHidden(frame)
    if not frame then
        return
    end

    if CanHideFrameNow(frame) then
        if frame.Hide then
            pcall(frame.Hide, frame)
        end
        return
    end

    if frame.SetAlpha then
        frame:SetAlpha(0)
        frame._wqolRaidToolsAlphaSuppressed = true
    end
end

local function SuppressRaidToolsFrame(frame)
    if not frame or frame._wqolRaidToolsSuppressed then
        return
    end

    if frame.UnregisterAllEvents then
        frame:UnregisterAllEvents()
    end

    frame._wqolRaidToolsSuppressed = true
    KeepRaidToolsHidden(frame)

    if frame.SetParent then
        pcall(frame.SetParent, frame, GetRaidToolsParkingFrame())
    end

    if frame._wqolRaidToolsHooksInstalled then
        return
    end

    frame._wqolRaidToolsHooksInstalled = true

    if frame.Show then
        hooksecurefunc(frame, "Show", function(shownFrame)
            KeepRaidToolsHidden(shownFrame)
        end)
    end

    if frame.SetShown then
        hooksecurefunc(frame, "SetShown", function(shownFrame, shown)
            if shown then
                KeepRaidToolsHidden(shownFrame)
            end
        end)
    end
end

function WipedQoL:IsRaidFrameManagerHidden()
    local modules = self.db and self.db.profile and self.db.profile.modules
    return modules and modules.hideRaidFrameManager == true
end

function WipedQoL:SetRaidFrameManagerHidden(enabled)
    if not self.db or not self.db.profile or not self.db.profile.modules then
        return
    end

    local wasEnabled = self.db.profile.modules.hideRaidFrameManager == true
    self.db.profile.modules.hideRaidFrameManager = enabled and true or false

    if self.db.profile.modules.hideRaidFrameManager then
        self.raidFrameManagerReloadSuggested = nil
        self:ApplyRaidFrameManagerVisibility()
        self:PrintDebug(TRANSLATE("Raid tools hidden."))
    elseif wasEnabled then
        self.raidFrameManagerReloadSuggested = true
        self:PrintDebug(TRANSLATE("Raid tools shown again after the next /reload."))
    end

    if self.RefreshRaidFrameManagerState then
        self:RefreshRaidFrameManagerState()
    end
end

function WipedQoL:ApplyRaidFrameManagerVisibility()
    if not self:IsRaidFrameManagerHidden() then
        return
    end

    local manager = _G.CompactRaidFrameManager
    if manager then
        SuppressRaidToolsFrame(manager)
    end

    if not self.raidFrameManagerHooked then
        self.raidFrameManagerHooked = true

        self:RegisterEvent("PLAYER_ENTERING_WORLD", "HandleRaidFrameManagerRefresh")
        self:RegisterEvent("GROUP_ROSTER_UPDATE", "HandleRaidFrameManagerRefresh")
        self:RegisterEvent("ADDON_LOADED", "HandleRaidFrameManagerAddonLoaded")
    end
end

function WipedQoL:HandleRaidFrameManagerRefresh()
    self:ApplyRaidFrameManagerVisibility()
end

function WipedQoL:HandleRaidFrameManagerAddonLoaded(_, loadedAddonName)
    if loadedAddonName == "Blizzard_CompactRaidFrames" or _G.CompactRaidFrameManager then
        self:ApplyRaidFrameManagerVisibility()
    end
end
