local addonName, SinsToolkit = ...

local Standalone = CreateFrame("Frame")
SinsToolkit.Standalone = Standalone

function Standalone:Init()
    local AceConfig = LibStub("AceConfig-3.0")
    local AceConfigDialog = LibStub("AceConfigDialog-3.0")

    -- 1. Register the options table dynamically via callback
    AceConfig:RegisterOptionsTable(addonName, function()
        return SinsToolkit:GetOptionsTable("Standalone")
    end)

    -- 2. Register into Blizzard's modern Settings -> AddOns menu
    -- Ace3 acts as a wrapper for Settings.RegisterCanvasLayoutCategory internally
    self.optionsFrame = AceConfigDialog:AddToBlizOptions(addonName, "Sin's Toolkit: QOL")

    -- 3. Setup the Minimap Button
    local LDB = LibStub("LibDataBroker-1.1", true)
    local DBIcon = LibStub("LibDBIcon-1.0", true)
    
    if LDB and DBIcon then
        local dataObject = LDB:NewDataObject(addonName, {
            type = "launcher",
            text = "Sin's Toolkit: QOL",
            icon = "Interface\\AddOns\\SinsToolkit\\Media\\Logo.tga",
            OnClick = function(_, button)
                if button == "RightButton" then
                    if SinsToolkit.TeleportHub and SinsToolkit.TeleportHub.Open then
                        SinsToolkit.TeleportHub:Open()
                    elseif SinsToolkit.Window then
                        SinsToolkit.Window:Show()
                        if SinsToolkit.Window.SelectPage then
                            SinsToolkit.Window:SelectPage("Teleports")
                        end
                    end
                elseif button == "LeftButton" and IsShiftKeyDown() then
                    Standalone:ToggleOptions()
                else
                    if SinsToolkit.Window then
                        SinsToolkit.Window:Toggle()
                    else
                        Standalone:ToggleOptions()
                    end
                end
            end,
            OnTooltipShow = function(tooltip)
                tooltip:AddLine("|cffb868dcSin's Toolkit: QOL|r", 1, 1, 1)
                tooltip:AddLine("Left-Click: Open Sin's Toolkit", 0.74, 0.45, 0.95)
                tooltip:AddLine("Shift+Left-Click: Blizzard Settings", 0.74, 0.45, 0.95)
                tooltip:AddLine("Right-Click: Open Teleport Hub", 0.85, 0.66, 0.30)
            end,
        })
        
        SinsToolkitDB.minimap = SinsToolkitDB.minimap or { hide = false }
        DBIcon:Register(addonName, dataObject, SinsToolkitDB.minimap)
    end
end

function Standalone:ToggleOptions()
    if not self.optionsFrame then
        print("|cff9b59b6[Sin's Toolkit: QOL]|r Options panel is not registered yet.")
        return
    end

    if Settings and Settings.OpenToCategory then
        local categoryID

        if type(self.optionsFrame.category) == "table"
            and type(self.optionsFrame.category.GetID) == "function" then
            categoryID = self.optionsFrame.category:GetID()
        end

        Settings.OpenToCategory(
            categoryID
            or self.optionsFrame.name
            or "Sin's Toolkit: QOL"
        )
    elseif InterfaceOptionsFrame_OpenToCategory then
        InterfaceOptionsFrame_OpenToCategory(self.optionsFrame)
    else
        print("|cff9b59b6[Sin's Toolkit: QOL]|r Unable to open the options panel on this client.")
    end
end

-- Hook initialization to ADDON_LOADED. Core runs first due to TOC ordering, so DB is ready.
Standalone:RegisterEvent("ADDON_LOADED")
Standalone:SetScript("OnEvent", function(self, event, loadedAddon)
    if event == "ADDON_LOADED" and loadedAddon == addonName then
        self:UnregisterEvent("ADDON_LOADED")
        self:Init()
    end
end)
