-- VDS_Controller_LiveBridge
-- ============================================================================
-- Glue between Blizzard's housing editor and VDS's live view. When
-- replaceBlizzard + live mode: hides DecorCustomizationsPane + DyeSelectionPopout,
-- opens VDS window. On exit: restores pane. Mirrors VDS_LivePreview:InstallHousingHooks.
-- Init() called by LiveCustomizeObserver once Blizzard_HouseEditor LoD is ready.

VDS = VDS or {}
VDS.Controller_LiveBridge = VDS.Controller_LiveBridge or {}

local C = VDS.Controller_LiveBridge
local A = VDS.Constants.ACTIONS

local function getPane()
    return _G.HouseEditorFrame.CustomizeModeFrame.DecorCustomizationsPane
end

local function setBlizzardChromeAlpha(alpha)
    getPane():SetAlpha(alpha)
    _G.DyeSelectionPopout:SetAlpha(alpha)
end

-- Reparent main window onto HouseEditorFrame in live mode so it renders above
-- housing chrome (mirrors VDS_LivePreview:ShowDyeStudioPanel lines 687-689).
local function reparentMainTo(parent)
    local f = VDS.MainFrame.frame
    if not f then return end  -- pre-Build guard only
    f:SetParent(parent or _G.UIParent)
    f:SetFrameStrata("DIALOG")
end

function C:Init()
    if self._inited then return end
    self._inited = true

    -- Guard against Blizzard re-Show calls mid-live-mode (donor hits this exact pattern).
    hooksecurefunc(getPane(), "Show", function()
        local state = VDS.Store:GetState()  -- top-level only: hook context
        if state.account.config.replaceBlizzard and state.session.ui.live.active then
            -- Defer past Blizzard's post-Show layout pass; direct SetAlpha gets overwritten.
            _G.C_Timer.After(0, function() setBlizzardChromeAlpha(0) end)
        end
    end)

    VDS.Store:Subscribe(function(actionType)
        local state = VDS.Store:GetState()  -- top-level only: Store subscriber callback
        if actionType == A.LIVE_MODE_ENTER then
            -- Snap off Herbs/Canvas/Custom; those tabs are hidden in live mode.
            local mode = state.session.ui.filterMode
            if mode == "herbs" or mode == "canvas" or mode == "custom" then
                VDS.Store:Dispatch({
                    type    = A.SET_FILTER_MODE,
                    payload = { mode = "category" },
                })
            end
            if state.account.config.replaceBlizzard then
                -- Open FIRST, reparent, then defer pane suppression past Blizzard's post-Show pass.
                -- (VDS_LivePreview:EnterLiveMode + ShowDyeStudioPanel lines 643-648, 687-689)
                if not state.session.ui.windowShown then
                    VDS.Store:Dispatch({ type = A.MAIN_WINDOW_SHOW })
                end
                reparentMainTo(_G.HouseEditorFrame)
                _G.C_Timer.After(0, function()
                    if VDS.Store:GetState().session.ui.live.active then  -- top-level only: deferred suppression
                        setBlizzardChromeAlpha(0)
                    end
                end)
            end
        elseif actionType == A.LIVE_MODE_EXIT then
            setBlizzardChromeAlpha(1)
            reparentMainTo(_G.UIParent)
        end
    end)

    -- Also restore on HouseEditorFrame:OnHide. Without reparenting back to UIParent
    -- the main frame disappears with the editor on next /reload (donor lines 750-755).
    _G.HouseEditorFrame:HookScript("OnHide", function()
        setBlizzardChromeAlpha(1)
        reparentMainTo(_G.UIParent)
    end)
end
