-- Notifications tab. A single full-width feed (RLRM-style): one scrolling list,
-- newest first, each row = severity dot + timestamp + full message. Data comes
-- from g_pastureHerding._notifications (prepended newest-first by
-- PastureHerding:pushNotification). A bottom-bar "Clear all" wipes the archive.

PHMenuNotificationsFrame = {}
local PHMenuNotificationsFrame_mt = Class(PHMenuNotificationsFrame, TabbedMenuFrameElement)

local modDirectory = g_currentModDirectory

-- Grey dot for the empty-state / colourless entries.
local GREY = { 0.6, 0.6, 0.6, 1.0 }

local function entries()
    local f = rawget(_G, "g_pastureHerding")
    return (f and f._notifications) or {}
end

local function tr(key, fallback)
    if g_i18n ~= nil and g_i18n.hasText ~= nil and g_i18n:hasText(key) then
        return g_i18n:getText(key)
    end
    return fallback
end

function PHMenuNotificationsFrame.new(target, mt)
    local self = PHMenuNotificationsFrame:superClass().new(target, mt or PHMenuNotificationsFrame_mt)
    self.name = "PHMenuNotificationsFrame"
    self.hasCustomMenuButtons = true
    self.backButtonInfo = { inputAction = InputAction.MENU_BACK }
    self.menuButtonInfo = { self.backButtonInfo }
    return self
end

function PHMenuNotificationsFrame.setupGui()
    if g_gui == nil then return end
    local xmlPath = Utils.getFilename("gui/frames/PHMenuNotificationsFrame.xml", modDirectory)
    g_gui:loadGui(xmlPath, "PHMenuNotificationsFrame", PHMenuNotificationsFrame.new(), true)
end

function PHMenuNotificationsFrame:initialize() end

-- SmoothList data source.
function PHMenuNotificationsFrame:getNumberOfSections(list) return 1 end
function PHMenuNotificationsFrame:getNumberOfItemsInSection(list, section)
    return math.max(1, #entries())  -- always >= 1 so we can show the empty-state row
end

function PHMenuNotificationsFrame:populateCellForItemInSection(list, section, index, cell)
    if list ~= self.notifList then return end
    local items = entries()
    local dot  = cell:getAttribute("dot")
    local time = cell:getAttribute("time")
    local msg  = cell:getAttribute("msg")

    local function setDot(c)
        if dot ~= nil and type(dot.setImageColor) == "function" then
            pcall(function() dot:setImageColor(nil, c[1], c[2], c[3], c[4] or 1) end)
        end
    end

    if #items == 0 then
        setDot(GREY)
        if time and type(time.setText) == "function" then time:setText("") end
        if msg and type(msg.setText) == "function" then
            msg:setText(tr("ph_notif_empty", "No notifications yet - anything the mod tells you on-screen is archived here."))
        end
        return
    end

    local n = items[index]
    if n == nil then return end
    setDot((type(n.color) == "table") and n.color or GREY)
    if time and type(time.setText) == "function" then time:setText(n.stamp or "?") end
    if msg and type(msg.setText) == "function" then msg:setText(n.text or "") end
end

-- Clicking a row does nothing (everything is already shown inline); we just keep
-- the handler so the list is interactive/scrollable with focus.
function PHMenuNotificationsFrame:onNotifClick(list, section, index) end

function PHMenuNotificationsFrame:_buildButtonInfos()
    self.clearButtonInfo = {
        inputAction = InputAction.MENU_EXTRA_1,
        text = tr("ph_notif_clear", "Clear all"),
        callback = function() self:onClickClearAll() end,
        disabled = #entries() == 0,
    }
    self.menuButtonInfo = { self.backButtonInfo, self.clearButtonInfo }
    if type(self.setMenuButtonInfoDirty) == "function" then
        self:setMenuButtonInfoDirty()
    end
end

function PHMenuNotificationsFrame:onClickClearAll()
    local f = rawget(_G, "g_pastureHerding")
    if f ~= nil then f._notifications = {} end
    if self.notifList ~= nil and type(self.notifList.reloadData) == "function" then
        self.notifList:reloadData()
    end
    self:_buildButtonInfos()
end

function PHMenuNotificationsFrame:onFrameOpen()
    PHMenuNotificationsFrame:superClass().onFrameOpen(self)
    if self.notifList ~= nil and type(self.notifList.reloadData) == "function" then
        self.notifList:reloadData()
    end
    self:_buildButtonInfos()
end

function PHMenuNotificationsFrame:onFrameClose()
    PHMenuNotificationsFrame:superClass().onFrameClose(self)
end
