-- LongChat settings panel.

local LC = _G.LongChat
local UI = LC and LC.UI
if not UI then
  return
end

function UI:RegisterSettings()
  if self.settingsPanel then
    return
  end

  local panel = CreateFrame("Frame", "LongChatSettingsPanel", UIParent)
  panel.name = "LongChat"

  local profilePanel = CreateFrame("Frame", "LongChatProfilesSettingsPanel", UIParent)
  profilePanel.name = "Profiles"
  profilePanel.parent = "LongChat"

  local title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
  title:SetPoint("TOPLEFT", 16, -16)
  title:SetText("LongChat")

  local desc = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  desc:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
  desc:SetWidth(520)
  desc:SetJustifyH("LEFT")
  desc:SetText("LongChat splits oversized chat messages and includes an optional composer for longer drafts.")

  local profilePageTitle = profilePanel:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
  profilePageTitle:SetPoint("TOPLEFT", 16, -16)
  profilePageTitle:SetText("LongChat - Profiles")

  local profileDesc = profilePanel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  profileDesc:SetPoint("TOPLEFT", profilePageTitle, "BOTTOMLEFT", 0, -8)
  profileDesc:SetWidth(520)
  profileDesc:SetJustifyH("LEFT")
  profileDesc:SetText("Pick a profile, then activate, rename, copy, delete, or reset it.")

  local profileTitle = profilePanel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  profileTitle:SetPoint("TOPLEFT", profileDesc, "BOTTOMLEFT", 0, -24)
  profileTitle:SetText("Profiles")

  local currentProfile = profilePanel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  currentProfile:SetPoint("TOPLEFT", profileTitle, "BOTTOMLEFT", 0, -10)
  currentProfile:SetWidth(520)
  currentProfile:SetJustifyH("LEFT")

  local selectedProfileLabel = profilePanel:CreateFontString(nil, "ARTWORK", "GameFontNormalSmall")
  selectedProfileLabel:SetPoint("TOPLEFT", currentProfile, "BOTTOMLEFT", 0, -12)
  selectedProfileLabel:SetText("Selected profile")

  local selectedProfileName
  local profileDropdown = CreateFrame("Frame", "LongChatSettingsProfileDropdown", profilePanel, "UIDropDownMenuTemplate")
  profileDropdown:SetPoint("TOPLEFT", selectedProfileLabel, "BOTTOMLEFT", -18, -6)
  UIDropDownMenu_SetWidth(profileDropdown, 190)

  local activateProfile = self:Button(profilePanel, "Activate", 124, function()
    if not selectedProfileName or not LC.SetCurrentEditorProfileName then
      return
    end
    if LC.FlushSettings then
      LC.FlushSettings()
    end
    if LC.SetCurrentEditorProfileName(selectedProfileName) then
      UI:RefreshFromProfile()
      if profilePanel.refresh then
        profilePanel:refresh()
      end
    end
  end)
  activateProfile:SetPoint("LEFT", profileDropdown, "RIGHT", 8, 2)

  local profileNameLabel = profilePanel:CreateFontString(nil, "ARTWORK", "GameFontNormalSmall")
  profileNameLabel:SetPoint("TOPLEFT", profileDropdown, "BOTTOMLEFT", 18, -8)
  profileNameLabel:SetText("Name for create, rename, or copy")

  local profileNameBox = CreateFrame("EditBox", "LongChatSettingsProfileName", profilePanel, "InputBoxTemplate")
  profileNameBox:SetPoint("TOPLEFT", profileNameLabel, "BOTTOMLEFT", 4, -6)
  profileNameBox:SetSize(210, 22)
  profileNameBox:SetAutoFocus(false)
  profileNameBox:SetMaxLetters(54)

  local createProfile = self:Button(profilePanel, "Create New", 124, function()
    local name = profileNameBox:GetText() or ""
    if LC.FlushSettings then
      LC.FlushSettings()
    end
    if LC.CreateEditorProfile and LC.CreateEditorProfile(name) then
      selectedProfileName = LC.GetCurrentEditorProfileName and LC.GetCurrentEditorProfileName()
      profileNameBox:SetText("")
      UI:RefreshFromProfile()
      if profilePanel.refresh then
        profilePanel:refresh()
      end
    end
  end)
  createProfile:SetPoint("LEFT", profileNameBox, "RIGHT", 44, 0)

  local copyProfile = self:Button(profilePanel, "Copy Active To", 124, function()
    local name = profileNameBox:GetText() or ""
    if LC.FlushSettings then
      LC.FlushSettings()
    end
    if LC.CopyEditorProfile and LC.CopyEditorProfile(LC.GetCurrentEditorProfileName and LC.GetCurrentEditorProfileName(), name) then
      selectedProfileName = LC.GetCurrentEditorProfileName and LC.GetCurrentEditorProfileName()
      profileNameBox:SetText("")
      UI:RefreshFromProfile()
      if profilePanel.refresh then
        profilePanel:refresh()
      end
    end
  end)
  copyProfile:SetPoint("TOPLEFT", profileNameBox, "BOTTOMLEFT", 0, -8)

  local renameProfile = self:Button(profilePanel, "Rename Selected", 124, function()
    local name = profileNameBox:GetText() or ""
    if not selectedProfileName then
      return
    end
    if LC.FlushSettings then
      LC.FlushSettings()
    end
    if LC.RenameEditorProfile and LC.RenameEditorProfile(selectedProfileName, name) then
      selectedProfileName = tostring(name or ""):gsub("^%s+", ""):gsub("%s+$", "")
      if selectedProfileName == "" then
        selectedProfileName = LC.GetCurrentEditorProfileName and LC.GetCurrentEditorProfileName()
      end
      profileNameBox:SetText("")
      UI:RefreshFromProfile()
      if profilePanel.refresh then
        profilePanel:refresh()
      end
    end
  end)
  renameProfile:SetPoint("LEFT", copyProfile, "RIGHT", 116, 0)

  local resetProfile = self:Button(profilePanel, "Reset Active", 124, function()
    if LC.FlushSettings then
      LC.FlushSettings()
    end
    if LC.ResetCurrentEditorProfile and LC.ResetCurrentEditorProfile() then
      UI:RefreshFromProfile()
      if profilePanel.refresh then
        profilePanel:refresh()
      end
    end
  end)
  resetProfile:SetPoint("TOPLEFT", copyProfile, "BOTTOMLEFT", 0, -6)

  local deleteProfile = self:Button(profilePanel, "Delete Selected", 124, function()
    if not selectedProfileName or not LC.DeleteEditorProfile then
      return
    end
    if LC.DeleteEditorProfile(selectedProfileName) then
      selectedProfileName = LC.GetCurrentEditorProfileName and LC.GetCurrentEditorProfileName()
      profileNameBox:SetText("")
      UI:RefreshFromProfile()
      if profilePanel.refresh then
        profilePanel:refresh()
      end
    end
  end)
  deleteProfile:SetPoint("LEFT", resetProfile, "RIGHT", 116, 0)

  UIDropDownMenu_Initialize(profileDropdown, function(_, level)
    local names = LC.GetEditorProfileNames and LC.GetEditorProfileNames() or {}
    for _, profileName in ipairs(names) do
      local info = UIDropDownMenu_CreateInfo()
      info.text = profileName
      info.checked = selectedProfileName == profileName
      info.func = function()
        selectedProfileName = profileName
        UIDropDownMenu_SetText(profileDropdown, profileName)
        profileNameBox:SetText(profileName)
      end
      UIDropDownMenu_AddButton(info, level)
    end
  end)

  local scaleTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  scaleTitle:SetPoint("TOPLEFT", desc, "BOTTOMLEFT", 0, -28)
  scaleTitle:SetText("Composer scale")

  local scaleValue = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  scaleValue:SetPoint("LEFT", scaleTitle, "RIGHT", 12, 0)

  local slider = CreateFrame("Slider", "LongChatComposerScaleSlider", panel, "OptionsSliderTemplate")
  slider:SetPoint("TOPLEFT", scaleTitle, "BOTTOMLEFT", 0, -18)
  slider:SetMinMaxValues(self.SCALE_MIN, self.SCALE_MAX)
  slider:SetValueStep(0.05)
  slider:SetObeyStepOnDrag(true)
  slider:SetWidth(240)
  _G[slider:GetName() .. "Low"]:SetText(string.format("%.2f", self.SCALE_MIN))
  _G[slider:GetName() .. "High"]:SetText(string.format("%.2f", self.SCALE_MAX))
  _G[slider:GetName() .. "Text"]:SetText("")
  slider:SetScript("OnValueChanged", function(_, value)
    LC.SetComposerScale(value)
    scaleValue:SetText(string.format("%.2fx", UI:Editor().scale))
  end)

  local fontScaleTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  fontScaleTitle:SetPoint("TOPLEFT", slider, "BOTTOMLEFT", 0, -24)
  fontScaleTitle:SetText("Composer font scale")

  local fontScaleValue = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  fontScaleValue:SetPoint("LEFT", fontScaleTitle, "RIGHT", 12, 0)

  local fontScale = CreateFrame("Slider", "LongChatComposerFontScaleSlider", panel, "OptionsSliderTemplate")
  fontScale:SetPoint("TOPLEFT", fontScaleTitle, "BOTTOMLEFT", 0, -18)
  fontScale:SetMinMaxValues(self.FONT_SCALE_MIN, self.FONT_SCALE_MAX)
  fontScale:SetValueStep(0.05)
  fontScale:SetObeyStepOnDrag(true)
  fontScale:SetWidth(240)
  _G[fontScale:GetName() .. "Low"]:SetText(string.format("%.2f", self.FONT_SCALE_MIN))
  _G[fontScale:GetName() .. "High"]:SetText(string.format("%.2f", self.FONT_SCALE_MAX))
  _G[fontScale:GetName() .. "Text"]:SetText("")
  fontScale:SetScript("OnValueChanged", function(_, value)
    LC.SetComposerFontScale(value)
    fontScaleValue:SetText(string.format("%.2fx", UI:Editor().fontScale))
  end)

  local opacityTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  opacityTitle:SetPoint("TOPLEFT", fontScale, "BOTTOMLEFT", 0, -24)
  opacityTitle:SetText("Composer opacity")

  local opacityValue = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  opacityValue:SetPoint("LEFT", opacityTitle, "RIGHT", 12, 0)

  local opacity = CreateFrame("Slider", "LongChatComposerOpacitySlider", panel, "OptionsSliderTemplate")
  opacity:SetPoint("TOPLEFT", opacityTitle, "BOTTOMLEFT", 0, -18)
  opacity:SetMinMaxValues(self.OPACITY_MIN, self.OPACITY_MAX)
  opacity:SetValueStep(0.05)
  opacity:SetObeyStepOnDrag(true)
  opacity:SetWidth(240)
  _G[opacity:GetName() .. "Low"]:SetText(string.format("%.0f%%", self.OPACITY_MIN * 100))
  _G[opacity:GetName() .. "High"]:SetText(string.format("%.0f%%", self.OPACITY_MAX * 100))
  _G[opacity:GetName() .. "Text"]:SetText("")
  opacity:SetScript("OnValueChanged", function(_, value)
    LC.SetComposerOpacity(value)
    opacityValue:SetText(string.format("%.0f%%", UI:Editor().opacity * 100))
  end)

  local minimap = CreateFrame("CheckButton", "LongChatSettingsMinimap", panel, "InterfaceOptionsCheckButtonTemplate")
  minimap:SetPoint("TOPLEFT", desc, "BOTTOMLEFT", 306, -24)
  _G[minimap:GetName() .. "Text"]:SetText("Show minimap button")
  minimap:SetScript("OnClick", function(self)
    LC.SetMinimapHidden(not self:GetChecked())
  end)

  local clear = CreateFrame("CheckButton", "LongChatSettingsClearAfterSend", panel, "InterfaceOptionsCheckButtonTemplate")
  clear:SetPoint("TOPLEFT", minimap, "BOTTOMLEFT", 0, -8)
  _G[clear:GetName() .. "Text"]:SetText("Clear composer after send")
  clear:SetScript("OnClick", function(self)
    UI:Store("clearOnSend", self:GetChecked() == true)
    if UI.composerClearCheck then
      UI.composerClearCheck:SetChecked(self:GetChecked())
    end
  end)

  local markers = CreateFrame("CheckButton", "LongChatSettingsSplitMarkers", panel, "InterfaceOptionsCheckButtonTemplate")
  markers:SetPoint("TOPLEFT", clear, "BOTTOMLEFT", 0, -8)
  _G[markers:GetName() .. "Text"]:SetText("Show split markers")
  markers:SetScript("OnClick", function(self)
    if LC.SetSplitMarkers then
      LC.SetSplitMarkers(self:GetChecked() == true)
    end
    if UI.editBox then
      UI:ApplyComposerSplitMarkers(UI.editBox, true)
      UI:RememberDraft()
      UI:RefreshCharacterCounter()
    end
  end)

  local themeTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  themeTitle:SetPoint("TOPLEFT", markers, "BOTTOMLEFT", 4, -14)
  themeTitle:SetText("Composer background")

  local themeDropdown
  local customSwatch
  local textSwatch

  local function update_custom_swatch()
    if not customSwatch then
      return
    end
    local color = UI:Editor().customColor or {}
    customSwatch:SetBackdropColor(color.r or 0.08, color.g or 0.10, color.b or 0.12, 1)
  end

  local function apply_custom_color(r, g, b)
    LC.SetComposerCustomColor(r, g, b)
    if themeDropdown then
      UIDropDownMenu_SetText(themeDropdown, UI.THEME_LABELS.custom or "Custom")
    end
    update_custom_swatch()
  end

  local function restore_custom_color(previous)
    if type(previous) == "table" then
      apply_custom_color(previous.r or previous[1], previous.g or previous[2], previous.b or previous[3])
    end
  end

  local function open_custom_color_picker()
    if not ColorPickerFrame then
      return
    end

    local color = UI:Editor().customColor or {}
    local r = color.r or 0.08
    local g = color.g or 0.10
    local b = color.b or 0.12
    local function changed()
      local newR, newG, newB = ColorPickerFrame:GetColorRGB()
      apply_custom_color(newR, newG, newB)
    end

    if ColorPickerFrame.SetupColorPickerAndShow then
      ColorPickerFrame:SetupColorPickerAndShow({
        r = r,
        g = g,
        b = b,
        swatchFunc = changed,
        cancelFunc = restore_custom_color,
      })
      return
    end

    ColorPickerFrame.previousValues = { r = r, g = g, b = b }
    ColorPickerFrame.func = changed
    ColorPickerFrame.cancelFunc = restore_custom_color
    ColorPickerFrame.hasOpacity = false
    ColorPickerFrame:SetColorRGB(r, g, b)
    ColorPickerFrame:Show()
  end

  local function update_text_swatch()
    if not textSwatch then
      return
    end
    local color = UI:Editor().textColor or {}
    textSwatch:SetBackdropColor(color.r or 1, color.g or 1, color.b or 1, 1)
  end

  local function apply_text_color(r, g, b)
    LC.SetComposerTextColor(r, g, b)
    update_text_swatch()
  end

  local function restore_text_color(previous)
    if type(previous) == "table" then
      apply_text_color(previous.r or previous[1], previous.g or previous[2], previous.b or previous[3])
    end
  end

  local function open_text_color_picker()
    if not ColorPickerFrame then
      return
    end

    local color = UI:Editor().textColor or {}
    local r = color.r or 1
    local g = color.g or 1
    local b = color.b or 1
    local function changed()
      local newR, newG, newB = ColorPickerFrame:GetColorRGB()
      apply_text_color(newR, newG, newB)
    end

    if ColorPickerFrame.SetupColorPickerAndShow then
      ColorPickerFrame:SetupColorPickerAndShow({
        r = r,
        g = g,
        b = b,
        swatchFunc = changed,
        cancelFunc = restore_text_color,
      })
      return
    end

    ColorPickerFrame.previousValues = { r = r, g = g, b = b }
    ColorPickerFrame.func = changed
    ColorPickerFrame.cancelFunc = restore_text_color
    ColorPickerFrame.hasOpacity = false
    ColorPickerFrame:SetColorRGB(r, g, b)
    ColorPickerFrame:Show()
  end

  themeDropdown = CreateFrame("Frame", "LongChatSettingsThemeDropdown", panel, "UIDropDownMenuTemplate")
  themeDropdown:SetPoint("TOPLEFT", themeTitle, "BOTTOMLEFT", -18, -6)
  UIDropDownMenu_SetWidth(themeDropdown, 150)
  UIDropDownMenu_Initialize(themeDropdown, function(_, level)
    local editor = UI:Editor()
    for _, theme in ipairs(UI.THEMES or {}) do
      local info = UIDropDownMenu_CreateInfo()
      info.text = theme.label
      info.checked = editor.theme == theme.key
      info.func = function()
        LC.SetComposerTheme(theme.key)
        UIDropDownMenu_SetText(themeDropdown, theme.label)
      end
      UIDropDownMenu_AddButton(info, level)
    end
  end)

  customSwatch = CreateFrame("Button", "LongChatSettingsCustomColor", panel, "BackdropTemplate")
  customSwatch:SetPoint("LEFT", themeDropdown, "RIGHT", -4, 2)
  customSwatch:SetSize(24, 24)
  customSwatch:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    tile = false,
    edgeSize = 10,
    insets = { left = 2, right = 2, top = 2, bottom = 2 },
  })
  customSwatch:SetBackdropBorderColor(0.75, 0.62, 0.35, 1)
  customSwatch:SetScript("OnClick", open_custom_color_picker)
  customSwatch:SetScript("OnEnter", function(self)
    self:SetBackdropBorderColor(1, 0.82, 0.28, 1)
  end)
  customSwatch:SetScript("OnLeave", function(self)
    self:SetBackdropBorderColor(0.75, 0.62, 0.35, 1)
  end)
  update_custom_swatch()

  local textColorTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  textColorTitle:SetPoint("TOPLEFT", themeDropdown, "BOTTOMLEFT", 18, -8)
  textColorTitle:SetText("Composer text")

  textSwatch = CreateFrame("Button", "LongChatSettingsTextColor", panel, "BackdropTemplate")
  textSwatch:SetPoint("LEFT", textColorTitle, "RIGHT", 12, 0)
  textSwatch:SetSize(24, 24)
  textSwatch:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    tile = false,
    edgeSize = 10,
    insets = { left = 2, right = 2, top = 2, bottom = 2 },
  })
  textSwatch:SetBackdropBorderColor(0.75, 0.62, 0.35, 1)
  textSwatch:SetScript("OnClick", open_text_color_picker)
  textSwatch:SetScript("OnEnter", function(self)
    self:SetBackdropBorderColor(1, 0.82, 0.28, 1)
  end)
  textSwatch:SetScript("OnLeave", function(self)
    self:SetBackdropBorderColor(0.75, 0.62, 0.35, 1)
  end)
  update_text_swatch()

  local resetTextColor = self:Button(panel, "Reset", 62, function()
    if LC.ResetComposerTextColor then
      LC.ResetComposerTextColor()
    end
    update_text_swatch()
  end)
  resetTextColor:SetPoint("LEFT", textSwatch, "RIGHT", 8, 0)

  local spell = CreateFrame("CheckButton", "LongChatSettingsEnableSpellcheck", panel, "InterfaceOptionsCheckButtonTemplate")
  spell:SetPoint("TOPLEFT", textColorTitle, "BOTTOMLEFT", -4, -10)
  _G[spell:GetName() .. "Text"]:SetText("Enable built-in spellcheck")
  spell:SetScript("OnClick", function(self)
    if LC.Spellcheck and LC.Spellcheck.SetEnabled then
      LC.Spellcheck:SetEnabled(self:GetChecked() == true)
    end
  end)

  local composerSpellTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  composerSpellTitle:SetPoint("TOPLEFT", spell, "BOTTOMLEFT", 4, -12)
  composerSpellTitle:SetText("Composer spellcheck display")

  local composerSpell = CreateFrame("Frame", "LongChatSettingsComposerSpellDisplay", panel, "UIDropDownMenuTemplate")
  composerSpell:SetPoint("TOPLEFT", composerSpellTitle, "BOTTOMLEFT", -18, -6)
  UIDropDownMenu_SetWidth(composerSpell, 150)
  UIDropDownMenu_Initialize(composerSpell, function(_, level)
    local editor = UI:Editor()
    for _, display in ipairs(UI.COMPOSER_SPELL_DISPLAYS or {}) do
      local info = UIDropDownMenu_CreateInfo()
      info.text = display.label
      info.checked = editor.composerSpellDisplay == display.key
      info.func = function()
        if LC.SetComposerSpellDisplay then
          LC.SetComposerSpellDisplay(display.key)
        end
        UIDropDownMenu_SetText(composerSpell, display.label)
      end
      UIDropDownMenu_AddButton(info, level)
    end
  end)

  local open = self:Button(panel, "Open Composer", 150, function()
    LC.ShowComposer()
  end)
  open:SetPoint("TOPLEFT", composerSpell, "BOTTOMLEFT", 18, -12)

  function profilePanel:refresh()
    local editor = UI:Editor()
    selectedProfileName = selectedProfileName or (LC.GetCurrentEditorProfileName and LC.GetCurrentEditorProfileName())
    local activeProfile = LC.GetCurrentEditorProfileName and LC.GetCurrentEditorProfileName() or tostring(editor.profileName or "Profile")
    if not selectedProfileName then
      selectedProfileName = activeProfile
    end
    currentProfile:SetText("Current Profile: " .. tostring(activeProfile))
    UIDropDownMenu_SetText(profileDropdown, selectedProfileName)
  end

  profilePanel:SetScript("OnShow", function(self)
    self:refresh()
  end)

  function panel:refresh()
    local editor = UI:Editor()
    slider:SetValue(editor.scale)
    scaleValue:SetText(string.format("%.2fx", editor.scale))
    fontScale:SetValue(editor.fontScale)
    fontScaleValue:SetText(string.format("%.2fx", editor.fontScale))
    opacity:SetValue(editor.opacity)
    opacityValue:SetText(string.format("%.0f%%", editor.opacity * 100))
    minimap:SetChecked(not editor.minimap.hide)
    clear:SetChecked(editor.clearOnSend)
    markers:SetChecked(LC.DB and LC.DB.splitMarkers ~= false)
    UIDropDownMenu_SetText(themeDropdown, UI.THEME_LABELS[editor.theme] or "LongChat Dark")
    update_custom_swatch()
    update_text_swatch()
    spell:SetChecked(LC.Spellcheck and LC.Spellcheck:IsEnabled())
    UIDropDownMenu_SetText(composerSpell, UI.COMPOSER_SPELL_DISPLAY_LABELS[editor.composerSpellDisplay] or "Footer indicator")
  end

  panel:SetScript("OnShow", function(self)
    self:refresh()
  end)

  if InterfaceOptions_AddCategory then
    InterfaceOptions_AddCategory(panel)
    InterfaceOptions_AddCategory(profilePanel)
  elseif Settings then
    local category = Settings.RegisterCanvasLayoutCategory(panel, panel.name)
    Settings.RegisterAddOnCategory(category)
    if category and category.GetID then
      panel.categoryID = category:GetID()
    end
    if Settings.RegisterCanvasLayoutSubcategory then
      local subcategory = Settings.RegisterCanvasLayoutSubcategory(category, profilePanel, profilePanel.name)
      if subcategory and subcategory.GetID then
        profilePanel.categoryID = subcategory:GetID()
      end
    else
      local profileCategory = Settings.RegisterCanvasLayoutCategory(profilePanel, "LongChat Profiles")
      Settings.RegisterAddOnCategory(profileCategory)
      if profileCategory and profileCategory.GetID then
        profilePanel.categoryID = profileCategory:GetID()
      end
    end
  end

  self.settingsPanel = panel
  self.profileSettingsPanel = profilePanel
end

function LC.OpenSettings()
  UI:RegisterSettings()
  local panel = UI.settingsPanel
  if not panel then
    return
  end
  if panel.refresh then
    panel:refresh()
  end
  if Settings and panel.categoryID then
    Settings.OpenToCategory(panel.categoryID)
    Settings.OpenToCategory(panel.categoryID)
  elseif InterfaceOptionsFrame_OpenToCategory then
    InterfaceOptionsFrame_OpenToCategory(panel)
    InterfaceOptionsFrame_OpenToCategory(panel)
  end
end

hooksecurefunc(LC, "OnAddonReady", function()
  UI:RegisterSettings()
end)
