local addonName, LRH = ...
LRH = LRH or {}
local L = LRH.L or {}

local function CreatePanelContents(panel)
    local title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", 16, -16)
    title:SetText(L.OPTIONS_TITLE or (L.ADDON_NAME or "Left Right Helper"))

    local desc = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    desc:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
    desc:SetWidth(520)
    desc:SetJustifyH("LEFT")
    desc:SetText(L.OPTIONS_DESC or "")

    local btnStart = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
    btnStart:SetSize(220, 24)
    btnStart:SetPoint("TOPLEFT", desc, "BOTTOMLEFT", 0, -16)
    btnStart:SetText(L.START_DRAG or "Start Drag Mode")
    btnStart:SetScript("OnClick", function()
        if LRH.SetDragMode then LRH:SetDragMode(true) end
        if SettingsPanel and SettingsPanel:IsShown() then
            HideUIPanel(SettingsPanel)
        end
    end)

    local resetBtn = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
    resetBtn:SetSize(220, 24)
    resetBtn:SetPoint("TOPLEFT", btnStart, "BOTTOMLEFT", 0, -12)
    resetBtn:SetText(L.RESET_BUTTON or "Reset to defaults")
    resetBtn:SetScript("OnClick", function()
        if LRH.ResetRequested then LRH:ResetRequested() end
    end)
end

local function RegisterSettingsPanel()
    if not Settings or not Settings.RegisterCanvasLayoutCategory then
        return
    end

    local panel = CreateFrame("Frame")
    panel.name = L.ADDON_NAME or "Left Right Helper"
    CreatePanelContents(panel)

    local category = Settings.RegisterCanvasLayoutCategory(panel, panel.name)
    Settings.RegisterAddOnCategory(category)
end

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", RegisterSettingsPanel)