--[[ A short, account-wide regional completion toast. ]]

local _, ns = ...

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

local frame

local function Build()
    local toast = CreateFrame("Frame", nil, UIParent)
    toast:SetSize(456, 152)
    toast:SetPoint("TOP", UIParent, "TOP", 0, -120)
    toast:SetFrameStrata("DIALOG")
    toast:Hide()

    local art = toast:CreateTexture(nil, "BACKGROUND")
    art:SetAllPoints(toast)
    art:SetTexture(ns.UI.Theme.sightingAlertTexture)

    local icon = toast:CreateTexture(nil, "ARTWORK")
    icon:SetSize(58, 58)
    icon:SetPoint("LEFT", toast, "LEFT", 43, 1)
    icon:SetTexture("Interface\\Icons\\Ability_Hunter_BeastCall")
    icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)

    toast.Eyebrow = toast:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    toast.Eyebrow:SetPoint("TOPLEFT", toast, "TOPLEFT", 130, -42)
    toast.Eyebrow:SetText(ns.L["REGION_TOAST_EYEBROW"])
    toast.Eyebrow:SetTextColor(0.25, 0.86, 0.78)

    toast.Title = toast:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    toast.Title:SetPoint("TOPLEFT", toast.Eyebrow, "BOTTOMLEFT", 0, -3)
    toast.Title:SetPoint("RIGHT", toast, "RIGHT", -34, 0)
    toast.Title:SetJustifyH("LEFT")
    toast.Title:SetTextColor(1.00, 0.82, 0.30)

    toast.Detail = toast:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    toast.Detail:SetPoint("TOPLEFT", toast.Title, "BOTTOMLEFT", 0, -5)
    toast.Detail:SetTextColor(0.76, 0.72, 0.65)
    return toast
end

function RegionToast:Show(region)
    frame = frame or Build()
    frame.Title:SetText(region.name)
    frame.Detail:SetText(ns.L["REGION_TOAST_DETAIL"]:format(region.total))
    frame:Show()
    frame:SetAlpha(1)

    C_Timer.After(5, function()
        if not frame or not frame:IsShown() then return end
        if UIFrameFadeOut then
            UIFrameFadeOut(frame, 0.8, 1, 0)
            C_Timer.After(0.9, function() if frame then frame:Hide() end end)
        else
            frame:Hide()
        end
    end)

    local settings = ns.Database:GetSettings()
    local sound = SOUNDKIT and (SOUNDKIT.UI_ACHIEVEMENT_TOAST
        or SOUNDKIT.ACHIEVEMENT_EARNED)
    if sound and (not settings or settings.soundEnabled ~= false) then
        PlaySound(sound, "Master")
    end
end

function RegionToast:OnEnable()
    ns.Events:Subscribe("REGION_COMPLETED", self, function(_, region)
        self:Show(region)
    end)
end
