-- 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.label       = "Login Digest"
Tab.WINDOW_SIZE = { width = 560, height = 440, locked = true }

-- 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
    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
