--UI for selecting a saved game.
local love = love
local _Update = love.update
local _Draw = love.draw
local _MousePressed = love.mousepressed
local _Resize = love.resize
local _KeyPressed = love.keypressed

local selectedGame = 1
local scrollOffset = 1
local deleteGame = false
local gameList = assert( love.filesystem.getDirectoryItems( "demos" ) )
local mouseX, mouseY = 0, 0
local w, h = love.graphics.getDimensions()
local canvas = love.graphics.newCanvas( w, h * (1 + #gameList) / 10.0 )
local font = love.graphics.newFont( 36 )
local OnClick

local function DeleteLeft()
  return w * 0.75 + 15 + 6
end

local function DeleteRight()
  return DeleteLeft() + 45
end

local function DrawSelection( )
  love.graphics.rectangle( "fill", 15, selectedGame * h / 10 )
end

local function PopulateGameList()
  scrollOffset = 0
  gameList = assert( love.filesystem.getDirectoryItems( "demos" ) )

  love.graphics.setCanvas( canvas )
  love.graphics.clear()
  love.graphics.setColor( 0.2, 0.2, 0.2, 0.5 )
  love.graphics.setLineWidth( 3 )

  for i, name in ipairs( gameList ) do
    local x, y = 15, i * h / 10

    love.graphics.printf( gameList[i], font, x + 10, y, 0.75 * w, "left" )
    love.graphics.rectangle( "line", x, y, w * 0.75, h / 10 - 12, 10, 10 )
    love.graphics.rectangle( "line", DeleteLeft(), y, h / 10 - 12, h / 10 - 12, 10, 10 )
  end

  love.graphics.setCanvas()
end


love.resize = function( newWidth, newHeight )
  _Resize(newWidth, newHeight )
  w, h = love.graphics.getDimensions()
  canvas = love.graphics.newCanvas()
  return PopulateGameList()
end

love.update = function() end

love.draw = function() 

  love.graphics.draw( canvas, 0, -scrollOffset * h / 10 )
  love.graphics.setColor( 1.0, 1.0, 1.0, 0.5 )
  if deleteGame then 
    love.graphics.rectangle( "fill", DeleteLeft(), ( selectedGame - scrollOffset ) * h / 10, h / 10 - 12, h / 10 - 12, 10, 10 )
  else
    love.graphics.rectangle( "fill", 15, ( selectedGame - scrollOffset ) * h / 10, w * 0.75, h / 10 - 12, 10, 10 )
  end


  love.graphics.setColor( 1.0, 1.0, 1.0, 1.0 )
  love.graphics.rectangle( "fill", 0, 0, w, h / 10 - 6 )
  love.graphics.setColor( 91 / 255, 206 / 255, 250 / 255 )
  love.graphics.print( "DEMOS "..( gameList[selectedGame] or selectedGame ) )
end

love.mousemoved = function( x, y, dx, dy, istouch) 
  deleteGame = ( x > DeleteLeft() ) and ( x < DeleteRight() )
  selectedGame = scrollOffset + math.floor( 10 * y / h )
end

love.keypressed = function( key, code, isRepeat )
  if code == "return" then
    return OnClick()
  end
  
  if code == "down" then 

    if selectedGame < #gameList then
      if selectedGame > 8 then
        scrollOffset = scrollOffset + 1
      end
      selectedGame = selectedGame + 1
    end
    return
  end
  if key == "up" then 
    if selectedGame > 8 then
      scrollOffset = scrollOffset - 1
    end
    if selectedGame > 1 then
      selectedGame = selectedGame - 1
    end
    return
  end
  if key == "right" or key == "left" then deleteGame = not( deleteGame ) end
end

OnClick = function( x, y, button, istouch, presses )

  if not selectedGame then return end
  if #gameList < 1 then return end

  if deleteGame then
    love.filesystem.remove( "demos/"..gameList[selectedGame] )
    return PopulateGameList()
  end

  --Restore main state.
  love.update = _Update
  love.draw = _Draw
  love.mousepressed = _MousePressed
  love.keypressed = _KeyPressed
  love.resize = _Resize
  love.mousemoved = nil
  return "demos/"..gameList[selectedGame]

end

PopulateGameList()


return OnClick