-- Notes: a popup notepad for grandMA3.
-- Notes are stored in the show file (GlobalVars), so they travel with the show.
-- Text autosaves every second while open. Save the show to keep it on disk.
--   Plugin "Notes"           open the notepad
--   Plugin "Notes" "clear"   wipe the notes

local pluginName  = select(1, ...)
local signalTable = select(3, ...)
local myHandle    = select(4, ...)

local VAR = "NotesPlugin_Text"

local function LoadNotes()
  return GetVar(GlobalVars(), VAR) or ""
end

local function SaveNotes(text)
  SetVar(GlobalVars(), VAR, text or "")
end

local function BuildWindow(display)
  local win = display.ScreenOverlay:Append("BaseInput")
  win.Name = "NotesWindow"
  win.W, win.H = 900, 650
  win.Columns, win.Rows = 1, 2
  win[1][1].SizePolicy, win[1][1].Size = "Fixed", 60
  win[1][2].SizePolicy = "Stretch"
  win.AutoClose, win.CloseOnEscape = "No", "Yes"

  local title = win:Append("TitleBar")
  title.Anchors, title.Columns, title.Rows = "0,0", 2, 1
  title[2][2].SizePolicy, title[2][2].Size = "Fixed", 50
  local caption = title:Append("TitleButton")
  caption.Anchors, caption.Text = "0,0", "Notes"
  title:Append("CloseButton").Anchors = "1,0"

  local frame = win:Append("DialogFrame")
  frame.Anchors, frame.W, frame.H = "0,1", "100%", "100%"
  frame.Columns, frame.Rows = 1, 2
  frame[1][1].SizePolicy = "Stretch"
  frame[1][2].SizePolicy, frame[1][2].Size = "Fixed", 70

  local edit = frame:Append("TextEdit")
  edit.Anchors = "0,0"
  edit.W, edit.H = "100%", "100%"
  edit.Focus = "InitialFocus"   -- TextEdit defaults to Focus=Never (not clickable)
  edit.Padding = "12,12,12,12"  -- space between the field's edge and the text
  edit.Content = LoadNotes()

  local buttons = frame:Append("UILayoutGrid")
  buttons.Anchors, buttons.Columns, buttons.Rows = "0,1", 2, 1

  local clear = buttons:Append("Button")
  clear.Anchors, clear.Text = "0,0", "Clear"
  clear.PluginComponent, clear.Clicked = myHandle, "NotesClear"

  local close = buttons:Append("Button")
  close.Anchors, close.Text = "1,0", "Save & Close"
  close.PluginComponent, close.Clicked = myHandle, "NotesClose"

  signalTable.NotesClear = function()
    if Confirm("Clear notes", "Delete all notes?") then
      edit.Content = ""
      SaveNotes("")
    end
  end

  signalTable.NotesClose = function()
    SaveNotes(edit.Content)
    win:Parent():Remove(win:Index())
  end

  return win, edit
end

local function Main(displayHandle, argument)
  local arg = argument and argument:lower() or ""

  if arg == "clear" then
    SaveNotes("")
    Printf("Notes cleared")
    return
  end

  if arg == "selftest" then
    local before = LoadNotes()
    SaveNotes("line 1\nline 2 with \"quotes\" and 100%")
    local back = LoadNotes()
    SaveNotes(before)
    if back ~= "line 1\nline 2 with \"quotes\" and 100%" then
      error("round trip failed, got: " .. tostring(back))
    end
    local win, edit = BuildWindow(GetFocusDisplay())
    local shown, focus = edit.Content, edit.Focus
    win:Parent():Remove(win:Index())
    if shown ~= before then error("window content mismatch") end
    if focus ~= "InitialFocus" then error("focus not set: " .. tostring(focus)) end
    return
  end

  local win, edit = BuildWindow(GetFocusDisplay())
  -- TextEdit has no change signal, so autosave once a second while open.
  -- This also covers closing with Escape or the X button.
  local last = edit.Content
  while not WaitObjectDelete(win, 1) do
    if not IsObjectValid(edit) then break end
    local now = edit.Content
    if now ~= last then
      SaveNotes(now)
      last = now
    end
  end
end

return Main
