-- Toast.lua -- Pop up toast --
-- WanderBook: Forge your path through Azeroth with your own goals. --
-- Made by Blakmane. May 2025. --

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------

WanderBookToast = {}
local toastQueue = {}
local isToastActive = false
local ShowNextToast


local toastFrame = CreateFrame("Frame", nil, UIParent, "BackdropTemplate")
toastFrame:SetSize(350, 110)
toastFrame:SetPoint("TOP", UIParent, "TOP", 0, -100)
toastFrame:SetBackdrop(nil)
toastFrame.bg = toastFrame:CreateTexture(nil, "BACKGROUND")
toastFrame.bg:SetAllPoints(true)
toastFrame.bg:SetTexture("Interface\\AddOns\\WanderBook\\Media\\toast.tga")
toastFrame:SetFrameStrata("BACKGROUND")
toastFrame:SetFrameLevel(0)

-- Left icon
toastFrame.leftIcon = toastFrame:CreateTexture(nil, "ARTWORK")
toastFrame.leftIcon:SetSize(40, 40)
toastFrame.leftIcon:SetPoint("LEFT", toastFrame, "LEFT", 24, -2)

-- Shadow overlay behind icon
toastFrame.leftIconShadow = toastFrame:CreateTexture(nil, "ARTWORK", nil, -1)
toastFrame.leftIconShadow:SetSize(60, 60)
toastFrame.leftIconShadow:SetPoint("CENTER", toastFrame.leftIcon, "CENTER", 0, -4)
toastFrame.leftIconShadow:SetTexture("Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-shadow.tga")
toastFrame.leftIconShadow:SetVertexColor(0, 0, 0, 0.3)

-- Border overlay
toastFrame.leftIconBorder = toastFrame:CreateTexture(nil, "OVERLAY")
toastFrame.leftIconBorder:SetSize(50, 50)
toastFrame.leftIconBorder:SetPoint("CENTER", toastFrame.leftIcon, "CENTER")
toastFrame.leftIconBorder:SetTexture("Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-wood.tga")
toastFrame.leftIconBorder:SetAlpha(1)

-- Right icon (placeholder for future feature)
toastFrame.rightIcon = toastFrame:CreateTexture(nil, "ARTWORK")
toastFrame.rightIcon:SetSize(60, 60)
toastFrame.rightIcon:SetPoint("RIGHT", toastFrame, "RIGHT", -18, -2)

-- Center text block: title
toastFrame.title = toastFrame:CreateFontString(nil, "OVERLAY")
toastFrame.title:SetPoint("TOP", toastFrame, "TOP", 0, -35)
toastFrame.title:SetFont("Fonts\\FRIZQT__.TTF", 14)
toastFrame.title:SetTextColor(1, 1, 1)
toastFrame.title:SetShadowOffset(0, -2)
toastFrame.title:SetShadowColor(0, 0, 0, .6)
toastFrame.title:SetJustifyH("CENTER")
toastFrame.title:SetWidth(200)
toastFrame.title:SetWordWrap(true)
toastFrame.title:SetNonSpaceWrap(true)
toastFrame.title:SetMaxLines(2)
toastFrame.title:SetHeight(32)

-- Center text block: date and character (smaller)
toastFrame.subText = toastFrame:CreateFontString(nil, "OVERLAY")
toastFrame.subText:SetPoint("TOP", toastFrame, "TOP", 0, -70)
toastFrame.subText:SetFont("Fonts\\FRIZQT__.TTF", 10)
toastFrame.subText:SetJustifyH("CENTER")
fontPath, _, fontFlags = toastFrame.subText:GetFont()
toastFrame.subText:SetFont(fontPath, 10, fontFlags)
toastFrame.subText:SetTextColor(0.35, 0.2, 0.1)

toastFrame:Hide()

-- ICON DONE --
local function GetRightIconByType(obj)
    if not obj or not obj.type then
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-check.tga"
    end

    local t = obj.type
    if t == "emote" then
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-hand.tga"
    elseif t == "kill" then
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-skull.tga"
    elseif t == "crit" or t == "crittarget" then
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-thunder.tga"
    elseif t == "regen" then
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-heal.tga"
    elseif t == "damage" or t == "killwith" then
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-sword.tga"
    elseif t == "use" then
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-check.tga"
    elseif t == "loot" then
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-bag.tga"
    else
        return "Interface\\AddOns\\WanderBook\\Media\\icon-done-check.tga"
    end
end

-- ICON BORDERS --
local function GetToastBorderByType(obj)
    if not obj or not obj.type then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-wood.tga"
    end

    local t = obj.type
    if t == "emote" then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-wood.tga"
    elseif t == "kill" then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-blood.tga"
    elseif t == "crit" or t == "crittarget" then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-crit.tga"
    elseif t == "regen" then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-heal.tga"
    elseif t == "damage" or t == "killwith" then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-damage.tga"
    elseif t == "use" then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-stone.tga"
    elseif t == "loot" then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-cloth.tga"
    else
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-wood.tga"
    end
end

local function ShowNextToast()
    if #toastQueue == 0 then
        isToastActive = false
        return
    end

    local obj = table.remove(toastQueue, 1)
    WanderBookToast.Show(obj)
end

-- AUTOHIDE --
function WanderBookToast.Show(obj, noAutoHide)
    if not obj then return end

    if isToastActive then
        table.insert(toastQueue, obj)
        return
    end

    isToastActive = true

    local by = obj.completedBy or UnitName("player")
    local dateStr = obj.completedOn or date("%Y-%m-%d %H:%M")

    toastFrame.title:SetText(obj.name or "Objective")
    toastFrame.subText:SetText(string.format("by %s on %s", by, dateStr))

    local iconTex = obj.icon or obj.selectedIcon or "Interface\\Icons\\INV_Misc_QuestionMark"
    toastFrame.leftIcon:SetTexture(iconTex)
    toastFrame.rightIcon:SetTexture(GetRightIconByType(obj))
    toastFrame.leftIconBorder:SetTexture(GetToastBorderByType(obj))

    toastFrame:Show()
    PlaySound(SOUNDKIT.UI_GARRISON_TOAST_MISSION_COMPLETE)

    -- Only auto-hide if not explicitly disabled
    if not noAutoHide then
        C_Timer.After(4, function()
            UIFrameFadeOut(toastFrame, 1, 1, 0)
            C_Timer.After(1, function()
                toastFrame:Hide()
                toastFrame:SetAlpha(1)
                isToastActive = false
                ShowNextToast()
            end)
        end)
    else
        -- If noAutoHide was used, still allow queue progression manually
        isToastActive = false
        ShowNextToast()
    end
end

-- SLASH COMMANDS --
SLASH_WANDERBOOKTOAST1 = "/wbtoast"
SlashCmdList["WANDERBOOKTOAST"] = function()
    if toastFrame:IsShown() then
        toastFrame:Hide()
    else
        local types = { "kill", "loot", "emote", "use", "regen", "damage", "crit" }
        local dummyObj = {
            name = "123456789012345678901234567890",
            completedBy = UnitName("player"),
            completedOn = date("%Y-%m-%d"),
            icon = 134400,
            type = types[math.random(#types)]
        }
        WanderBookToast.Show(dummyObj, true)
    end
end


