-- TOG Tools — Login Digest tab (UI)
-- Per-field toggles for the one-line digest printed at PLAYER_LOGIN.

local _, addon = ...
local AceGUI = LibStub("AceGUI-3.0")

local BRAND = "|c" .. (addon.BrandColor or "ffFF8000")

local Tab = addon.modules["logindigest"]
Tab.tabKey      = "logindigest"
Tab.configKey   = "loginDigest"
Tab.label       = "Login Digest"
Tab.WINDOW_SIZE = { width = 560, height = 440, locked = true }
Tab.help = {
    title = "Login Digest",
    lines = {
        "Prints a one-line state-of-account summary in chat each time you log in.",
        "Tick the fields you want included: |cffffd700expiring mail|r (across all alts), |cffffd700guildies online|r, |cffffd700hearthstone bind|r, |cffffd700bag fullness|r, |cffffd700time since last logout|r.",
        "Each field renders only when it has something to say — e.g. the expiring-mail line is omitted entirely when no alts have mail about to expire.",
        "|cffffd700Master toggle|r at the top of this tab silences the digest line entirely without losing your per-field selections.",
    },
}

-- Field metadata for the toggle list. Order matches the engine's render
-- order so the tab visually mirrors the printed line.
local FIELDS = {
    { key = "mail",        label = "Mail expiring soon",
      desc = "Total mail across all alts expiring within the Mailbox tab's threshold (default 2 days)." },
    { key = "guildOnline", label = "Guild members online",
      desc = "Number of guildies currently online when you log in." },
    { key = "hearth",      label = "Hearthstone bind",
      desc = "Where this character's hearthstone is currently bound." },
    { key = "bags",        label = "Bag fullness",
      desc = "Percentage of carried-bag slots used (backpack + four bag slots, bank excluded)." },
    { key = "lastLogout",  label = "Last logout",
      desc = "How long ago this specific character last logged out." },
}

-- ---------------------------------------------------------------------------
-- Helpers
-- ---------------------------------------------------------------------------
local function cfg()
    return addon.DB.global.loginDigest
end

-- ---------------------------------------------------------------------------
-- Draw
-- ---------------------------------------------------------------------------
function Tab:Draw(container)
    local c = cfg()

    local scroll = AceGUI:Create("ScrollFrame")
    scroll:SetLayout("Flow")
    scroll:SetFullWidth(true)
    scroll:SetFullHeight(true)
    container:AddChild(scroll)

    -- ---- Master enable -------------------------------------------------
    local enableCheck = AceGUI:Create("CheckBox")
    enableCheck:SetLabel(BRAND .. "Enable Login Digest|r")
    enableCheck:SetDescription("Print a one-line summary in chat each time you log in.")
    enableCheck:SetValue(c.enabled)
    enableCheck:SetFullWidth(true)
    enableCheck:SetCallback("OnValueChanged", function(_, _, val)
        c.enabled = val
        -- Same flag the Settings > Modules toggle writes to; rebuild the
        -- main window so the tab disappears (or reappears) immediately.
        if addon.MainWindow and addon.MainWindow.Rebuild then
            addon.MainWindow:Rebuild()
        end
    end)
    scroll:AddChild(enableCheck)

    -- Spacer
    local sp1 = AceGUI:Create("Label")
    sp1:SetText(" ")
    sp1:SetFullWidth(true)
    scroll:AddChild(sp1)

    -- ---- Fields header -------------------------------------------------
    local fieldsHeader = AceGUI:Create("Label")
    fieldsHeader:SetText(BRAND .. "Fields shown|r")
    fieldsHeader:SetFullWidth(true)
    scroll:AddChild(fieldsHeader)

    local fieldsHint = AceGUI:Create("Label")
    fieldsHint:SetText("|cffaaaaaaA field is hidden automatically when it has nothing to report (no expiring mail, no guildies online, etc).|r")
    fieldsHint:SetFullWidth(true)
    scroll:AddChild(fieldsHint)

    -- Spacer
    local sp2 = AceGUI:Create("Label")
    sp2:SetText(" ")
    sp2:SetFullWidth(true)
    scroll:AddChild(sp2)

    -- ---- Per-field toggles ---------------------------------------------
    c.fields = c.fields or {}
    for _, f in ipairs(FIELDS) do
        local cb = AceGUI:Create("CheckBox")
        cb:SetLabel(f.label)
        cb:SetDescription(f.desc)
        cb:SetValue(c.fields[f.key] ~= false)
        cb:SetFullWidth(true)
        cb:SetCallback("OnValueChanged", function(_, _, val)
            c.fields[f.key] = val
        end)
        scroll:AddChild(cb)
    end

    -- Spacer
    local sp3 = AceGUI:Create("Label")
    sp3:SetText(" ")
    sp3:SetFullWidth(true)
    scroll:AddChild(sp3)

    -- ---- Test button ---------------------------------------------------
    local testBtn = AceGUI:Create("Button")
    testBtn:SetText("Print test digest")
    testBtn:SetRelativeWidth(0.4)
    testBtn:SetCallback("OnClick", function()
        if Tab.Print then Tab:Print() end
    end)
    scroll:AddChild(testBtn)

    local testHint = AceGUI:Create("Label")
    testHint:SetText("|cffaaaaaaPrints the digest using current data so you can preview the format.|r")
    testHint:SetRelativeWidth(0.6)
    scroll:AddChild(testHint)
end
