In-progress browser for a directory of text files
utf8 = require 'utf8'
json = require 'json'

require 'app'
require 'test'
require 'live'

require 'keychord'
require 'button'

-- delegate most business logic to a layer that can be reused by other projects
require 'edit'
Editor_state = {}

-- called both in tests and real run
function App.initialize_globals()
  Supported_versions = {'11.5', '11.4', '11.3', '11.2', '11.1', '11.0', '12.0'}  -- put the recommended version first
  Error_message = ''
  Error_count = 0

  -- tests currently mostly clear their own state

  Load_time_error = nil
  Run_time_error = nil

  -- This app has some limited capability to juggle multiple fonts. Mostly we
  -- want to keep everything at a single font size at any time. However,
  -- there's also a notion of a "heads-up display" that's overlaid atop the
  -- app, whose font size is even more fixed.
  HUD_font_height = 20
  HUD_line_height = math.floor(HUD_font_height*1.3)
  Menu_bar_height = 5 + HUD_line_height + 5

  -- place to store global state
  HUD = {}

  -- commonly used text objects
  HUD_font = nil
  HUD_text_cache = {}  -- fixed font, never recomputed
  Text_cache = {}  -- recompute when default font changes

  -- blinking cursor
  Cursor_time = 0

  -- for hysteresis in a few places
  Current_time = 0
  Last_focus_time = 0  -- https://love2d.org/forums/viewtopic.php?p=249700
  Last_resize_time = 0
end

-- called only for real run
function App.initialize(arg, unfiltered_arg)
  Arg, Unfiltered_arg = arg, unfiltered_arg
  love.keyboard.setKeyRepeat(true)

  Editor_state = nil  -- not used outside editor tests

  love.graphics.setBackgroundColor(1,1,1)
  HUD_font = love.graphics.newFont(HUD_font_height)

  if love.filesystem.getInfo('config') then
    load_settings()
  else
    initialize_default_settings()
  end



  -- app-specific stuff
  -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
  love.window.setTitle('pothi.love')



  if on.initialize then on.initialize(arg, unfiltered_arg) end

  if rawget(_G, 'jit') then
    jit.off()
    jit.flush()
  end

  check_love_version()
end

function check_love_version()
  -- we'll reuse error mode on load for an initial version check
  if array.find(Supported_versions, Version) == nil then
    while true do
      love.graphics.origin()
      love.graphics.clear(0,0,1)
      love.graphics.setColor(1,1,1)
      love.graphics.printf(("This app hasn't been tested with LÖVE version %s; please switch to version %s if you run into issues. Press any key to continue."):format(Version, Supported_versions[1]), 40,40, 400)
      love.graphics.present()
      if love.event then
        love.event.pump()
        for name, a,b,c,d,e,f in love.event.poll() do
          if name == 'quit' then
            if not love.quit or not love.quit() then
              os.exit(1)
            end
          elseif name == 'keypressed' then
            return
          end
        end
      end
      love.timer.sleep(0.01)
    end
  end
end

function print_and_log(s)
  print(s)
  log(3, s)
end

function love.quit()
  if on.quit then on.quit() end
  love.filesystem.write('config', json.encode(settings()))
end

function restart()
  if on.quit then on.quit() end
  love.filesystem.write('config', json.encode(settings()))
  load_settings()
  if on.initialize then on.initialize(Arg, Unfiltered_arg) end
end

function settings()
  local x, y, displayindex = App.screen.position()
  return {
    x=x, y=y, displayindex=displayindex,
    width=App.screen.width, height=App.screen.height,
    app = on.save_settings and on.save_settings(),
  }
end

function load_settings()
  local settings = json.decode(love.filesystem.read('config'))
  -- set up desired window dimensions and make window resizable
  _, _, App.screen.flags = App.screen.size()
  App.screen.flags.resizable = true
  App.screen.width, App.screen.height = settings.width, settings.height
  App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
  set_window_position_from_settings(settings)
  if on.load_settings then on.load_settings(settings.app or {}) end
end

function set_window_position_from_settings(settings)
  local os = love.system.getOS()
  if os == 'Linux' then
    -- love.window.setPosition doesn't quite seem to do what is asked of it on Linux.
    App.screen.move(settings.x, settings.y-37, settings.displayindex)
  else
    App.screen.move(settings.x, settings.y, settings.displayindex)
  end
end

function initialize_default_settings()
  local font_height = 20
  love.graphics.setFont(love.graphics.newFont(font_height))
  initialize_window_geometry()
end

function initialize_window_geometry()
  -- Initialize window width/height and make window resizable.
  --
  -- I get tempted to have opinions about window dimensions here, but they're
  -- non-portable:
  --  - maximizing doesn't work on mobile and messes things up
  --  - maximizing keeps the title bar on screen in Linux, but off screen on
  --    Windows. And there's no way to get the height of the title bar.
  -- It seems more robust to just follow LÖVE's default window size until
  -- someone overrides it.
  App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
  App.screen.flags.resizable = true
  App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
end

function App.resize(w,h)
--?   print(("Window resized to width: %d and height: %d."):format(w, h))
  App.screen.width, App.screen.height = w, h
  Last_resize_time = Current_time
  if on.resize then on.resize(w,h) end
end

function App.filedropped(file)
  if on.file_drop then on.file_drop(file) end
end

function App.draw()
  HUD.button_handlers = {}
  if on.draw then on.draw() end
end

function App.update(dt)
  Current_time = Current_time + dt
  -- some hysteresis while resizing
  if Current_time < Last_resize_time + 0.1 then
    return
  end
  Cursor_time = Cursor_time + dt
  live.update(dt)
  if on.update then on.update(dt) end
end

function App.mousepressed(x,y, mouse_button, is_touch, presses)
  Cursor_time = 0  -- ensure cursor is visible immediately after it moves
  love.keyboard.setTextInput(true)  -- bring up keyboard on touch screen
  if on.mouse_press then on.mouse_press(x,y, mouse_button, is_touch, presses) end
end

function App.mousereleased(x,y, mouse_button, is_touch, presses)
  Cursor_time = 0  -- ensure cursor is visible immediately after it moves
  if on.mouse_release then on.mouse_release(x,y, mouse_button, is_touch, presses) end
end

function App.mousemoved(x,y, dx,dy, istouch)
  if on.mouse_move then on.mouse_move(x,y, dx,dy, istouch) end
end

function App.wheelmoved(dx,dy)
  Cursor_time = 0  -- ensure cursor is visible immediately after it moves
  if on.mouse_wheel_move then on.mouse_wheel_move(dx,dy) end
end

function App.mousefocus(in_focus)
  Cursor_time = 0  -- ensure cursor is visible immediately after it moves
  if on.mouse_focus then on.mouse_focus(in_focus) end
end

function App.focus(in_focus)
  if in_focus then
    Last_focus_time = Current_time
  end
  if in_focus then
    love.graphics.setBackgroundColor(1,1,1)
  else
    love.graphics.setBackgroundColor(0.8,0.8,0.8)
  end
  if on.focus then on.focus(in_focus) end
end

-- App.keypressed is defined in keychord.lua

function App.keychord_press(chord, key, scancode, is_repeat)
  -- ignore events for some time after window in focus (mostly alt-tab)
  if Current_time < Last_focus_time + 0.01 then
    return
  end
  Cursor_time = 0  -- ensure cursor is visible immediately after it moves
  if on.keychord_press then on.keychord_press(chord, key, scancode, is_repeat) end
end

function App.textinput(t)
  -- ignore events for some time after window in focus (mostly alt-tab)
  if Current_time < Last_focus_time + 0.01 then
    return
  end
  Cursor_time = 0  -- ensure cursor is visible immediately after it moves
  if on.text_input then on.text_input(t) end
end

function App.keyreleased(key, scancode)
  -- ignore events for some time after window in focus (mostly alt-tab)
  if Current_time < Last_focus_time + 0.01 then
    return
  end
  Cursor_time = 0  -- ensure cursor is visible immediately after it moves
  if on.key_release then on.key_release(key, scancode) end
end

-- use this sparingly
function to_hud_text(s)
  if HUD_text_cache[s] == nil then
    HUD_text_cache[s] = love.graphics.newText(HUD_font, s)
  end
  return HUD_text_cache[s]
end

-- plumb all other handlers through to on.*
for handler_name in pairs(love.handlers) do
  if App[handler_name] == nil then
    App[handler_name] = function(...)
      if on[handler_name] then on[handler_name](...) end
    end
  end
end