-- VDS_Controller_Minimap
-- ============================================================================
-- Minimap button via LibDataBroker-1.1 + LibDBIcon-1.0 (embedded under Libs/).
-- account.minimap handed to DBIcon so position persists via SavedVars.
-- Addon Compartment entry is TOC-registered (AddonCompartmentFunc directives);
-- handlers at the bottom of this file.

VDS = VDS or {}
VDS.Controller_Minimap = VDS.Controller_Minimap or {}

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

local LDB    = _G.LibStub and _G.LibStub("LibDataBroker-1.1", true)
local DBIcon = _G.LibStub and _G.LibStub("LibDBIcon-1.0", true)

function C:Init()
    if not (LDB and DBIcon) then return end
    if self._inited then return end
    self._inited = true

    local dataObject = LDB:NewDataObject("VamoosesDyeStudio", {
        type    = "launcher",
        text    = "Vamoose's Dye Studio",
        icon    = "Interface\\AddOns\\VamoosesDyeStudio\\textures\\vds_icon",
        OnClick = function(_self, button)
            if button == "LeftButton" then
                VDS.Store:Dispatch({ type = A.MAIN_WINDOW_TOGGLE })
            elseif button == "RightButton" then
                print(VDS.Constants.CHAT_PREFIX .. " Right-click config TODO (Phase 7)")
            end
        end,
        OnTooltipShow = function(tooltip)
            tooltip:AddLine("Vamoose's Dye Studio")
            tooltip:AddLine("|cFFcbd5e1Left-click:|r open studio", 1, 1, 1)
            tooltip:AddLine("|cFFcbd5e1Right-click:|r config", 1, 1, 1)
        end,
    })

    local minimap = VDS.Store:GetState().account.minimap  -- top-level only: LDBIcon binds to live table
    DBIcon:Register("VamoosesDyeStudio", dataObject, minimap)

    VDS.Store:Subscribe(function(actionType)
        if actionType == A.MINIMAP_HIDE_SET then
            local hide = VDS.Store:GetState().account.minimap.hide  -- top-level only: Store subscriber callback
            if hide then DBIcon:Hide("VamoosesDyeStudio")
            else DBIcon:Show("VamoosesDyeStudio") end
        end
    end)
end

-- Wired from MainFrame:Build post-SavedVars-load, so account.minimap is live.
function C:Wire(_rootFrame)
    self:Init()
end

-- ============================================================================
-- ADDON COMPARTMENT (referenced by TOC AddonCompartmentFunc directives).
-- Blizzard registers the entry from the TOC at PLAYER_ENTERING_WORLD using
-- ## Title as the text and ## IconTexture as the icon (HDG pattern).
-- ============================================================================

function VDS_OnAddonCompartmentClick(_, buttonName)
    if buttonName == "LeftButton" then
        VDS.MainFrame:Toggle()
    end
end

function VDS_OnAddonCompartmentEnter(_, menuItem)
    local CC = VDS.Constants.COLOR_CODE
    _G.GameTooltip:SetOwner(menuItem, "ANCHOR_RIGHT")
    _G.GameTooltip:AddLine(CC.brandTeal .. "Vamoose's Dye Studio|r", 1, 1, 1)
    _G.GameTooltip:AddLine("Browse, preview, and manage housing dyes", 0.7, 0.7, 0.7)
    _G.GameTooltip:AddLine(" ")
    _G.GameTooltip:AddLine("|cFFFFFFFFClick:|r Toggle window", 0.7, 0.7, 0.7)
    _G.GameTooltip:Show()
end

function VDS_OnAddonCompartmentLeave()
    _G.GameTooltip:Hide()
end
