-- HousingDecorGuide/UI/Tabs/HDG_ProjectsTab.lua
-- Top-level Projects tab. View-state machine:
--   landing   — empty state (no captured rooms yet) or pre-capture
--   architect — 2D blueprint canvas + floor switcher + room detail side panel

local _, privateDB = ...

local M = {}

-- Bound lazily in Init (widget modules load AFTER this file in the TOC).
local BudgetStrip, Breadcrumb, Canvas, FloorSwitch, SidePanel, Landing
local Store, IDs

local function _faction()
    local f = UnitFactionGroup("player")
    if f == "Alliance" then return "alliance" elseif f == "Horde" then return "horde" end
    return nil
end

local function _currentHouseID()
    local f = _faction()
    if not f then return nil end
    return "house:" .. f
end

local function _hasRooms()
    local h = _currentHouseID()
    if not h then return false end
    local entries = Store.GetRoomsForHouse(h)
    return #entries > 0
end

function M:Init(mainFrame)
    if self._initialized then return end
    self._initialized = true

    -- Bind widget references lazily so TOC order doesn't matter.
    BudgetStrip = privateDB.HDG_BudgetStrip
    Breadcrumb  = privateDB.HDG_Breadcrumb
    Canvas      = privateDB.HDG_ArchitectCanvas
    FloorSwitch = privateDB.HDG_FloorSwitcher
    SidePanel   = privateDB.HDG_RoomSidePanel
    Landing     = privateDB.HDG_ProjectsLanding
    Store       = privateDB.HDG_ProjectsStore
    IDs         = privateDB.HDG_ProjectsIDs

    local UI = _G.HousingDecorGuide and _G.HousingDecorGuide.UI
    local layout = UI and UI:TabPanel(mainFrame, { hasHeaderBar = false })
    self.panel = layout and layout.frame or mainFrame
    self.panel:Hide()

    -- Budget strip docks at very top
    self.budget = BudgetStrip
    self.budget:Create(self.panel)

    -- Breadcrumb under strip
    local bcHost = CreateFrame("Frame", nil, self.panel)
    bcHost:SetPoint("TOPLEFT",  self.budget.frame, "BOTTOMLEFT",  0, 0)
    bcHost:SetPoint("TOPRIGHT", self.budget.frame, "BOTTOMRIGHT", 0, 0)
    bcHost:SetHeight(22)
    self.bcHost = bcHost
    self.breadcrumb = Breadcrumb
    self.breadcrumb:Create(bcHost)

    -- Content host (everything below breadcrumb)
    local content = CreateFrame("Frame", nil, self.panel)
    content:SetPoint("TOPLEFT",     bcHost,      "BOTTOMLEFT",  0, -2)
    content:SetPoint("BOTTOMRIGHT", self.panel,  "BOTTOMRIGHT", 0,  0)
    self.content = content

    -- Architect view: canvas + floor switcher + side panel layered on content
    self.canvas = Canvas
    self.canvas:Create(content)
    self.floors = FloorSwitch
    self.floors:Create(content)
    self.sidePanel = SidePanel
    self.sidePanel:Create(content)

    self.canvas:SetOnRoomClick(function(roomID)
        Store.SetUIField("selectedRoomID", roomID)
        self.sidePanel:Show(roomID)
        self:_RefreshBreadcrumb()
    end)
    self.floors:SetOnFloorSelected(function(floor)
        self:_SetActiveFloor(floor)
    end)

    -- Landing view: empty-state fallback only
    self.landing = Landing
    self.landing:Create(content)
end

function M:_SetActiveFloor(floor)
    self.activeFloor = floor
    self.canvas:Refresh(floor)
    self.floors:Refresh(_currentHouseID(), floor)
    self:_RefreshBreadcrumb()
end

function M:_RefreshBreadcrumb()
    local segments = {
        { label = "Projects",  key = "projects"  },
        { label = "Architect", key = "architect" },
    }
    local houseID = _currentHouseID()
    if houseID then
        local faction = IDs.parseHouseID(houseID) or ""
        local title = faction:gsub("^%l", string.upper)
        segments[#segments + 1] = { label = title, key = "house" }
    end
    if self.activeFloor then
        segments[#segments + 1] = { label = "F" .. self.activeFloor, key = "floor" }
    end
    local ui = Store.GetUI()
    if ui.selectedRoomID then
        local parsed = IDs.parsePath(ui.selectedRoomID) or {}
        segments[#segments + 1] = {
            label = "Room " .. (parsed.roomShortID or "?"),
            key   = "room",
        }
    end
    self.breadcrumb:SetSegments(segments, function(key, _)
        if key == "architect" or key == "house" or key == "floor" or key == "projects" then
            -- Pop back: clear room selection
            Store.SetUIField("selectedRoomID", nil)
            self.sidePanel:Hide()
            self:_RefreshBreadcrumb()
        end
    end)
end

function M:Show()
    if not self.panel then return end
    self.panel:Show()
    self.budget:Refresh()

    if _hasRooms() then
        -- Architect view
        self.landing:Hide()
        if self.canvas.frame then self.canvas.frame:Show() end
        if self.floors.frame then self.floors.frame:Show() end

        local ui = Store.GetUI()
        local floor = ui.selectedFloor or 1
        self:_SetActiveFloor(floor)

        if ui.selectedRoomID then
            self.sidePanel:Show(ui.selectedRoomID)
        else
            self.sidePanel:Hide()
        end
    else
        -- Landing (empty state)
        if self.canvas.frame then self.canvas.frame:Hide() end
        if self.floors.frame then self.floors.frame:Hide() end
        self.sidePanel:Hide()
        self.landing:Show()
        self.breadcrumb:SetSegments({ { label = "Projects", key = "projects" } }, nil)
    end
end

function M:Hide()
    if self.landing   and self.landing.Hide        then self.landing:Hide()        end
    if self.sidePanel                              then self.sidePanel:Hide()      end
    if self.canvas    and self.canvas.frame        then self.canvas.frame:Hide()   end
    if self.floors    and self.floors.frame        then self.floors.frame:Hide()   end
    if self.panel                                  then self.panel:Hide()          end
end

privateDB.HDG_ProjectsTab = M
