-- TOG Tools
-- Author: Pimptasty
-- A collection of lightweight utility tools for WoW Classic and Retail.

local addonName, addon = ...

-- ---------------------------------------------------------------------------
-- Addon global
-- ---------------------------------------------------------------------------
TOGTools       = TOGTools or {}
TOGTools.addon = addon
addon.name     = addonName

addon.BrandColor = "ffFF8000"   -- Orange, consistent with the TOG suite

local _GetAddOnMetadata = (C_AddOns and C_AddOns.GetAddOnMetadata) or GetAddOnMetadata
addon.Version = _GetAddOnMetadata(addonName, "Version") or "dev"

-- ---------------------------------------------------------------------------
-- Version flags
-- ---------------------------------------------------------------------------
local gv = {}
addon.gameVersion = gv
do
    local tocVersion = select(4, GetBuildInfo())
    gv.isVanilla = tocVersion < 20000
    gv.isTBC     = tocVersion >= 20000 and tocVersion < 30000
    gv.isWrath   = tocVersion >= 30000 and tocVersion < 40000
    gv.isCata    = tocVersion >= 40000 and tocVersion < 50000
    gv.isMists   = tocVersion >= 50000 and tocVersion < 60000
    gv.isClassic = gv.isVanilla or gv.isTBC or gv.isWrath or gv.isCata or gv.isMists
    gv.isRetail  = not gv.isClassic
    -- Retail 12.0+ uses the EventRegistry path for chat hooks.
    gv.isRetail120Plus = tocVersion >= 120000
end

-- ---------------------------------------------------------------------------
-- AceAddon
-- ---------------------------------------------------------------------------
local Ace = LibStub("AceAddon-3.0"):NewAddon(
    addonName,
    "AceConsole-3.0",
    "AceEvent-3.0",
    "AceHook-3.0"
)
addon.lib = Ace

-- ---------------------------------------------------------------------------
-- AceDB schema
-- ---------------------------------------------------------------------------
local DB_DEFAULTS = {
    char = {
        minimapPos  = 220,
        minimapHide = false,
        frames = {
            mainWindow = { width = 640, height = 460 },
        },
        -- NamePrefix module settings (per-character — different alts may
        -- use different names/personas)
        namePrefix = {
            enabled  = true,
            nickname = "",
            format   = "(%s): ",   -- %s is replaced with nickname
            guild    = true,
            officer  = true,
            party    = false,
            raid     = false,
            instance = false,
            channel  = "",         -- custom channel name filter (empty = off)
            skipExclamation  = true,
            hideIfCharName   = false,
        },
    },
}

-- ---------------------------------------------------------------------------
-- Module registry — tabs are built from this table, sorted alphabetically
-- by their `label` field.  Each module file registers itself here.
-- ---------------------------------------------------------------------------
addon.modules = {}

-- ---------------------------------------------------------------------------
-- OnInitialize / OnEnable
-- ---------------------------------------------------------------------------
function Ace:OnInitialize()
    addon.DB = LibStub("AceDB-3.0"):New("TOGTools_DB", DB_DEFAULTS, true)

    -- Expose Version so VersionCheck-1.0 reads it from the host object directly.
    Ace.Version = addon.Version

    -- Register with VersionCheck-1.0 on Classic; the library is not yet
    -- available on Retail so we guard with the version flag.
    if not gv.isRetail then
        local VC = LibStub("VersionCheck-1.0", true)
        if VC then
            VC:Enable(Ace)
        end
    end

end

function Ace:OnEnable()
    -- Enable all registered modules
    for _, mod in pairs(addon.modules) do
        if mod.OnEnable then mod:OnEnable() end
    end
end
