-- VDS_Controller_Detail
-- ============================================================================
-- OnClick handlers for detail-strip buttons. Popup-driven flows (rename /
-- delete / export) declared in VDS_Popups.lua.

VDS = VDS or {}
VDS.Controller_Detail = VDS.Controller_Detail or {}

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

local function dispatchCycleHerbSort()
    -- Reducer detects nil payload.mode and rotates to next entry in HERB_SORT_MODES_CYCLE.
    VDS.Store:Dispatch({ type = A.SET_HERB_SORT_MODE, payload = {} })
end

local function currentEditingTheme()
    local ui = VDS.Store:GetState().session.ui  -- top-level only: popup target
    local bucket = ui.byView[ui.filterMode]
    return bucket and (bucket.editingThemeName or bucket.activeTheme) or nil
end

local function dispatchRename()
    local name = currentEditingTheme()
    if not name or not _G.StaticPopup_Show then return end
    local dlg = _G.StaticPopup_Show("VDS_RENAME_CUSTOM_THEME", name)  -- %s in popup.text
    if dlg then dlg.data = name end
end

local function dispatchDelete()
    local name = currentEditingTheme()
    if not name or not _G.StaticPopup_Show then return end
    local dlg = _G.StaticPopup_Show("VDS_DELETE_CUSTOM_THEME", name)
    if dlg then dlg.data = name end
end

local function dispatchExport()
    local name = currentEditingTheme()
    if not name or not _G.StaticPopup_Show then return end
    -- Find the theme's dyes; pass to the popup via .data.
    local theme
    for _, t in ipairs(VDS.Store:GetState().account.customThemes) do  -- top-level only: export reads current theme dyes
        if t.name == name then theme = t; break end
    end
    if not theme then return end
    local dlg = _G.StaticPopup_Show("VDS_EXPORT_CUSTOM_THEME", name)
    if dlg then dlg.data = { name = name, dyeIDs = theme.dyes or {} } end
end

-- Edit/Done toggle: enters/exits custom palette edit mode.
-- Button label tracks state via detail.editToggle.text binding.
local function dispatchDone()
    local ui = VDS.Store:GetState().session.ui  -- top-level only: Edit/Done toggle reads current mode
    local bucket = ui.byView[ui.filterMode]
    if not bucket then return end
    if bucket.editingThemeName then
        VDS.Store:Dispatch({ type = A.CUSTOM_THEME_EDIT_END })
    elseif bucket.activeTheme then
        VDS.Store:Dispatch({
            type    = A.CUSTOM_THEME_EDIT_START,
            payload = { name = bucket.activeTheme },
        })
    end
end

local CLICK_HANDLERS = {
    ["detail.sortBtn"]       = dispatchCycleHerbSort,
    ["detail.renameBtn"]     = dispatchRename,
    ["detail.deleteBtn"]     = dispatchDelete,
    ["detail.exportBtn"]     = dispatchExport,
    ["detail.doneBtn"]       = dispatchDone,
}

function C:Wire(rootFrame)
    if not (rootFrame and rootFrame.widgets) then return end  -- exception(false-positive): Wire-on-empty-rootFrame is a supported no-op (wire smoke contract)
    for id, handler in pairs(CLICK_HANDLERS) do
        local widget = rootFrame.widgets[id]
        if widget and widget.SetScript then  -- exception(false-positive): Wire tolerates absent widgets (empty-root contract)
            widget:SetScript("OnClick", handler)
        end
    end
end
