local ADDON_NAME, ns = ...

local Addon = LibStub("AceAddon-3.0"):NewAddon(
    "RatedTrackerFieldManual", "AceEvent-3.0", "AceConsole-3.0", "AceTimer-3.0"
)
ns.Addon = Addon

ns.GREEN = "|cFF74D06C"
ns.GOLD = "|cFFFFD100"
ns.RESET = "|r"

function ns:Print(msg)
    if DEFAULT_CHAT_FRAME then
        DEFAULT_CHAT_FRAME:AddMessage(ns.GREEN .. "RatedTracker Field Manual" .. ns.RESET .. ": " .. tostring(msg))
    end
end

function ns:Debug(msg)
    if ns.db and ns.db.profile.debugMode then
        ns:Print("[debug] " .. tostring(msg))
    end
end

function Addon:OnInitialize()
    self.db = LibStub("AceDB-3.0"):New("RatedTrackerFieldManualDB", ns.DefaultDB, true)
    ns.db = self.db

    self:RegisterChatCommand("rtfm", "SlashCommand")
    self:RegisterChatCommand("rtmanual", "SlashCommand")
end

function Addon:OnEnable()
    self:RegisterEvent("PLAYER_ENTERING_WORLD")
end

function Addon:PLAYER_ENTERING_WORLD()
    if not ns.db.profile.autoOpenInBlitz then return end
    if not ns.MapView then return end
    local slug = ns.MapView.SlugForCurrentInstance and ns.MapView:SlugForCurrentInstance()
    if slug then
        ns:OpenToGuide(slug)
    end
end

function Addon:SlashCommand(input)
    input = (input or ""):lower():gsub("^%s+", ""):gsub("%s+$", "")
    if input == "" or input == "toggle" then
        ns:Toggle()
    elseif input == "options" or input == "config" then
        ns:OpenOptions()
    elseif input == "debug" then
        ns.db.profile.debugMode = not ns.db.profile.debugMode
        ns:Print("debug = " .. tostring(ns.db.profile.debugMode))
    elseif input == "intro" then
        if ns.db and ns.db.global then ns.db.global.introShown = false end
        ns:OpenAdventureGuide()
        if ns.AdventureGuide and ns.AdventureGuide.MaybeShowIntro then
            ns.AdventureGuide:MaybeShowIntro()
        end
        ns:Print("First-time callout reset and shown.")
    else
        ns:Print("/rtfm [toggle | options | intro | debug]")
    end
end

function ns:Toggle()
    if ns.MainFrame then ns.MainFrame:Toggle() end
end

function ns:Show()
    if ns.MainFrame then ns.MainFrame:Show() end
end

function ns:OpenAdventureGuide()
    if not EncounterJournal then
        if C_AddOns and C_AddOns.LoadAddOn then
            C_AddOns.LoadAddOn("Blizzard_EncounterJournal")
        elseif LoadAddOn then
            LoadAddOn("Blizzard_EncounterJournal")
        end
    end
    if not ns.AdventureGuide then return end
    ns.AdventureGuide:Setup()
    if not EncounterJournal then
        ns:Print("The Adventure Guide is unavailable right now.")
        return
    end
    if not EncounterJournal:IsShown() then
        ShowUIPanel(EncounterJournal)
    end
    ns.AdventureGuide:Activate()
end

function ns:OpenToGuide(slug)
    ns:OpenAdventureGuide()
    if ns.AdventureGuide and not ns.AdventureGuide:IsActive() then return end
    if ns.MainFrame then ns.MainFrame:SelectSection("blitz") end
    if ns.GuideView then ns.GuideView:OpenGuide(slug) end
end

function ns:OpenOptions()
    local AceConfigDialog = LibStub("AceConfigDialog-3.0", true)
    if not AceConfigDialog then
        ns:Print("AceConfigDialog not loaded")
        return
    end
    local DEFAULT_W, DEFAULT_H = 620, 460
    if AceConfigDialog.SetDefaultSize then
        AceConfigDialog:SetDefaultSize("RatedTrackerFieldManual", DEFAULT_W, DEFAULT_H)
    end
    if AceConfigDialog.OpenFrames and AceConfigDialog.OpenFrames["RatedTrackerFieldManual"] then
        AceConfigDialog:Close("RatedTrackerFieldManual")
    end
    AceConfigDialog:Open("RatedTrackerFieldManual")
end
