-- HousingDecorGuide/UI/Projects/HDG_Breadcrumb.lua
-- Breadcrumb header for the Projects tab. Displays the current navigation path
-- (Projects ▸ Architect ▸ House ▸ F1 ▸ Room) and allows clicking any segment to
-- pop back up. Segments are passed as { { label = "...", key = "..." }, ... }.

local _, privateDB = ...

local M = {}

local SEG_HEIGHT = 22

function M:Create(parent)
    if self.frame then return self.frame end
    local f = CreateFrame("Frame", nil, parent)
    f:SetHeight(SEG_HEIGHT)
    f:SetPoint("TOPLEFT", parent, "TOPLEFT", 0, 0)
    f:SetPoint("TOPRIGHT", parent, "TOPRIGHT", 0, 0)

    local bg = f:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.04, 0.10, 0.16, 0.7)

    f.segments = {}
    self.frame = f
    return f
end

function M:SetSegments(segments, onSegmentClick)
    if not self.frame then return end

    -- Hide all existing segment buttons
    for _, s in ipairs(self.frame.segments) do s:Hide() end

    if not segments or #segments == 0 then return end

    local x = 12
    for i, seg in ipairs(segments) do
        local btn = self.frame.segments[i]
        if not btn then
            btn = CreateFrame("Button", nil, self.frame)
            btn:SetHeight(SEG_HEIGHT)
            btn.label = btn:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
            btn.label:SetPoint("LEFT", btn, "LEFT", 0, 0)
            self.frame.segments[i] = btn
        end

        local sep = (i < #segments) and "  >  " or ""
        local color = (i == #segments) and "|cfff5cb5c" or "|cff6cc8e6"
        btn.label:SetText(color .. (seg.label or "") .. "|r" .. sep)
        btn:SetWidth(btn.label:GetStringWidth() + 4)
        btn:ClearAllPoints()
        btn:SetPoint("LEFT", self.frame, "LEFT", x, 0)

        local capturedKey = seg.key
        local capturedIndex = i
        btn:SetScript("OnClick", function()
            if onSegmentClick then onSegmentClick(capturedKey, capturedIndex) end
        end)

        btn:Show()
        x = x + btn:GetWidth()
    end
end

privateDB.HDG_Breadcrumb = M
