-- VDS_LayoutConfig_Warehouse
-- ============================================================================
-- Stock view widgets: mode bar + body scroll + herb side panel.
-- stock.herbs.list selector emits kind="herbListItem" explicitly; nil kind
-- would silently skip rows (scrollBoxList uses rowKinds[elementData.kind] directly).

VDS = VDS or {}
local LC = VDS.LayoutConfig

-- ===== Mode toggle row =====

LC.sections["stock.modeBar.row"] = {
    ["in"]  = "stockModeBarPanel",
    order   = 10,
    layout  = "horizontal",
    padding = { left = 8, top = 2, right = 8, bottom = 2 },
    gap     = 0,
}

LC.widgets["stock.modeToggle"] = {
    kind    = "tabBar",
    ["in"]  = "stock.modeBar.row",
    order   = 10,
    binding = "stock.modeToggle.chips",
    width   = "fill",
    tooltip = { recipe = "StockModeToggle" },
}

LC.widgets["stock.collapseAllBtn"] = {
    kind   = "button",
    ["in"] = "stock.modeBar.row",
    order  = 20,
    text   = "Collapse",
    font   = "caption",
    width  = 70,
    height = 22,
    tooltip = { recipe = "StockCollapseAll" },
}

LC.widgets["stock.expandAllBtn"] = {
    kind   = "button",
    ["in"] = "stock.modeBar.row",
    order  = 30,
    text   = "Expand",
    font   = "caption",
    width  = 70,
    height = 22,
    tooltip = { recipe = "StockExpandAll" },
}

-- ===== Status footer =====

-- Buy-gap toggle (above the Send-to-AH button). Default on: subtract on-hand
-- (bag + bank + reagent bank + warband) from herb totals so the popout + AH
-- list show "what to buy", not gross craft requirements. Craft mode only --
-- Buy mode's dye deficit is always net (you wouldn't buy dyes you already own).
LC.sections["stock.buyGap.row"] = {
    ["in"]   = "stockControlsPanel",
    order    = 5,
    layout   = "horizontal",
    padding  = { left = 8, top = 4, right = 8, bottom = 0 },
    gap      = 8,
    visible  = "stock.mode.isCraft",
}

LC.widgets["stock.buyGap.spacer"] = {
    kind   = "spacer",
    ["in"] = "stock.buyGap.row",
    order  = 10,
    width  = "fill",
    tooltip = false,
}

LC.widgets["stock.buyGap.check"] = {
    kind    = "checkbox",
    ["in"]  = "stock.buyGap.row",
    order   = 20,
    binding = "stock.buyGap",
    label   = "Use owned herbs",
    width   = 140,
    height  = 20,
    tooltip = { recipe = "StockBuyGap" },
    action  = "STOCK_SET_BUY_GAP",
}

LC.sections["stock.footer.row"] = {
    ["in"]  = "stockControlsPanel",
    order   = 10,
    layout  = "horizontal",
    padding = { left = 8, top = 6, right = 8, bottom = 6 },
    gap     = 8,
}

LC.widgets["stock.summary"] = {
    kind    = "label",
    ["in"]  = "stock.footer.row",
    order   = 10,
    binding = "stock.summary.label",
    font    = "small",
    width   = 160,
    tooltip = false,
}

LC.widgets["stock.controlsSpacer"] = {
    kind   = "spacer",
    ["in"] = "stock.footer.row",
    order  = 15,
    width  = "fill",
    tooltip = false,
}

LC.widgets["stock.restock"] = {
    kind    = "button",
    ["in"]  = "stock.footer.row",
    order   = 20,
    binding = "stock.restock.label",
    font    = "body",
    width   = 140,
    height  = 26,
    tooltip = { recipe = "StockRestock" },
}

-- ===== Body scroll =====

LC.sections["stock.body.stack"] = {
    ["in"]  = "stockBodyPanel",
    order   = 10,
    layout  = "vertical",
    padding = { left = 0, top = 0, right = 0, bottom = 0 },
    gap     = 0,
}

-- Have column dropped (group rows show counts as "X/Y - need Z" anyway).
LC.sections["stock.list.header.row"] = {
    ["in"]   = "stock.body.stack",
    order    = 5,
    layout   = "horizontal",
    padding  = { left = 8, top = 4, right = 8, bottom = 2 },
    gap      = 0,
    height   = 20,
}
LC.widgets["stock.list.header.resetAll"] = {
    kind   = "button",
    ["in"] = "stock.list.header.row",
    order  = 5,
    text   = "Reset",
    font   = "caption",
    width  = 48,
    height = 18,
    tooltip = { recipe = "StockResetTargets" },
}
LC.widgets["stock.list.header.color"] = {
    kind     = "label",
    ["in"]   = "stock.list.header.row",
    order    = 10,
    text     = "Color",
    font     = "caption",
    width    = 60,
    height   = 14,
    justifyH = "LEFT",
    tooltip = false,
}
LC.widgets["stock.list.header.midSpacer"] = {
    kind   = "spacer",
    ["in"] = "stock.list.header.row",
    order  = 15,
    width  = "fill",
    tooltip = false,
}
LC.widgets["stock.list.header.minusAll"] = {
    kind   = "iconButton",
    ["in"] = "stock.list.header.row",
    order  = 20,
    atlas  = "communities-chat-icon-minus",
    width  = 18,
    height = 18,
    tooltip = { recipe = "StockMinusAll" },
}
LC.widgets["stock.list.header.target"] = {
    kind     = "label",
    ["in"]   = "stock.list.header.row",
    order    = 30,
    text     = "Target",
    font     = "caption",
    width    = 44,
    height   = 14,
    justifyH = "CENTER",
    tooltip = false,
}
LC.widgets["stock.list.header.plusAll"] = {
    kind   = "iconButton",
    ["in"] = "stock.list.header.row",
    order  = 35,
    atlas  = "communities-chat-icon-plus",
    width  = 18,
    height = 18,
    tooltip = { recipe = "StockPlusAll" },
}
LC.widgets["stock.list.header.need"] = {
    kind     = "label",
    ["in"]   = "stock.list.header.row",
    order    = 40,
    text     = "Need",
    font     = "caption",
    width    = 50,
    height   = 14,
    justifyH = "RIGHT",
    tooltip = false,
}

LC.widgets["stock.list"] = {
    kind     = "scrollBoxList",
    ["in"]   = "stock.body.stack",
    order    = 10,
    binding  = "stock.row.array",
    height   = "fill",
    extent   = 24,
    rowKinds = {
        groupHead    = "warehouseGroupHeaderRow",
        warehouseRow = "warehouseDyeRow",
    },
    tooltip = false,
}

-- ===== Herb side panel =====

LC.sections["stock.herbs.stack"] = {
    ["in"]  = "stockHerbsPanel",
    order   = 10,
    layout  = "vertical",
    padding = { left = 0, top = 8, right = 0, bottom = 0 },
    gap     = 0,
}

LC.widgets["stock.herbs.title"] = {
    kind    = "label",
    ["in"]  = "stock.herbs.stack",
    order   = 10,
    text    = "Herbs needed to craft target",
    font    = "subheading",
    height  = 24,
    tooltip = false,
}

LC.widgets["stock.herbs.sub"] = {
    kind    = "label",
    ["in"]  = "stock.herbs.stack",
    order   = 15,
    binding = "stock.herbs.summary.label",
    font    = "caption",
    height  = 16,
    tooltip = false,
}

-- Herb list column header. Widths match row factory: name (120), qty (44), cost (right).
LC.sections["stock.herbs.header.row"] = {
    ["in"]   = "stock.herbs.stack",
    order    = 17,
    layout   = "horizontal",
    padding  = { left = 8, top = 4, right = 8, bottom = 2 },
    gap      = 4,
    height   = 20,
}
LC.widgets["stock.herbs.header.name"] = {
    kind   = "label",
    ["in"] = "stock.herbs.header.row",
    order  = 10,
    text   = "Herb",
    font   = "caption",
    width  = 120,
    height = 14,
    tooltip = false,
}
LC.widgets["stock.herbs.header.qty"] = {
    kind     = "label",
    ["in"]   = "stock.herbs.header.row",
    order    = 20,
    text     = "Need",
    font     = "caption",
    width    = 48,
    height   = 14,
    justifyH = "RIGHT",
    tooltip = false,
}
LC.widgets["stock.herbs.header.spacer"] = {
    kind   = "spacer",
    ["in"] = "stock.herbs.header.row",
    order  = 30,
    width  = "fill",
    tooltip = false,
}
LC.widgets["stock.herbs.header.cost"] = {
    kind     = "label",
    ["in"]   = "stock.herbs.header.row",
    order    = 40,
    text     = "Cost",
    font     = "caption",
    width    = 44,
    height   = 14,
    justifyH = "RIGHT",
    tooltip = false,
}

LC.widgets["stock.herbs.list"] = {
    kind     = "scrollBoxList",
    ["in"]   = "stock.herbs.stack",
    order    = 20,
    binding  = "stock.herbs.list",
    height   = "fill",
    extent   = 40,
    rowKinds = { herbListItem = "herbListItemRow" },
    -- Declarative async-name kick: rows where data.name is nil get
    -- Request'd via ItemNameResolver. Selector emits row.name = nil on
    -- cache miss (see VDS_Selectors_Warehouse). Middleware handles dedup +
    -- debounced batch dispatch.
    resolveAsync = {
        { idField = "itemID", valueField = "name", resolver = VDS.ItemNameResolver },
    },
    tooltip = false,
}

-- Footer: two stacked rows showing dual totals (gross + net) so the "buy gap"
-- is visible at a glance regardless of the buyGap toggle state.
--
-- Row 1 (Required): what the planned crafts actually consume.
-- Row 2 (To buy):   what's left after subtracting on-hand bag/bank/reagent/warband.

-- Row 1: Required (gross)
LC.sections["stock.herbs.footer.required.row"] = {
    ["in"]  = "stock.herbs.stack",
    order   = 30,
    layout  = "horizontal",
    padding = { left = 8, top = 6, right = 8, bottom = 0 },
    gap     = 4,
    height  = 22,
}
LC.widgets["stock.herbs.footer.required.name"] = {
    kind    = "label",
    ["in"]  = "stock.herbs.footer.required.row",
    order   = 10,
    text    = "Crafts will use",
    font    = "body",
    width   = 120,
    height  = 18,
    tooltip = false,
}
LC.widgets["stock.herbs.footer.required.qty"] = {
    kind     = "label",
    ["in"]   = "stock.herbs.footer.required.row",
    order    = 20,
    binding  = "stock.herbs.footer.required.qty.label",
    font     = "body",
    width    = 48,
    height   = 18,
    justifyH = "RIGHT",
    tooltip  = false,
}
LC.widgets["stock.herbs.footer.required.spacer"] = {
    kind    = "spacer",
    ["in"]  = "stock.herbs.footer.required.row",
    order   = 30,
    width   = "fill",
    tooltip = false,
}
LC.widgets["stock.herbs.footer.required.cost"] = {
    kind     = "label",
    ["in"]   = "stock.herbs.footer.required.row",
    order    = 40,
    binding  = "stock.herbs.footer.required.cost.label",
    font     = "body",
    width    = 44,
    height   = 18,
    justifyH = "RIGHT",
    tooltip  = false,
}

-- Row 2: To buy (net)
LC.sections["stock.herbs.footer.buy.row"] = {
    ["in"]  = "stock.herbs.stack",
    order   = 35,
    layout  = "horizontal",
    padding = { left = 8, top = 2, right = 8, bottom = 6 },
    gap     = 4,
    height  = 22,
}
LC.widgets["stock.herbs.footer.buy.name"] = {
    kind    = "label",
    ["in"]  = "stock.herbs.footer.buy.row",
    order   = 10,
    text    = "You need to buy",
    font    = "body",
    width   = 120,
    height  = 18,
    tooltip = false,
}
LC.widgets["stock.herbs.footer.buy.qty"] = {
    kind     = "label",
    ["in"]   = "stock.herbs.footer.buy.row",
    order    = 20,
    binding  = "stock.herbs.footer.buy.qty.label",
    font     = "body",
    width    = 48,
    height   = 18,
    justifyH = "RIGHT",
    tooltip  = false,
}
LC.widgets["stock.herbs.footer.buy.spacer"] = {
    kind    = "spacer",
    ["in"]  = "stock.herbs.footer.buy.row",
    order   = 30,
    width   = "fill",
    tooltip = false,
}
LC.widgets["stock.herbs.footer.buy.cost"] = {
    kind     = "label",
    ["in"]   = "stock.herbs.footer.buy.row",
    order    = 40,
    binding  = "stock.herbs.footer.buy.cost.label",
    font     = "body",
    width    = 44,
    height   = 18,
    justifyH = "RIGHT",
    tooltip  = false,
}
