-- Main-Frame.lua -- Main frame UI --
-- WanderBook: Forge your path through Azeroth with your own goals. --
-- Made by Blakmane. May 2025. --

local _, addon = ...
WanderBook = addon

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- LOCALS & HELPERS + MAIN FRAME --

local page = 1
local pageSize = 5
local filteredObjectives = nil
local entrySlots = {}
local confirmPanel
local confirmLabel
local currentEditSlot = nil

-- MAIN PANEL --
local wbFrame = CreateFrame("Frame", "WanderBookUI", UIParent, "BasicFrameTemplateWithInset")
local regions = { wbFrame:GetRegions() }
for _, region in ipairs(regions) do
    if region:GetObjectType() == "Texture" then
        region:SetTexture(nil)
    end
end

local bgTex = wbFrame:CreateTexture(nil, "BACKGROUND")
bgTex:SetAllPoints(true)
bgTex:SetTexture("Interface\\AddOns\\WanderBook\\Media\\main-frame.tga")
wbFrame:SetSize(580, 630)
wbFrame:SetPoint("CENTER")
wbFrame:SetMovable(true)
wbFrame:EnableMouse(true)
wbFrame:RegisterForDrag("LeftButton")
wbFrame:SetScript("OnDragStart", wbFrame.StartMoving)
wbFrame:SetScript("OnDragStop", wbFrame.StopMovingOrSizing)
wbFrame:Hide()
table.insert(UISpecialFrames, "WanderBookUI")
wbFrame:SetFrameStrata("DIALOG")
wbFrame:SetFrameLevel(0)

wbFrame:HookScript("OnShow", function()
    PlaySound(SOUNDKIT.IG_SPELLBOOK_OPEN)
end)

wbFrame:HookScript("OnHide", function()
    PlaySound(SOUNDKIT.IG_SPELLBOOK_CLOSE)
end)

-- MATH ROUNDER HELPER --
local function ShortenGoalNumber(n)
    if not n then return "0" end
    if n >= 1000000 then
        return string.format("%.1fM", n / 1000000)
    elseif n >= 1000 then
        return string.format("%.1fk", n / 1000)
    else
        return string.format("%.0f", n) -- Round small numbers to integer
    end
end

-- OBJECTIVE TIER BGS --
local function ObjectiveSlotBackground(obj)
    if not obj or not obj.goal or obj.isCounter then
        return "Interface\\AddOns\\WanderBook\\Media\\obj-slot.tga"
    end

    local t = obj.type
    if t == "regen" or t == "damage" then
        return "Interface\\AddOns\\WanderBook\\Media\\obj-slot.tga"
    end

    if obj.goal >= 10000 then
        return "Interface\\AddOns\\WanderBook\\Media\\obj-slot-gold.tga"
    elseif obj.goal >= 500 then
        return "Interface\\AddOns\\WanderBook\\Media\\obj-slot-silver.tga"
    else
        return "Interface\\AddOns\\WanderBook\\Media\\obj-slot.tga"
    end
end

-- COMPLETED OBJECTIVE ICONS --
local function CompletedIcon(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 BORDER HELPER --
local function IconBorder(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 == "regen" or t == "damage" then
        -- These types skip tiering
        if t == "regen" then
            return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-heal.tga"
        else
            return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-damage.tga"
        end
    end

    if obj.goal >= 10000 then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-gold.tga"
    elseif obj.goal >= 500 then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-silver.tga"
    end

    if 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 == "use" or t == "usetarget" 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"
    elseif t == "emote" then
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-wood.tga"
    else
        return "Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-wood.tga"
    end
end

-- TITTLE TEXT --
--wbFrame.title = wbFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge")
--wbFrame.title:SetPoint("TOPLEFT", 16, -12)
--wbFrame.title:SetText("WanderBook")

-- INNER OBJECTIVE LIST FRAME --
local innerFrame = CreateFrame("Frame", nil, wbFrame)
innerFrame:SetSize(478, 415)
innerFrame:SetPoint("TOPLEFT", 50, -85)
innerFrame.bg = innerFrame:CreateTexture(nil, "BACKGROUND")
innerFrame.bg:SetAllPoints(true)
wbFrame.inner = innerFrame

-- COVER FRAME --
local cover = CreateFrame("Button", "WanderBookCover", UIParent)
cover:SetSize(520, 570)
cover:ClearAllPoints()
cover:SetPoint("CENTER", WanderBookUI, "CENTER")
cover:SetFrameStrata("DIALOG") -- Above main UI
cover:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\cover.tga")
cover:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\cover-h.tga", "ADD")
--[[cover:EnableMouse(true)
cover:SetMovable(true)
cover:RegisterForDrag("LeftButton")
cover:SetScript("OnDragStart", function(self)
    self:StartMoving()
    WanderBookUI:StartMoving()
end)
cover:SetScript("OnDragStop", function(self)
    self:StopMovingOrSizing()

    -- Sync position to WanderBookUI
    local point, relativeTo, relativePoint, xOfs, yOfs = self:GetPoint()
    WanderBookUI:ClearAllPoints()
    WanderBookUI:SetPoint(point, relativeTo, relativePoint, xOfs, yOfs)
end)]]
cover:Hide()
tinsert(UISpecialFrames, "WanderBookCover")

-- Click-to-open behavior
cover:SetScript("OnClick", function()
    cover:Hide()
    WanderBookUI:Show()
    if WanderBookUI.ShowObjectives then
        WanderBookUI:ShowObjectives()
    end
end)


--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- OBJECTIVE SLOTS --

for i = 1, pageSize do
    local slot = CreateFrame("Frame", nil, innerFrame)
    slot:SetSize(410, 64)
    if i == 1 then
        slot:SetPoint("TOPLEFT", 32, -25)
    else
        slot:SetPoint("TOPLEFT", entrySlots[i - 1], "BOTTOMLEFT", 0, -6)
    end

    -- CUSTOM TEXTURED BACKGROUND
    slot.bg = slot:CreateTexture(nil, "BACKGROUND")
    slot.bg:SetPoint("TOPLEFT", 0, -2)
    slot.bg:SetPoint("BOTTOMRIGHT", 0, -2)
    slot.bg:SetTexture(ObjectiveSlotBackground(obj))
    -- Optional crop if needed:
    -- slot.bg:SetTexCoord(0.05, 0.95, 0.1, 0.9)
    
    slot.highlightOverlay = slot:CreateTexture(nil, "OVERLAY")
    slot.highlightOverlay:SetPoint("TOPLEFT", 0, -2)
    slot.highlightOverlay:SetPoint("BOTTOMRIGHT", 0, -2)
    slot.highlightOverlay:SetTexture("Interface\\AddOns\\WanderBook\\Media\\obj-slot-h.tga")
    slot.highlightOverlay:SetBlendMode("ADD")
    slot.highlightOverlay:Hide()

    -- Title
    slot.title = slot:CreateFontString(nil, "OVERLAY", nil)
    slot.title:SetFont("Fonts\\FRIZQT__.TTF", 14, "")
    slot.title:SetPoint("BOTTOM", slot, "CENTER", 0, 8)
    slot.title:SetJustifyH("CENTER")
    slot.title:SetText("")
    slot.title:SetTextColor(1, 1, 1)
    slot.title:SetShadowColor(0, 0, 0, .6)
    slot.title:SetShadowOffset(0, -2)

    -- Description
    slot.desc = slot:CreateFontString(nil, "OVERLAY", nil)
    slot.desc:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
    slot.desc:SetPoint("TOP", slot.title, "BOTTOM", 0, -6)
    slot.desc:SetJustifyH("CENTER")
    slot.desc:SetWidth(210)
    slot.desc:SetWordWrap(true)
    slot.desc:SetNonSpaceWrap(true)
    slot.desc:SetMaxLines(2)
    slot.desc:SetText("")
    slot.desc:SetTextColor(0.35, 0.2, 0.1)
    --slot.desc:SetShadowColor(0, 0, 0, 0)
    
    -- Progress bar background (separate texture for full control)
    slot.progressBarBG = slot:CreateTexture(nil, "BACKGROUND")
    slot.progressBarBG:SetSize(60, 18)
    slot.progressBarBG:SetPoint("CENTER", slot, "RIGHT", -50, 0)
    slot.progressBarBG:SetTexture("Interface\\AddOns\\WanderBook\\Media\\bar-bg.tga")
    slot.progressBarBG:SetDrawLayer("ARTWORK", 0)
    slot.progressBarBG:SetAlpha(0.8)

    -- Progress bar (custom fill)
    slot.progressBar = CreateFrame("StatusBar", nil, slot)
    slot.progressBar:SetSize(60, 18)
    slot.progressBar:SetPoint("CENTER", slot, "RIGHT", -50, 0)
    slot.progressBar:SetStatusBarTexture("Interface\\AddOns\\WanderBook\\Media\\bar-fill.tga")
    slot.progressBar:SetStatusBarColor(1, 1, 1, 1)
    slot.progressBar:SetMinMaxValues(0, 1)

    -- Progress text (after progressBar exists)
    slot.progressText = slot.progressBar:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    slot.progressText:SetPoint("CENTER", slot.progressBar, "CENTER", 0, 0)
    slot.progressText:SetJustifyH("CENTER")
    slot.progressText:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
    slot.progressText:SetTextColor(1, 1, 0, 1)
    --slot.progressText:SetShadowColor(0, 0, 0, 1)
    --slot.progressText:SetShadowOffset(1, -1)
    
    slot.doneIcon = slot:CreateTexture(nil, "ARTWORK")
    slot.doneIcon:SetSize(60, 60)
    slot.doneIcon:SetPoint("CENTER", slot.progressBar, "CENTER", 0, 0)
    slot.doneIcon:Hide()  -- Hidden by default

    -- Icon
    slot.icon = slot:CreateTexture(nil, "ARTWORK")
    slot.icon:SetSize(35, 35)
    slot.icon:SetPoint("LEFT", slot, "LEFT", 13, 0)
    slot.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
    
    -- Shadow version of the icon overlay
    slot.iconOverlayShadow = slot:CreateTexture(nil, "ARTWORK", nil, -1)
    slot.iconOverlayShadow:SetSize(60, 60)
    slot.iconOverlayShadow:SetPoint("CENTER", slot.icon, "CENTER", 0, -4)
    slot.iconOverlayShadow:SetTexture("Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-shadow.tga")
    slot.iconOverlayShadow:SetVertexColor(0, 0, 0, 0.3)
    
    slot.iconOverlay = slot:CreateTexture(nil, "OVERLAY")
    slot.iconOverlay:SetSize(50, 50)
    slot.iconOverlay:SetPoint("CENTER", slot.icon, "CENTER")
    slot.iconOverlay:SetTexture("Interface\\AddOns\\WanderBook\\Media\\slot-icon-border-wood.tga")
    slot.iconOverlay:SetAlpha(1) 

    -- ID Text
    slot.idText = slot:CreateFontString(nil, "OVERLAY")
    slot.idText:SetPoint("RIGHT", slot.icon, "RIGHT", -55, 0)
    slot.idText:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
    slot.idText:SetTextColor(0.35, 0.2, 0.1, 0.5)

    -- SLOT EDIT BUTTON
    slot.editBtn = CreateFrame("Button", nil, slot)
    slot.editBtn:SetSize(20, 20)
    slot.editBtn:SetPoint("TOPRIGHT", slot, "TOPRIGHT", 23, -19)
    slot.editBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-slot-edit.tga")
    slot.editBtn:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-slot-edit.tga")
    slot.editBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-smll-h.tga", "ADD")
    slot.editBtn:GetNormalTexture():SetAlpha(0.5)
    slot.editBtn:GetHighlightTexture():SetAlpha(0.2)

    slot.editBtn:SetScript("OnClick", function()
        if confirmPanel:IsShown() and confirmPanel.currentSlotIndex == i then
            confirmPanel:Hide()
            confirmPanel.currentSlotIndex = nil

            -- Reset all slot backgrounds on close
            for _, s in ipairs(entrySlots) do
                if s.bg then
                    s.bg:SetTexture(ObjectiveSlotBackground(s.currentObjective))
                end
            end
        else
            if WanderBook.NewOathPanel and WanderBook.NewOathPanel:IsShown() then
                WanderBook.NewOathPanel:Hide()
            end
            if WanderBookConfirmDialog and WanderBookConfirmDialog:IsShown() then
                WanderBookConfirmDialog:Hide()
            end

            confirmPanel:Hide()

            -- Reset all slot backgrounds before switching
            for _, s in ipairs(entrySlots) do
                if s.bg then
                    s.bg:SetTexture(ObjectiveSlotBackground(s.currentObjective))
                end
            end

            C_Timer.After(0.02, function()
                confirmPanel.titleBox.text:SetText(slot.title:GetText() or "Objective")
                confirmPanel.currentSlotIndex = i
                confirmPanel:SetAlpha(0)
                confirmPanel:Show()
                UIFrameFadeIn(confirmPanel, 0.1, 0, 1)

                currentEditSlot = slot
                if slot.highlightOverlay then
                    slot.highlightOverlay:Show()
                end
            end)
        end
    end)

    -- HOVER SLOT FUNCTION --
    slot:SetScript("OnEnter", function()

        -- TOOLTIP --
        GameTooltip:SetOwner(slot, "ANCHOR_NONE")
        GameTooltip:SetPoint("LEFT", slot, "RIGHT", 10, 0)

        local title = slot.title:GetText() or "Objective"
        GameTooltip:AddLine(title, 1, 1, 1)

        local prog = slot.rawProgress or 0
        local goal = slot.rawGoal or 1
        local obj = slot.currentObjective or {}
        
        -- Fetch names if not already present
        if obj.spellID and not obj.spellName then
            local info = C_Spell and C_Spell.GetSpellInfo(obj.spellID)
            if type(info) == "table" and info.name then
                obj.spellName = info.name
            elseif type(info) == "string" then
                obj.spellName = info
            end
        end

        if obj.itemID and not obj.itemName then
            local name = C_Item and C_Item.GetItemNameByID and C_Item.GetItemNameByID(obj.itemID)
            if name then obj.itemName = name end
        end

        if obj.npcID and not obj.npcName then
            obj.npcName = "NPC " .. obj.npcID
        end

        -- Helper to format name + ID
        local function formatIDName(id, name, typeLabel)
            if id and name then
                return string.format("%s [%s %d]", name, typeLabel, id)
            elseif id then
                return string.format("[%s %d]", typeLabel, id)
            else
                return name or "?"
            end
        end

        -- Objective type summary
        local summary = ""

        if obj.type == "emote" then
            summary = string.format("Do emote /%s", obj.emote or obj.target or "?")
            if obj.creatureType then
                summary = summary .. string.format(" on %s", obj.creatureType)
            elseif obj.npcID then
                summary = summary .. string.format(" on %s", formatIDName(obj.npcID, obj.npcName, "NPC"))
            end

        elseif obj.type == "use" or obj.type == "usetarget" then
            if obj.spellID then
                summary = string.format("Use %s", formatIDName(obj.spellID, obj.spellName, "Spell"))
            elseif obj.itemID then
                summary = string.format("Use %s", formatIDName(obj.itemID, obj.itemName, "Item"))
            else
                summary = "Use"
            end
            if obj.creatureType then
                summary = summary .. string.format(" on %s", obj.creatureType)
            elseif obj.npcID then
                summary = summary .. string.format(" on %s", formatIDName(obj.npcID, obj.npcName, "NPC"))
            end

        elseif obj.type == "loot" then
            if obj.itemID then
                summary = string.format("Loot %s", formatIDName(obj.itemID, obj.itemName, "Item"))
            else
                summary = "Loot"
            end

        elseif obj.type == "kill" or obj.type == "killwith" then
            summary = "Kill"
            if obj.creatureType then
                summary = summary .. string.format(" %s", obj.creatureType)
            elseif obj.npcID then
                summary = summary .. string.format(" %s", formatIDName(obj.npcID, obj.npcName, "NPC"))
            end
            if obj.spellID then
                summary = summary .. string.format(" w/ %s", formatIDName(obj.spellID, obj.spellName, "Spell"))
            elseif obj.itemID then
                summary = summary .. string.format(" w/ %s", formatIDName(obj.itemID, obj.itemName, "Item"))
            end
                
        elseif obj.type == "zone" then
            summary = string.format("Go to Zone %s", obj.zoneName or "?")
            if obj.subzoneName then
                summary = summary .. string.format(", %s", obj.subzoneName)
            end

        elseif obj.type == "crit" or obj.type == "crittarget" then
            summary = "Crit"
            if obj.creatureType then
                summary = summary .. string.format(" on %s", obj.creatureType)
            elseif obj.npcID then
                summary = summary .. string.format(" on %s", formatIDName(obj.npcID, obj.npcName, "NPC"))
            end
            if obj.spellID then
                summary = summary .. string.format(" w/ %s", formatIDName(obj.spellID, obj.spellName, "Spell"))
            elseif obj.itemID then
                summary = summary .. string.format(" w/ %s", formatIDName(obj.itemID, obj.itemName, "Item"))
            end

        elseif obj.type == "regen" then
            if obj.spellID then
                summary = string.format("Regenerate w/ %s", formatIDName(obj.spellID, obj.spellName, "Spell"))
            elseif obj.itemID then
                summary = string.format("Regenerate w/ %s", formatIDName(obj.itemID, obj.itemName, "Item"))
            else
                summary = "Regenerate"
            end

        elseif obj.type == "damage" then
            if obj.spellID then
                summary = string.format("Damage w/ %s", formatIDName(obj.spellID, obj.spellName, "Spell"))
            elseif obj.itemID then
                summary = string.format("Damage w/ %s", formatIDName(obj.itemID, obj.itemName, "Item"))
            else
                summary = "Damage"
            end
        end

        if obj.zoneName and obj.type ~= "zone" then
            summary = summary .. string.format(" in %s", obj.zoneName)
        end
        if obj.counter then
            summary = summary .. string.format(" [%d]", obj.counter)
        end

        if summary ~= "" then
            local wrapLimit = 35
            local current = ""
            for word in summary:gmatch("%S+") do
                if #current + #word + 1 > wrapLimit then
                    GameTooltip:AddLine(current, 0.7, 0.8, 1)
                    current = word
                else
                    if current ~= "" then
                        current = current .. " " .. word
                    else
                        current = word
                    end
                end
            end
            if current ~= "" then
                GameTooltip:AddLine(current, 0.7, 0.8, 1)
            end
        end

        -- Progress
        if obj.isCounter then
            GameTooltip:AddLine(string.format("Count: %s", ShortenGoalNumber(prog)), 0.9, 0.9, 0.9)
        else
            GameTooltip:AddLine(string.format("Progress: %s / %s", ShortenGoalNumber(prog), ShortenGoalNumber(goal)), 0.9, 0.9, 0.9)
        end

        -- Completion
        if obj.completed and obj.completedBy and obj.completedOn then
            GameTooltip:AddLine("Completed: " .. obj.completedBy, 0.6, 1, 0.6)
            GameTooltip:AddLine("" .. obj.completedOn, 0.6, 0.9, 0.6)
        end

        GameTooltip:Show()
        if slot.highlightOverlay then
            slot.highlightOverlay:Show()
        end
    end)

    slot:SetScript("OnLeave", function()
        GameTooltip:Hide()
        if slot.highlightOverlay then
            if currentEditSlot ~= slot and slot.highlightOverlay then
                slot.highlightOverlay:Hide()
            end
        end
    end)

    entrySlots[i] = slot
end

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- SCALE BUTTONS

local scales = {1.0, 1.2, 1.4}
local currentScaleIndex = WanderBook_Settings and WanderBook_Settings.scaleIndex or 1
local function ApplyScale(index)
    currentScaleIndex = index
    if WanderBook_Settings then
        WanderBook_Settings.scaleIndex = index
    end
    local scale = scales[index]

    WanderBookUI:SetScale(scale)

    if WanderBook.NewOathPanel then
        WanderBook.NewOathPanel:SetScale(scale)
    end

    if WanderBook.IconSelector then
        WanderBook.IconSelector:SetScale(scale)
    end
end

-- SCALE DOWN BUTTON
local scaleDownBtn = CreateFrame("Button", nil, wbFrame)
scaleDownBtn:SetSize(16, 16)
scaleDownBtn:SetPoint("TOPLEFT", wbFrame, "TOPLEFT", 50, -42)
scaleDownBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-less.tga")
scaleDownBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-smll-h.tga", "ADD")
scaleDownBtn:GetHighlightTexture():SetAlpha(0.2)
scaleDownBtn:SetScript("OnClick", function()
    if currentScaleIndex > 1 then
        ApplyScale(currentScaleIndex - 1)
    end
end)

-- SCALE UP BUTTON
local scaleUpBtn = CreateFrame("Button", nil, wbFrame)
scaleUpBtn:SetSize(16, 16)
scaleUpBtn:SetPoint("LEFT", scaleDownBtn, "RIGHT", 6, 0)
scaleUpBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-more.tga")
scaleUpBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-smll-h.tga", "ADD")
scaleUpBtn:GetHighlightTexture():SetAlpha(0.2)
scaleUpBtn:SetScript("OnClick", function()
    if currentScaleIndex < #scales then
        ApplyScale(currentScaleIndex + 1)
    end
end)

-- SORTING BUTTONS--

-- A-Z Button (Primary Anchor)
local sortAZ = CreateFrame("Button", nil, innerFrame)
sortAZ:SetSize(20, 20)
sortAZ:SetPoint("BOTTOM", innerFrame, "BOTTOM", -180, 14)
sortAZ:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-sort-az.tga")
sortAZ:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-sort-az.tga")
sortAZ:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-smll-h.tga", "ADD")
sortAZ:GetNormalTexture():SetAlpha(0.6)
sortAZ:GetHighlightTexture():SetAlpha(0.2)
sortAZ:SetScript("OnClick", function()
    PlaySound(SOUNDKIT.IG_MINIMAP_OPEN)
    WanderBook.SortObjectivesBy("alpha")
end)

-- Progress (%) Button
local sortProgress = CreateFrame("Button", nil, sortAZ)
sortProgress:SetSize(20, 20)
sortProgress:SetPoint("LEFT", sortAZ, "RIGHT", 10, 0)
sortProgress:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-sort-pr.tga")
sortProgress:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-sort-pr.tga")
sortProgress:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-smll-h.tga", "ADD")
sortProgress:GetNormalTexture():SetAlpha(0.6)
sortProgress:GetHighlightTexture():SetAlpha(0.2)
sortProgress:SetScript("OnClick", function()
    PlaySound(SOUNDKIT.IG_MINIMAP_OPEN)
    WanderBook.SortObjectivesBy("progress")
end)

-- Type Button
local sortType = CreateFrame("Button", nil, sortAZ)
sortType:SetSize(20, 20)
sortType:SetPoint("LEFT", sortProgress, "RIGHT", 10, 0)
sortType:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-sort-ty.tga")
sortType:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-sort-ty.tga")
sortType:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-smll-h.tga", "ADD")
sortType:GetNormalTexture():SetAlpha(0.6)
sortType:GetHighlightTexture():SetAlpha(0.2)
sortType:SetScript("OnClick", function()
    PlaySound(SOUNDKIT.IG_MINIMAP_OPEN)
    WanderBook.SortObjectivesBy("type")
end)

-- PAGE LABEL & BUTTONS --

local prevBtn = CreateFrame("Button", nil, innerFrame)
prevBtn:SetSize(20, 20)
prevBtn:SetPoint("BOTTOM", 162, 14)
prevBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-page.tga")
prevBtn:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-page.tga")
prevBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-smll-h.tga", "ADD")
prevBtn:GetNormalTexture():SetAlpha(0.6)
prevBtn:GetHighlightTexture():SetAlpha(0.2)

local n = prevBtn:GetNormalTexture()
if n then
    n:SetTexCoord(1, 0, 0, 1)
end

local p = prevBtn:GetPushedTexture()
if p then
    p:SetTexCoord(1, 0, 0, 1)
end

local nextBtn = CreateFrame("Button", nil, innerFrame)
nextBtn:SetSize(20, 20)
nextBtn:SetPoint("BOTTOM", 185, 14)
nextBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-page.tga")
nextBtn:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-page.tga")
nextBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-smll-h.tga", "ADD")
nextBtn:GetNormalTexture():SetAlpha(0.6)
nextBtn:GetHighlightTexture():SetAlpha(0.2)

local pageLabel = innerFrame:CreateFontString(nil, "OVERLAY")
pageLabel:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
pageLabel:SetTextColor(0.35, 0.2, 0.1, 0.8)
pageLabel:SetPoint("BOTTOM", 115, 17)
pageLabel:SetText("Page 1")

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

-- EDIT CONFIRMATION DIALOG --
local function ShowConfirmationDialog(message, onConfirm)
    if not WanderBookConfirmDialog then
        WanderBookConfirmDialog = CreateFrame("Frame", "WanderBookConfirmDialog", confirmPanel, "BasicFrameTemplateWithInset")
        WanderBookConfirmDialog:SetSize(200, 80)
        WanderBookConfirmDialog:SetPoint("TOP", confirmPanel, "BOTTOM", 0, 5)
        WanderBookConfirmDialog:SetFrameStrata("DIALOG")
        WanderBookConfirmDialog:EnableMouse(true)

        local text = WanderBookConfirmDialog:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
        text:SetPoint("TOP", -5, -5)
        text:SetWidth(200)
        text:SetJustifyH("CENTER")
        WanderBookConfirmDialog.text = text

        local yesBtn = CreateFrame("Button", nil, WanderBookConfirmDialog, "UIPanelButtonTemplate")
        yesBtn:SetSize(80, 24)
        yesBtn:SetPoint("BOTTOMLEFT", 20, 16)
        yesBtn:SetText("Yes")
        WanderBookConfirmDialog.yesBtn = yesBtn

        local cancelBtn = CreateFrame("Button", nil, WanderBookConfirmDialog, "UIPanelButtonTemplate")
        cancelBtn:SetSize(80, 24)
        cancelBtn:SetPoint("BOTTOMRIGHT", -20, 16)
        cancelBtn:SetText("Cancel")
        WanderBookConfirmDialog.cancelBtn = cancelBtn

        WanderBookConfirmDialog.cancelBtn:SetScript("OnClick", function()
            WanderBookConfirmDialog:Hide()
        end)
    end

    WanderBookConfirmDialog.text:SetText(message)
    WanderBookConfirmDialog:Show()

    WanderBookConfirmDialog.yesBtn:SetScript("OnClick", function()
        WanderBookConfirmDialog:Hide()
        if onConfirm then onConfirm() end
    end)
end

-- OBJECTIVE EDIT PANEL --
confirmPanel = CreateFrame("Frame", "WBConfirmPanel", UIParent, "BasicFrameTemplateWithInset")
confirmPanel:SetFrameStrata(WanderBookUI:GetFrameStrata())
confirmPanel:SetFrameLevel(WanderBookUI:GetFrameLevel() + 10)

-- Strip Blizzard default textures
local regions = { confirmPanel:GetRegions() }
for _, region in ipairs(regions) do
    if region:GetObjectType() == "Texture" then
        region:SetTexture(nil)
    end
end

-- Apply custom background texture
local bgTex = confirmPanel:CreateTexture(nil, "BACKGROUND")
bgTex:SetAllPoints(true)
bgTex:SetTexture("Interface\\AddOns\\WanderBook\\Media\\slot-edit-panel.tga")
confirmPanel:SetSize(210, 300)
confirmPanel:SetPoint("LEFT", WanderBookUI, "RIGHT", -25, 80)
confirmPanel:Hide()

-- Sound and close behavior
confirmPanel:HookScript("OnShow", function()
    PlaySound(SOUNDKIT.IG_MINIMAP_OPEN)
end)
confirmPanel:HookScript("OnHide", function()
    PlaySound(SOUNDKIT.IG_MINIMAP_CLOSE)

    currentEditSlot = nil
    for _, slot in ipairs(entrySlots) do
        if slot.highlightOverlay then
            slot.highlightOverlay:Hide()
        end
    end
end)

-- Create the title box container
confirmPanel.titleBox = CreateFrame("Frame", nil, confirmPanel)
confirmPanel.titleBox:SetSize(160, 58)  -- Adjust size as needed
confirmPanel.titleBox:SetPoint("TOP", 0, -30)

confirmPanel.titleBox.headerText = confirmPanel.titleBox:CreateFontString(nil, "OVERLAY")
confirmPanel.titleBox.headerText:SetPoint("TOP", confirmPanel.titleBox, "TOP", 0, -6)
confirmPanel.titleBox.headerText:SetFont("Fonts\\FRIZQT__.TTF", 10)
confirmPanel.titleBox.headerText:SetTextColor(0.35, 0.2, 0.1)
confirmPanel.titleBox.headerText:SetJustifyH("CENTER")
confirmPanel.titleBox.headerText:SetText("Edit Oath")

-- Create the label inside the title box
confirmPanel.titleBox.text = confirmPanel.titleBox:CreateFontString(nil, "OVERLAY")
confirmPanel.titleBox.text:SetPoint("TOP", confirmPanel.titleBox, "TOP", 0, -22)
confirmPanel.titleBox.text:SetFont("Fonts\\FRIZQT__.TTF", 14)
confirmPanel.titleBox.text:SetWidth(140)  -- Match the box width to enable wrapping
confirmPanel.titleBox.text:SetJustifyH("CENTER")
confirmPanel.titleBox.text:SetJustifyV("BOTTOM")
confirmPanel.titleBox.text:SetText("")
confirmPanel.titleBox.text:SetTextColor(1, 1, 1)
confirmPanel.titleBox.text:SetShadowOffset(0, -2)
confirmPanel.titleBox.text:SetShadowColor(0, 0, 0, .6)

-- Export Button
confirmPanel.exportBtn = CreateFrame("Button", nil, confirmPanel)
confirmPanel.exportBtn:SetSize(42, 40)
confirmPanel.exportBtn:SetPoint("TOPLEFT", confirmPanel, "TOPLEFT", 36, -112)
confirmPanel.exportBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-blue-export-n.tga")
confirmPanel.exportBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-blue-export-h.tga")
confirmPanel.exportBtn:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-blue-export-d.tga")
local exportText = confirmPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
exportText:SetPoint("LEFT", confirmPanel.exportBtn, "RIGHT", 12, 0)
exportText:SetText("Export Oath")

confirmPanel.exportBtn:SetScript("OnClick", function()
    local slot = entrySlots[confirmPanel.currentSlotIndex]
    if slot and slot.currentObjective and slot.currentObjective.id then
        SlashCmdList["EXPORTOATH"](tostring(slot.currentObjective.id))
    end
    confirmPanel:Hide()
end)

-- Renew Button
confirmPanel.renewBtn = CreateFrame("Button", nil, confirmPanel)
confirmPanel.renewBtn:SetSize(42, 40)
confirmPanel.renewBtn:SetPoint("TOPLEFT", confirmPanel.exportBtn, "BOTTOMLEFT", 0, -14)
confirmPanel.renewBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-yellow-reset-n.tga")
confirmPanel.renewBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-yellow-reset-h.tga")
confirmPanel.renewBtn:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-yellow-reset-d.tga")
local renewText = confirmPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
renewText:SetPoint("LEFT", confirmPanel.renewBtn, "RIGHT", 12, 0)
renewText:SetText("Renew Oath")

confirmPanel.renewBtn:SetScript("OnClick", function()
    ShowConfirmationDialog("Reset this Oath?", function()
        local slot = entrySlots[confirmPanel.currentSlotIndex]
        if slot and slot.currentObjective and slot.currentObjective.id then
            SlashCmdList["RENEWOATH"]("id=" .. tostring(slot.currentObjective.id))
            confirmPanel:Hide()
            C_Timer.After(0.1, function()
                WanderBookUI:ShowObjectives()
            end)
        end
    end)
end)

-- Dismiss Button
confirmPanel.dismissBtn = CreateFrame("Button", nil, confirmPanel)
confirmPanel.dismissBtn:SetSize(42, 40)
confirmPanel.dismissBtn:SetPoint("TOPLEFT", confirmPanel.renewBtn, "BOTTOMLEFT", 0, -15)
confirmPanel.dismissBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-red-delete-n.tga")
confirmPanel.dismissBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-red-delete-h.tga")
confirmPanel.dismissBtn:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-red-delete-d.tga")
local dismissText = confirmPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
dismissText:SetPoint("LEFT", confirmPanel.dismissBtn, "RIGHT", 12, 0)
dismissText:SetText("Dismiss Oath")

confirmPanel.dismissBtn:SetScript("OnClick", function()
    ShowConfirmationDialog("Dismiss this Oath?", function()
        local source = filteredObjectives or WanderBook.objectives
        local objIndex = (page - 1) * pageSize + confirmPanel.currentSlotIndex
        local obj = source[objIndex]
        if obj and obj.id then
            SlashCmdList["DISMISSOATH"]("id=" .. tostring(obj.id))
            confirmPanel:Hide()
            C_Timer.After(0.1, function()
                WanderBookUI:ShowObjectives()
            end)
        end
    end)
end)

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- SEARCH BOX

local searchBox = CreateFrame("EditBox", nil, wbFrame, "InputBoxTemplate")
for i = 1, select("#", searchBox:GetRegions()) do
    local region = select(i, searchBox:GetRegions())
    if region and region:GetObjectType() == "Texture" then
        region:SetTexture(nil)
    end
end
searchBox:SetSize(175, 28)
searchBox:SetPoint("TOPLEFT", innerFrame, "BOTTOMLEFT", 76, -46)
searchBox:SetAutoFocus(false)
searchBox:SetText("Search Oaths")
searchBox:SetFont("Fonts\\FRIZQT__.TTF", 14, "")
searchBox:SetTextColor(0.5, 0.5, 0.5)
searchBox:SetTextInsets(5, 0, 0, 0)
searchBox:SetScript("OnEditFocusGained", function(self)
    if self:GetText() == "Search Oaths" then
        self:SetText("")
        self:SetTextColor(1, 1, 1)
    end
end)
searchBox:SetScript("OnEditFocusLost", function(self)
    if self:GetText() == "" then
        self:SetText("Search Oaths")
        self:SetTextColor(0.5, 0.5, 0.5)
        filteredObjectives = nil
        page = 1
        WanderBookUI:ShowObjectives()
    end
end)
searchBox:SetScript("OnTextChanged", function(self, isUserInput)
    if not isUserInput then return end

    local text = self:GetText():lower()
    if text == "" or text == "search oaths" then
        filteredObjectives = nil
    else
        filteredObjectives = {}
        for _, obj in ipairs(WanderBook.objectives) do
            if obj.name and obj.name:lower():find(text) then
                table.insert(filteredObjectives, obj)
            end
        end
    end
    page = 1
    WanderBookUI:ShowObjectives()
end)
searchBox:SetScript("OnKeyDown", function(self, key)
    if key == "ESCAPE" then
        self:SetText("Search Oaths")
        self:SetTextColor(0.5, 0.5, 0.5)
        filteredObjectives = nil
        page = 1
        WanderBookUI:ShowObjectives()
        self:ClearFocus()
    end
end)

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- NEW OATH BUTTON --

local newOathBtn = CreateFrame("Button", nil, wbFrame)
--newOathBtn:SetFrameStrata("HIGH")
newOathBtn:SetSize(123, 39)
newOathBtn:SetPoint("TOPRIGHT", innerFrame, "BOTTOMRIGHT", -68, -39)
newOathBtn:SetNormalTexture("Interface\\AddOns\\WanderBook\\Media\\btn-green-big-n.tga")
newOathBtn:SetHighlightTexture("Interface\\AddOns\\WanderBook\\Media\\btn-green-big-h.tga")
newOathBtn:SetPushedTexture("Interface\\AddOns\\WanderBook\\Media\\btn-green-big-d.tga")
local btnLabel = newOathBtn:CreateFontString(nil, "OVERLAY", nil)
btnLabel:SetFont("Fonts\\FRIZQT__.TTF", 14, "")
btnLabel:SetTextColor(0.5, 1, 0.5)
btnLabel:SetPoint("CENTER")
btnLabel:SetText("New Oath")

newOathBtn:SetScript("OnClick", function()
    if WBConfirmPanel and WBConfirmPanel:IsShown() then
        WBConfirmPanel:Hide()
    end
    WanderBook.ToggleNewOathPanel()
end)

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- OBJECTIVES DISPLAY LOGIC --

local function UpdatePageLabel()
    local total = math.ceil(#WanderBook.objectives / pageSize)
    pageLabel:SetText("Page " .. page .. " / " .. total)
end

function WanderBookUI:ShowObjectives()
    if not WanderBookUI:IsShown() then return end
    
    if confirmPanel and confirmPanel:IsShown() then
        confirmPanel:Hide()
    end
    if WanderBookConfirmDialog and WanderBookConfirmDialog:IsShown() then
        WanderBookConfirmDialog:Hide()
    end
    
    for _, slot in ipairs(entrySlots) do
        if slot.bg then
            slot.bg:SetTexture(ObjectiveSlotBackground(obj))
        end
    end

    local source = filteredObjectives or WanderBook.objectives
    
    local total = #source
    local totalPages = math.max(1, math.ceil(total / pageSize))
    page = math.min(page, totalPages)
    
    local start = (page - 1) * pageSize + 1

    for i = 1, pageSize do
        local obj = source[start + i - 1]
        local slot = entrySlots[i]

        if obj then
            if slot.title then
                slot.title:SetText(obj.name or "")
            end
            if slot.desc then
                slot.desc:SetText(obj.desc or "")
            end
            if slot.progressText then
                -- Always show just the progress number in the bar text
                slot.progressText:SetText(string.format("%s", ShortenGoalNumber(obj.progress)))

                if obj.isCounter then
                    local maxVal = obj.progress > 0 and obj.progress or 1
                    slot.progressBar:SetMinMaxValues(0, maxVal)
                    slot.progressBar:SetValue(obj.progress)
                    slot.rawGoal = maxVal
                    slot.progressBar:SetStatusBarTexture("Interface\\AddOns\\WanderBook\\Media\\bar-fill-2.tga")
                else
                    local goal = obj.goal or 1
                    slot.progressBar:SetMinMaxValues(0, goal)
                    slot.progressBar:SetValue(obj.progress)
                    slot.rawGoal = goal
                    slot.progressBar:SetStatusBarTexture("Interface\\AddOns\\WanderBook\\Media\\bar-fill.tga")
                end

                slot.rawProgress = obj.progress
                slot.currentObjective = obj
                
                if slot.bg then
                    slot.bg:SetTexture(ObjectiveSlotBackground(obj))
                    slot.bg:SetBlendMode("BLEND")
                end
            end

            if slot.icon then
                slot.icon:SetTexture(obj.icon or 134400)
            end
            
            if slot.iconOverlay then
                local tex = IconBorder(obj)
                slot.iconOverlay:SetTexture(tex)
            end
            
            if obj.completed and not obj.isCounter then
                -- Hide progress bar + text
                slot.progressBar:Hide()
                slot.progressBarBG:Hide()
                slot.progressText:Hide()

                -- Set and show done icon
                local tex = CompletedIcon(obj)
                slot.doneIcon:SetTexture(tex)
                slot.doneIcon:Show()
            else
                -- Show progress bar + text
                slot.progressBar:Show()
                slot.progressBarBG:Show()
                slot.progressText:Show()
                slot.doneIcon:Hide()
            end

            if slot.idText then
                slot.idText:SetText(string.format("%d", obj.id))
            end
            slot:Show()

            -- SLOT EDIT BUTTON
            slot.editBtn:Show()
        else
            slot:Hide()
            if slot.buttons then
                for _, b in ipairs(slot.buttons) do
                    b:Hide()
                    b:SetScript("OnClick", nil)
                end
            end
        end
    end

    pageLabel:SetText(string.format("Page %d/%d", page, totalPages))
    -- Always clear any leftover tooltip first
    GameTooltip:Hide()

    -- Now refresh tooltip for slot under mouse (if any)
    for _, slot in ipairs(entrySlots) do
        if slot:IsShown() and slot:IsMouseOver() then
            if slot:GetScript("OnEnter") then
                slot:GetScript("OnEnter")(slot)
            end
            break
        end
    end
end

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- PAGE BUTTON LOGIC --

function WanderBookUI:NextPage()
    local totalPages = math.ceil(#WanderBook.objectives / pageSize)
    if page < totalPages then
        page = page + 1
        self:ShowObjectives()
        PlaySound(SOUNDKIT.IG_ABILITY_PAGE_TURN)
    end
end
function WanderBookUI:PrevPage()
    if page > 1 then
        page = page - 1
        self:ShowObjectives()
        PlaySound(SOUNDKIT.IG_ABILITY_PAGE_TURN)
    end
end

prevBtn:SetScript("OnClick", function() WanderBookUI:PrevPage() end)
nextBtn:SetScript("OnClick", function() WanderBookUI:NextPage() end)

wbFrame:EnableMouseWheel(true)
wbFrame:SetScript("OnMouseWheel", function(_, delta)
    if delta > 0 then WanderBookUI:PrevPage()
    else WanderBookUI:NextPage() end
end)

wbFrame:HookScript("OnHide", function()
    if WanderBook.NewOathPanel and WanderBook.NewOathPanel:IsShown() then
        WanderBook.NewOathPanel:Hide()
    end
    if WanderBook_IconSelector and WanderBook_IconSelector:IsShown() then
        WanderBook_IconSelector:Hide()
    end
    if WanderBook.SealFrame and WanderBook.SealFrame:IsShown() then
        WanderBook.SealFrame:Hide()
    end
    if WBConfirmPanel and WBConfirmPanel:IsShown() then
        WBConfirmPanel:Hide()
    end
end)

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-- SLASH COMMAND --

SLASH_WANDERBOOK1 = "/wb"
SlashCmdList["WANDERBOOK"] = function()
    if not WanderBook_UnlockState or not WanderBook_UnlockState.unlocked then
        ShowWanderBookUnlockQuest()
        print("|cff3399ffYou feel something calling you from cold lands...|r")
        return
    end
    
PlaySound(SOUNDKIT.IG_SPELLBOOK_CLOSE)
    
    if WanderBookCover:IsShown() then
        WanderBookCover:Hide()
    elseif WanderBookUI:IsShown() then
        WanderBookUI:Hide()
    else
        WanderBookCover:Show()
    end
end

SLASH_WANDERBOOKNEW1 = "/wbn"
SlashCmdList["WANDERBOOKNEW"] = function()
    if not WanderBook_UnlockState or not WanderBook_UnlockState.unlocked then
        ShowWanderBookUnlockQuest()
        print("|cff3399ffYou feel something calling you from cold lands...|r")
        return
    end

    if not WanderBookUI:IsShown() then
        WanderBookUI:Show()
    end

    if WanderBook.NewOathPanel and not WanderBook.NewOathPanel:IsShown() then
        WanderBook.NewOathPanel:Show()
    end

    C_Timer.After(0, function()
        if WanderBookUI.ShowObjectives then
            WanderBookUI:ShowObjectives()
        end
    end)
end

SLASH_WANDERBOOKOPEN1 = "/wbo"
SlashCmdList["WANDERBOOKOPEN"] = function()
    if not WanderBook_UnlockState or not WanderBook_UnlockState.unlocked then
        ShowWanderBookUnlockQuest()
        print("|cff3399ffYou feel something calling you from cold lands...|r")
        return
    end

    WanderBookCover:Hide()
    WanderBookUI:Show()

    C_Timer.After(0, function()
        if WanderBookUI.ShowObjectives then
            WanderBookUI:ShowObjectives()
        end
    end)
end


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