local GC = select(2, ...)

local AceGUI = LibStub("AceGUI-3.0")

-- Global loot manager object (used by LootManager.lua)
LOOTMANAGER = {}
LOOTMANAGER["ZoneLoc"] = ""
LOOTMANAGER["Player"] = ""
LOOTMANAGER["LootSession"] = ""
LOOTMANAGER["LootedItems"] = ""

--[[------------------------------------------------------------------------
    AceAddon-3.0: OnInitialize
--------------------------------------------------------------------------]]
function GC:OnInitialize()
    GC.Init.InitDB()

    -- Override the default AcePrint
    function GC:Print(...)
        local msg = ""
        for i = 1, select("#", ...) do
            msg = msg .. tostring(select(i, ...)) .. (i < select("#", ...) and " " or "")
        end
        DEFAULT_CHAT_FRAME:AddMessage("|cffffd100[GallywixCoins]|r " .. msg)
    end

    GC.Debug.Log("GC:OnInitialize()")

    GC:SetSinkStorage(GC.db.profile.notification.sink)

    -- Set up minimap icon and data broker
    GC.MinimapIcon.SetupMinimapIcon()
    GC.MinimapIcon.SetupAddonCompartment()
    GC.MinimapIcon.SetupDataBroker()
end

--[[------------------------------------------------------------------------
    AceAddon-3.0: OnEnable
--------------------------------------------------------------------------]]
function GC:OnEnable()
    GC.UI.SetupToastPositioning()
    GC.PriceSources.Prepare()

    -- Register chat commands
    GC:RegisterChatCommand("la", function(input)
        if input == "freset" then GC.UI.ResetFrames() end
        GC.UI.ShowMainWindow(true)  -- Just shows the pill
    end)
    GC:RegisterChatCommand("gc", function(input)
        if input == "freset" then GC.UI.ResetFrames() end
        GC.UI.ShowMainWindow(true)  -- Just shows the pill
    end)
    GC:RegisterChatCommand("lal", function()
        if not GC.Session.IsRunning() then GC.Session.Start(false) end
        GC.UI.ShowLiteWindow()
    end)
    GC:RegisterChatCommand("laa", function()
        if not GC.Session.IsRunning() then GC.Session.Start(true) end
        GC.UI.ShowLastNoteworthyItemWindow()
    end)
    GC:RegisterChatCommand("lade", function()
        GC:Print(tostring(GC.GetFromDb("pricesource", "useDisenchantValue",
                                       "TSM_REQUIRED")))
    end)
    GC:RegisterChatCommand("laconfig", function()
        if Settings and Settings.OpenToCategory then
            pcall(Settings.OpenToCategory, GC.CONST.METADATA.NAME)
        else
            pcall(InterfaceOptionsFrame_OpenToCategory, GC.CONST.METADATA.NAME)
        end
    end)
    GC.Commands.Register()

    -- Register events
    GC:RegisterEvent("CHAT_MSG_SYSTEM", GC.Events.OnResetInfoEvent)
    GC:RegisterEvent("CHAT_MSG_ADDON", GC.Events.OnChatMsgAddon)
    GC:RegisterEvent("CHAT_MSG_LOOT", GC.Events.OnChatMsgLoot)
    GC:RegisterEvent("CHAT_MSG_MONEY", GC.Events.OnChatMsgMoney)

    if GC.Util.IsModern then
        GC:RegisterEvent("TRADE_SKILL_ITEM_CRAFTED_RESULT",
                         GC.Events.OnTradeSkillCrafted)
    end

    -- Kill tracking: version-aware strategy
    if GC.Util.IsClassic then
        GC:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED",
                         GC.KillTracker.OnCombatLogEvent)
    else
        -- Backup: LOOT_READY fires when loot window data is available
        GC:RegisterEvent("LOOT_READY", GC.KillTracker.OnLootReady)
        -- Count dead target/mouseover when leaving combat
        GC:RegisterEvent("PLAYER_REGEN_ENABLED", GC.KillTracker.OnRegenEnabled)
    end

    -- Register addon message prefix for group loot
    C_ChatInfo.RegisterAddonMessagePrefix(GC.CONST.PARTYLOOT_MSGPREFIX)

    -- Merchant events
    GC:RegisterEvent("MERCHANT_SHOW", GC.Merchant.OnShow)
    GC:RegisterEvent("MERCHANT_CLOSED", GC.Merchant.OnClose)

    -- Auto-show the Pill UI 1 second after login (gives time for frames to be ready)
    C_Timer.After(1, function()
        GC.UI.ShowMainWindow(true)
    end)
end

--[[---------------------------------------------------------------------------
    ShowStartSessionDialog — just show the Pill UI, no auto-start.
    The user must press Play on the Pill to explicitly start a session.
    This is called on login (via autoStartLA or ADDON_LOADED events).
----------------------------------------------------------------------------]]
function GC:ShowStartSessionDialog()
    -- Simply show the Pill
    GC.UI.ShowMainWindow(true)
end
