--[[ Field-guide onboarding: short, actionable and visible inside the journal. ]]

local _, ns = ...

local TutorialPanel = ns.NewModule("TutorialPanel")
ns.UI = ns.UI or {}
ns.UI.TutorialPanel = TutorialPanel

local Theme = ns.UI.Theme

local function AddText(parent, font, text, color, y, height)
    local label = parent:CreateFontString(nil, "ARTWORK", font)
    label:SetPoint("TOPLEFT", parent, "TOPLEFT", 24, y)
    label:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -24, y)
    label:SetJustifyH("LEFT")
    label:SetJustifyV("TOP")
    label:SetWordWrap(true)
    label:SetText(text)
    label:SetTextColor(color[1], color[2], color[3], color[4] or 1)
    label:SetHeight(height)
    return label
end

function TutorialPanel:Create(parent)
    local panel = { parent = parent, labels = {} }
    local title = AddText(parent, "GameFontNormalLarge", "", Theme.color.gold, -20, 28)
    local intro = AddText(parent, "GameFontHighlight", "", Theme.color.text, -56, 48)
    panel.labels = { title, intro }

    local steps = {}
    for index = 1, 3 do
        local card = Theme:CreatePanel(parent, Theme.color.card, Theme.color.line)
        card:SetHeight(76)
        -- The title itself carries the step number. A separate oversized
        -- numeral competed with the body baseline at several UI scales.
        local heading = AddText(card, "GameFontNormal", "", Theme.color.gold, -12, 20)
        heading:ClearAllPoints()
        heading:SetPoint("TOPLEFT", card, "TOPLEFT", 28, -14)
        heading:SetPoint("TOPRIGHT", card, "TOPRIGHT", -28, -14)
        local body = AddText(card, "GameFontHighlightSmall", "", Theme.color.text, -36, 42)
        body:ClearAllPoints()
        body:SetPoint("TOPLEFT", card, "TOPLEFT", 28, -39)
        body:SetPoint("TOPRIGHT", card, "TOPRIGHT", -28, -39)
        steps[index] = { card = card, heading = heading, body = body }
    end

    local automatic = Theme:CreatePanel(parent, Theme.color.panelSoft, Theme.color.tracker)
    -- Anchor this summary to the usable lower edge. Fixed Y offsets expanded
    -- beyond the journal at high UI scales even though the cards themselves fit.
    automatic:SetPoint("BOTTOMLEFT", parent, "BOTTOMLEFT", 24, 14)
    automatic:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", -24, 14)
    automatic:SetHeight(72)
    local automaticTitle = AddText(automatic, "GameFontNormal", "", Theme.color.tracker, -10, 18)
    local automaticText = AddText(automatic, "GameFontHighlightSmall", "", Theme.color.text, -31, 34)

    -- Build upward from the lower summary instead of assigning independent Y
    -- offsets. This keeps the third card above the summary at every UI scale.
    local below = automatic
    for index = #steps, 1, -1 do
        local card = steps[index].card
        card:SetPoint("BOTTOMLEFT", below, "TOPLEFT", 0, 14)
        card:SetPoint("BOTTOMRIGHT", below, "TOPRIGHT", 0, 14)
        below = card
    end

    function panel:Refresh()
        local L = ns.L
        title:SetText(L["GUIDE_TITLE"])
        intro:SetText(L["GUIDE_INTRO"])
        for index, step in ipairs(steps) do
            step.heading:SetText(L["GUIDE_STEP_" .. ({ "ONE", "TWO", "THREE" })[index] .. "_TITLE"])
            step.body:SetText(L["GUIDE_STEP_" .. ({ "ONE", "TWO", "THREE" })[index] .. "_TEXT"])
        end
        automaticTitle:SetText(L["GUIDE_AUTOMATIC_TITLE"])
        automaticText:SetText(L["GUIDE_AUTOMATIC_TEXT"])
    end
    panel:Refresh()
    return panel
end
