-- HousingDecorGuide/UI/Projects/HDG_BudgetStrip.lua
-- Persistent header strip for the Projects tab: Rooms budget + Decor budget meters.
-- Live-updates from C_HousingLayout / C_HousingDecor budget APIs.

local _, privateDB = ...

local M = {}

local STRIP_HEIGHT = 28

local function _createMeter(parent, label, x)
    local f = CreateFrame("Frame", nil, parent)
    f:SetSize(180, STRIP_HEIGHT - 4)
    f:SetPoint("LEFT", parent, "LEFT", x, 0)

    local lbl = f:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    lbl:SetPoint("LEFT", f, "LEFT", 0, 0)
    lbl:SetText(label)
    lbl:SetTextColor(0.6, 0.7, 0.8, 1)

    local bar = CreateFrame("StatusBar", nil, f)
    bar:SetSize(80, 8)
    bar:SetPoint("LEFT", lbl, "RIGHT", 6, 0)
    bar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
    bar:SetStatusBarColor(0.43, 0.72, 0.55, 1)   -- green
    bar:SetMinMaxValues(0, 100)
    bar:SetValue(0)

    local bg = bar:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0, 0, 0, 0.5)

    local txt = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    txt:SetPoint("LEFT", bar, "RIGHT", 6, 0)
    txt:SetText("0 / 0")

    f.bar = bar
    f.txt = txt
    return f
end

function M:Update(roomSpent, roomMax, decorSpent, decorMax)
    if not self.frame then return end
    local function pct(s, mx) return mx > 0 and math.min(100, (s / mx) * 100) or 0 end

    self.roomMeter.bar:SetValue(pct(roomSpent or 0, roomMax or 0))
    self.roomMeter.txt:SetText(string.format("%d / %d", roomSpent or 0, roomMax or 0))

    self.decorMeter.bar:SetValue(pct(decorSpent or 0, decorMax or 0))
    self.decorMeter.txt:SetText(string.format("%d / %d", decorSpent or 0, decorMax or 0))
end

function M:Refresh()
    local roomMax    = (C_HousingLayout and C_HousingLayout.GetRoomPlacementBudget())  or 0
    local roomSpent  = (C_HousingLayout and C_HousingLayout.GetSpentPlacementBudget()) or 0
    local decorMax   = (C_HousingDecor  and C_HousingDecor.GetMaxPlacementBudget())    or 0
    local decorSpent = (C_HousingDecor  and C_HousingDecor.GetSpentPlacementBudget())  or 0
    self:Update(roomSpent, roomMax, decorSpent, decorMax)
end

function M:Create(parent)
    self.frame = CreateFrame("Frame", nil, parent)
    self.frame:SetHeight(STRIP_HEIGHT)
    self.frame:SetPoint("TOPLEFT", parent, "TOPLEFT", 0, 0)
    self.frame:SetPoint("TOPRIGHT", parent, "TOPRIGHT", 0, 0)

    local bg = self.frame:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.08, 0.16, 0.25, 0.4)

    self.roomMeter  = _createMeter(self.frame, "Rooms", 12)
    self.decorMeter = _createMeter(self.frame, "Decor", 220)

    -- Live update on relevant events
    self.frame:RegisterEvent("HOUSE_LEVEL_FAVOR_UPDATED")
    self.frame:RegisterEvent("HOUSING_NUM_DECOR_PLACED_CHANGED")
    self.frame:RegisterEvent("HOUSING_LAYOUT_ROOM_RECEIVED")
    self.frame:RegisterEvent("HOUSING_LAYOUT_ROOM_REMOVED")
    self.frame:RegisterEvent("HOUSING_LAYOUT_ROOM_RETURNED")
    self.frame:SetScript("OnEvent", function() self:Refresh() end)
    self.frame:SetScript("OnShow", function() self:Refresh() end)

    self:Refresh()
    return self.frame
end

privateDB.HDG_BudgetStrip = M
