local AN = _G.AmmeNotepad
if not AN then return end

local function clamp(v, lo, hi)
  if v < lo then return lo end
  if v > hi then return hi end
  return v
end

local function MakeRowTitle(parent, text, y)
  local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
  fs:SetPoint("TOPLEFT", 16, y)
  fs:SetText(text)
  return fs
end

function AN:CreateOptionsPanel()
  if self.OptionsPanel then return end

  local panel = CreateFrame("Frame", "AmmeNotepad_OptionsPanel", UIParent)
  panel.name = "AmmeNotepad"

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

  local subtitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  subtitle:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -4)
  subtitle:SetText("Account-wide tabbed notepad settings")

  local y = -52
  local maxLabel = MakeRowTitle(panel, "Maximum number of notes/tabs", y)
  local maxSlider = CreateFrame("Slider", "AmmeNotepad_MaxNotesSlider", panel, "OptionsSliderTemplate")
  maxSlider:SetPoint("TOPLEFT", maxLabel, "BOTTOMLEFT", 0, -10)
  maxSlider:SetMinMaxValues(20, 200)
  maxSlider:SetValueStep(1)
  maxSlider:SetObeyStepOnDrag(true)
  maxSlider:SetWidth(320)
  _G[maxSlider:GetName() .. "Low"]:SetText("20")
  _G[maxSlider:GetName() .. "High"]:SetText("200")

  local maxText = panel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  maxText:SetPoint("LEFT", maxSlider, "RIGHT", 12, 0)

  y = -132
  local autoCheck = CreateFrame("CheckButton", "AmmeNotepad_AutosaveCheck", panel, "ChatConfigCheckButtonTemplate")
  autoCheck:SetPoint("TOPLEFT", 16, y)
  autoCheck.Text:SetText("Enable autosave")

  y = -174
  local freqLabel = MakeRowTitle(panel, "Autosave frequency (minutes)", y)
  local freqSlider = CreateFrame("Slider", "AmmeNotepad_AutosaveSlider", panel, "OptionsSliderTemplate")
  freqSlider:SetPoint("TOPLEFT", freqLabel, "BOTTOMLEFT", 0, -10)
  freqSlider:SetMinMaxValues(5, 30)
  freqSlider:SetValueStep(1)
  freqSlider:SetObeyStepOnDrag(true)
  freqSlider:SetWidth(320)
  _G[freqSlider:GetName() .. "Low"]:SetText("5")
  _G[freqSlider:GetName() .. "High"]:SetText("30")

  local freqText = panel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  freqText:SetPoint("LEFT", freqSlider, "RIGHT", 12, 0)

  y = -254
  local charCheck = CreateFrame("CheckButton", "AmmeNotepad_CharacterFilterCheck", panel, "ChatConfigCheckButtonTemplate")
  charCheck:SetPoint("TOPLEFT", 16, y)
  charCheck.Text:SetText("Character specific notes (view filter only)")

  local info = panel:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
  info:SetPoint("TOPLEFT", charCheck, "BOTTOMLEFT", 4, -6)
  info:SetText("Only shows notes created by your current character while keeping all notes in account-wide SavedVariables.")
  info:SetWidth(620)
  info:SetJustifyH("LEFT")

  y = -324
  local minimapCheck = CreateFrame("CheckButton", "AmmeNotepad_MinimapIconCheck", panel, "ChatConfigCheckButtonTemplate")
  minimapCheck:SetPoint("TOPLEFT", 16, y)
  minimapCheck.Text:SetText("Show minimap icon")

  y = -366
  local scaleLabel = MakeRowTitle(panel, "UI scale", y)
  local scaleSlider = CreateFrame("Slider", "AmmeNotepad_UIScaleSlider", panel, "OptionsSliderTemplate")
  scaleSlider:SetPoint("TOPLEFT", scaleLabel, "BOTTOMLEFT", 0, -10)
  scaleSlider:SetMinMaxValues(0.5, 1.8)
  scaleSlider:SetValueStep(0.1)
  scaleSlider:SetObeyStepOnDrag(true)
  scaleSlider:SetWidth(320)
  _G[scaleSlider:GetName() .. "Low"]:SetText("0.5x")
  _G[scaleSlider:GetName() .. "High"]:SetText("1.8x")

  local scaleText = panel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  scaleText:SetPoint("LEFT", scaleSlider, "RIGHT", 12, 0)

  panel:SetScript("OnShow", function()
    maxSlider:SetValue(tonumber(AN.DB.settings.maxNotes) or 100)
    maxText:SetText(tostring(math.floor(maxSlider:GetValue() + 0.5)))

    autoCheck:SetChecked(AN.DB.settings.autosaveEnabled ~= false)

    freqSlider:SetValue(tonumber(AN.DB.settings.autosaveIntervalMins) or 5)
    freqText:SetText(string.format("%d mins", math.floor(freqSlider:GetValue() + 0.5)))

    charCheck:SetChecked(AN.DB.settings.characterSpecific == true)
    minimapCheck:SetChecked(not (AN.DB.minimap and AN.DB.minimap.hide))

    local scale = clamp(tonumber(AN.DB.settings.uiScale) or 1.0, 0.5, 1.8)
    scale = math.floor((scale * 10) + 0.5) / 10
    scaleSlider:SetValue(scale)
    scaleText:SetText(string.format("%.1fx", scale))
  end)

  maxSlider:SetScript("OnValueChanged", function(_, value)
    value = math.floor(value + 0.5)
    AN.DB.settings.maxNotes = clamp(value, 20, 200)
    maxText:SetText(tostring(AN.DB.settings.maxNotes))
    if AN.RefreshTabs then
      AN:RefreshTabs()
    end
  end)

  autoCheck:SetScript("OnClick", function(selfCheck)
    AN.DB.settings.autosaveEnabled = selfCheck:GetChecked() and true or false
    if AN.RestartAutosaveTicker then
      AN:RestartAutosaveTicker()
    end
  end)

  freqSlider:SetScript("OnValueChanged", function(_, value)
    value = clamp(math.floor(value + 0.5), 5, 30)
    AN.DB.settings.autosaveIntervalMins = value
    freqText:SetText(string.format("%d mins", value))
    if AN.RestartAutosaveTicker then
      AN:RestartAutosaveTicker()
    end
  end)

  charCheck:SetScript("OnClick", function(selfCheck)
    AN.DB.settings.characterSpecific = selfCheck:GetChecked() and true or false
    if AN.RefreshAllUI then
      AN:RefreshAllUI()
    end
  end)

  minimapCheck:SetScript("OnClick", function(selfCheck)
    local show = selfCheck:GetChecked() and true or false
    if AN.SetMinimapHidden then
      AN:SetMinimapHidden(not show)
    else
      AN.DB.minimap = AN.DB.minimap or {}
      AN.DB.minimap.hide = not show
    end
  end)

  scaleSlider:SetScript("OnValueChanged", function(_, value)
    value = clamp(tonumber(value) or 1.0, 0.5, 1.8)
    value = math.floor((value * 10) + 0.5) / 10
    AN.DB.settings.uiScale = value
    scaleText:SetText(string.format("%.1fx", value))
    if AN.ApplyUIScale then
      AN:ApplyUIScale(value)
    end
  end)

  self.OptionsPanel = panel

  if Settings and Settings.RegisterCanvasLayoutCategory then
    local category = Settings.RegisterCanvasLayoutCategory(panel, "AmmeNotepad")
    Settings.RegisterAddOnCategory(category)
  elseif InterfaceOptions_AddCategory then
    InterfaceOptions_AddCategory(panel)
  end
end
