-- modules/Mail.lua
-- Mail datatext adapted from ElvUI for Simple DataTexts (BDT)
local _, ns = ...
local BDT = ns.DataTexts
local L = BDT.L

local mod = {}

----------------------------------------------------
-- WoW API locals
----------------------------------------------------
local HasNewMail = HasNewMail
local GetLatestThreeSenders = GetLatestThreeSenders
local HAVE_MAIL_FROM = HAVE_MAIL_FROM
local MAIL_LABEL = MAIL_LABEL

----------------------------------------------------
-- File locals
----------------------------------------------------
local moduleName = "Mail"

----------------------------------------------------
-- Module Config Settings
----------------------------------------------------
local function SetupModuleConfig()
    BDT.ModuleRegistry:GlobalModuleSettings(moduleName)
end

SetupModuleConfig()

----------------------------------------------------
-- Module Creation
----------------------------------------------------
function mod.Create(slotFrame)
    local f = CreateFrame("Frame", nil, slotFrame)
    f:SetAllPoints(slotFrame)
    f:EnableMouse(false)

    local text = slotFrame.text
    if not text then
        text = slotFrame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
        text:SetPoint("CENTER")
        slotFrame.text = text
    end

    ----------------------------------------------------
    -- Event Handler
    ----------------------------------------------------
    local function OnEvent(self, event, ...)
        local mailText = HasNewMail() and L["New Mail"] or L["No Mail"]
        text:SetText(BDT.FormatUtils:ColorModuleText(moduleName, mailText))
        BDT.FontManager:ApplyModuleFont(moduleName, text)
    end

    f.Update = function() OnEvent(f) end

    f:SetScript("OnEvent", OnEvent)
    f:RegisterEvent("MAIL_INBOX_UPDATE")
    f:RegisterEvent("UPDATE_PENDING_MAIL")
    f:RegisterEvent("MAIL_CLOSED")
    f:RegisterEvent("MAIL_SHOW")
    f:RegisterEvent("PLAYER_ENTERING_WORLD")

    ----------------------------------------------------
    -- Tooltip
    ----------------------------------------------------
    slotFrame:EnableMouse(true)
    slotFrame:SetScript("OnEnter", function(self)
        local anchor = BDT.FormatUtils:FindBestAnchorPoint(self)
        BDT.Tooltip:SetOwner(self, anchor)
        BDT.Tooltip:ClearLines()

        local senders = { GetLatestThreeSenders() }
        if next(senders) then
            local header = HasNewMail() and HAVE_MAIL_FROM or MAIL_LABEL
            BDT.FormatUtils:AddTooltipHeader(BDT.Tooltip, nil, header)
            BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, " ")

            for _, sender in pairs(senders) do
                BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, sender)
            end

            BDT.Tooltip:Show()
        end
    end)

    slotFrame:SetScript("OnLeave", function()
        BDT.Tooltip:Hide()
    end)

    OnEvent(f)

    return f
end

----------------------------------------------------
-- Register with BDT
----------------------------------------------------
BDT.ModuleRegistry:RegisterDatatext(moduleName, mod)

return mod
