-- HDG_HT_W_CraftableNow.lua  (C1)
-- "Craftable Now" — big-number headline of recipes you can craft RIGHT now,
-- followed by a list of the player's professions. Each profession row has
-- a recipe-book button that opens that profession's window.

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
privateDB.HDG_HT.Widgets = privateDB.HDG_HT.Widgets or {}
local W = { id = "craftableNow", title = "Craftable Now" }
privateDB.HDG_HT.Widgets.CraftableNow = W

local NUMBER_BLOCK_H = 50    -- vertical space for the big number + sub-line
local ROW_HEIGHT     = 22
local ROW_GAP        = 1
local ICON_SIZE      = 18
local BOOK_SIZE      = 18

-- Returns ordered list of player's profession names. Skips Archaeology +
-- Fishing (not in HDG_ExpansionData.PROFESSION_IDS).
local function GetPlayerProfessions()
    local ED = _G.HDG_ExpansionData
    if not (ED and GetProfessions and GetProfessionInfo) then return {} end
    local out = {}
    local prof1, prof2, _, _, cooking = GetProfessions()
    for _, idx in ipairs({ prof1, prof2, cooking }) do
        if idx then
            -- 2nd return is the spellbook icon FileDataID (e.g. Cooking
            -- 4620671, Enchanting 4620672, Engineering 4620673) — same icon
            -- Blizzard shows next to the profession in the spellbook, which
            -- is what we want here. Skip the Mobile-* atlas lookup entirely.
            local name, icon, _, _, _, _, skillLine = GetProfessionInfo(idx)
            if name and ED.PROFESSION_IDS[name] then
                out[#out + 1] = { name = name, icon = icon, skillLine = skillLine }
            end
        end
    end
    return out
end

function W.Create(parent, _)
    local self = { rows = {} }
    local Theme  = privateDB.HDG_HT.Theme
    local scheme = Theme.GetCurrentScheme()

    self.frame = CreateFrame("Frame", nil, parent)
    self.frame:SetAllPoints(parent)

    -- ====== Top: BigNumber headline ======
    self.number = self.frame:CreateFontString(nil, "OVERLAY", "GameFontNormalHuge")
    self.number:SetPoint("TOP", self.frame, "TOP", 0, -4)
    Theme.ApplyFont(self.number, scheme, "header", 22)

    self.label = self.frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    self.label:SetPoint("TOP", self.number, "BOTTOM", 0, -2)
    self.label:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b)
    Theme.ApplyFont(self.label, scheme, "small")

    self.sub = self.frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    self.sub:SetPoint("TOP", self.label, "BOTTOM", 0, -2)
    self.sub:SetTextColor(scheme.text_dim.r, scheme.text_dim.g, scheme.text_dim.b)
    Theme.ApplyFont(self.sub, scheme, "small")

    -- Divider between the big number and the profession list.
    self.divider = self.frame:CreateTexture(nil, "ARTWORK")
    self.divider:SetTexture("Interface\\Buttons\\WHITE8x8")
    self.divider:SetSize(1, 1)
    self.divider:SetPoint("TOPLEFT",  self.frame, "TOPLEFT",  4, -NUMBER_BLOCK_H)
    self.divider:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -4, -NUMBER_BLOCK_H)
    self.divider:SetHeight(1)
    self.divider:SetVertexColor(scheme.border.r, scheme.border.g, scheme.border.b, 0.5)

    -- ====== Bottom: profession rows ======
    self.rowHost = CreateFrame("Frame", nil, self.frame)
    self.rowHost:SetPoint("TOPLEFT",  self.frame, "TOPLEFT",  0, -(NUMBER_BLOCK_H + 2))
    self.rowHost:SetPoint("BOTTOMRIGHT", self.frame, "BOTTOMRIGHT", 0, 0)

    local function ensureRow(i)
        if self.rows[i] then return self.rows[i] end
        local row = CreateFrame("Frame", nil, self.rowHost)
        row:SetHeight(ROW_HEIGHT)
        row:SetPoint("TOPLEFT",  self.rowHost, "TOPLEFT",  0, -((i - 1) * (ROW_HEIGHT + ROW_GAP)))
        row:SetPoint("TOPRIGHT", self.rowHost, "TOPRIGHT", 0, -((i - 1) * (ROW_HEIGHT + ROW_GAP)))

        row.icon = row:CreateTexture(nil, "ARTWORK")
        row.icon:SetSize(ICON_SIZE, ICON_SIZE)
        row.icon:SetPoint("LEFT", row, "LEFT", 2, 0)

        row.label = row:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        row.label:SetPoint("LEFT",  row.icon, "RIGHT", 6, 0)
        row.label:SetPoint("RIGHT", row, "RIGHT", -BOOK_SIZE - 12, 0)
        row.label:SetJustifyH("LEFT")
        row.label:SetWordWrap(false)
        row.label:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b)
        Theme.ApplyFont(row.label, scheme, "small")

        row.bookBtn = CreateFrame("Button", nil, row)
        row.bookBtn:SetSize(BOOK_SIZE, BOOK_SIZE)
        row.bookBtn:SetPoint("RIGHT", row, "RIGHT", -4, 0)
        row.bookBtn.tex = row.bookBtn:CreateTexture(nil, "ARTWORK")
        row.bookBtn.tex:SetAllPoints()
        row.bookBtn.tex:SetAtlas("UI-HUD-MicroMenu-SpellbookAbilities-Up")
        row.bookBtn:SetScript("OnEnter", function(slf)
            if slf.tex then slf.tex:SetAtlas("UI-HUD-MicroMenu-SpellbookAbilities-Mouseover") end
            if slf._tooltip then
                GameTooltip:SetOwner(slf, "ANCHOR_RIGHT")
                GameTooltip:AddLine(slf._tooltip)
                GameTooltip:Show()
            end
        end)
        row.bookBtn:SetScript("OnLeave", function(slf)
            if slf.tex then slf.tex:SetAtlas("UI-HUD-MicroMenu-SpellbookAbilities-Up") end
            GameTooltip:Hide()
        end)

        row:Hide()
        self.rows[i] = row
        return row
    end

    function self:Refresh(snap)
        -- BigNumber section
        local scheme2 = Theme.GetCurrentScheme()
        local success = scheme2.success or scheme2.accent
        local textDim = scheme2.text_dim
        local c = snap and snap.craftable or {}
        local n = c.canCraftNow or 0
        if n > 0 then
            self.number:SetText(tostring(n))
            self.number:SetTextColor(success.r, success.g, success.b)
            self.label:SetText("decor items now")
            if (c.almostCraftable or 0) > 0 then
                self.sub:SetText(string.format("+%d almost craftable", c.almostCraftable))
                self.sub:Show()
            else
                self.sub:Hide()
            end
        else
            self.number:SetText("0")
            self.number:SetTextColor(textDim.r, textDim.g, textDim.b)
            self.label:SetText("nothing in your bags")
            self.sub:Hide()
        end

        -- Profession rows
        local profs = GetPlayerProfessions()
        for i, p in ipairs(profs) do
            local row = ensureRow(i)
            -- Spellbook icon FileDataID (from GetProfessionInfo) — the same
            -- icon shown in the profession tooltip. Crops via TexCoord to
            -- trim the default 4-px transparent margin off the file.
            row.icon:SetTexture(p.icon or "Interface\\Icons\\INV_Misc_QuestionMark")
            row.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
            row.label:SetText(p.name)
            row.bookBtn._tooltip = "Open " .. p.name
            local profName = p.name
            row.bookBtn:SetScript("OnClick", function()
                if _G.HDG_Data and _G.HDG_Data.OpenProfessionAtRecipe then
                    _G.HDG_Data.OpenProfessionAtRecipe(profName, nil)
                end
            end)
            row:Show()
        end
        for i = #profs + 1, #self.rows do self.rows[i]:Hide() end
    end

    function self:Resize(_, _) end
    return self
end

return W
