--UI for selecting a saved game.
local love = love
local loadGame = {}
local _Update
local _Draw
local _MousePressed
local _MouseMoved
local _Resize
local _KeyPressed

local function RestoreMainState()
  love.update = _Update
  love.draw = _Draw
  love.mousepressed = _MousePressed
  love.mousemoved = _MouseMoved
  love.keypressed = _KeyPressed
  love.resize = _Resize
  love.mousemoved = nil
  love.mouse.setRelativeMode( true )
end

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 deleteIcon = love.graphics.newImage( "sitelenpona/ala.png" )
local playIcon = love.graphics.newImage( "sitelenpona/li.png" )
local OnClick


local function DeleteLeft()
  return love.graphics.getWidth() * 0.75 + 15 + 6
end

local function DeleteRight()
  return DeleteLeft() + h / 10 - 12
end

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

local function PopulateGameList()
  scrollOffset = 0
  gameList = assert( love.filesystem.getDirectoryItems( "demos" ) )
  
  local w, h = love.graphics.getDimensions()

  canvas = love.graphics.newCanvas( w, h * (1 + #gameList) / 10.0 )
  love.graphics.setCanvas( canvas )
  love.graphics.clear()
  love.graphics.setColor( 1, 1, 1, 1 )
  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.draw( deleteIcon, DeleteLeft() - 8, y - 8, 0, 0.25, 0.25 )
  end

  love.graphics.setCanvas()
end


local resizeLoadMenu = function( newWidth, newHeight )
  _Resize(newWidth, newHeight )
  w, h = love.graphics.getDimensions()
  canvas = love.graphics.newCanvas( w, h * (1 + #gameList) / 10.0 )
  return PopulateGameList()
end

local updateLoadMenu = function() end

local drawLoadGameMenu = function() 
  local w, h = love.graphics.getDimensions()
  love.graphics.setColor( 1, 1, 1, 1 )
  if options["high contrast"].value then
  love.graphics.setColor( 0,0,0, 1 )
  end
  love.graphics.draw( canvas, 0, 15 - scrollOffset * h / 10 )
  
  
  
  love.graphics.setColor( 1.0, 1.0, 1.0, 1.0 )
  love.graphics.rectangle( "fill", 0, 0, w, 10 + h / 10 - 6 )
  love.graphics.setColor( 91 / 255, 206 / 255, 250 / 255 )--
  love.graphics.print( "BACK")
  
  if not selectedGame then return end
  love.graphics.setColor( 1, 1, 1, 0.4 )
  if deleteGame then 
    love.graphics.rectangle( "fill",
      DeleteLeft(), 15 + ( selectedGame - scrollOffset ) * h / 10,
      48, 48,
      10, 10 )
  else
    love.graphics.rectangle( "fill", 15, 15 + ( selectedGame - scrollOffset ) * h / 10, w * 0.75, 
      --h / 10 - 12, 10, 10 )
      48, 10, 10)
  end


end

local mouseMoved = function( x, y, dx, dy, istouch) 
  deleteGame = ( x > DeleteLeft() )
  selectedGame = math.max( math.min( #gameList, scrollOffset + math.floor( 10 * ( y - 15 ) / h ) ), 1)
  if y < 4 + h / 10 then selectedGame = nil end
end

local KeyPress = function( key, code, isRepeat )
  if code == "return" then
    return OnClick()
  end
  
  if not selectedGame then selectedGame = 1 end

  if code == "down" then 

    if selectedGame < #gameList then
      if selectedGame > 8 then
        scrollOffset = scrollOffset + 1
      end
      selectedGame = selectedGame + 1
    end
    return
  end
  if code == "up" then 
    if selectedGame > 9 then
      scrollOffset = scrollOffset - 1
    end
    if selectedGame > 1 then
      selectedGame = selectedGame - 1
    end
    return
  end
  if code == "left" then deleteGame = false end
  if code == "right" then deleteGame = true end
  if code == "escape" or code == "backspace" then return RestoreMainState() end
end


OnClick = function( x, y, button, istouch, presses )
  if y and y < 4 + h / 10 then return RestoreMainState() end
  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.
  RestoreMainState()
  return "demos/"..gameList[selectedGame]

end


local wheelMoved = function(x, y)
  --scrollOffset = math.max( 0, math.min( #gameList, scrollOffset - y * 0.1 ) )
end

local function LoadGameMenu()
  _Update = love.update
  _Draw = love.draw
  _MousePressed = love.mousepressed
  _MouseMoved = love.mousemoved
  _Resize = love.resize
  _KeyPressed = love.keypressed
  love.mouse.setRelativeMode( false )
  
  love.wheelmoved = wheelMoved
  love.mousemoved = mouseMoved
  love.draw = drawLoadGameMenu
  love.update = updateLoadMenu
  love.resize = resizeLoadMenu
  PopulateGameList()

end

return { OnClick = OnClick, KeyPress = KeyPress, LoadGameMenu = LoadGameMenu }