local http = require("socket.http")

-- VARIABLES
sysUser = os.getenv( "HOME" )
pathDosbox = "/usr/bin/dosbox"
pathFDD = sysUser.."/.fdd"
urlGameList="http://fanta.56k.es/fdd/fdd_server/archive.lst"
urlGames="http://fanta.56k.es/fdd/fdd_server/archive/"

screenSizeW = love.graphics.getWidth()
screenSizeH = love.graphics.getHeight()

love.window.setTitle( "FDD GUI" )
font = love.graphics.newFont("res/fonts/COMPUTERRobot.ttf", 20)

disquete = {}
disquete.img = love.graphics.newImage("res/imgs/disquete.png")
disquete.width = 200
disquete.height = 200
disquete.x = (screenSizeW/2)-(disquete.width/2)
disquete.y = (screenSizeH/2)-(disquete.height/2)

rkey = {}
rkey.img = love.graphics.newImage("res/imgs/keyboard_key_r.png")
rkey.width = 64
rkey.height = 64
rkey.x = -100
rkey.y = -100

-- FUNCTIONS

function file_check(file_name)
  local f=io.open(file_name, "r")
  if f==nil then print(file_name.." not found. Install it and try again") os.exit() else print(file_name..": OK") end
end

function split_string(s, delimiter)
  result = {};
  for match in (s..delimiter):gmatch("(.-)"..delimiter) do
    table.insert(result, match);
  end
  return result;
end

function check_dependencies()
  file_check(pathDosbox) 
end

function draw_floppyDiskName(floppydisk_name)
  love.graphics.setColor(2550,255,255)
  love.graphics.setFont(font)
  love.graphics.printf(floppydisk_name,335,292,400,left)
end

function download_gameList(url_gameList)
  os.execute("mkdir -p "..pathFDD)
  local body, code = http.request(url_gameList)
  local f = assert(io.open(pathFDD.."/archive.lst", "wb"))
  f:write(body)
  f:close()
end

function download_listGames(file)
  local arr = {}
  local handle  = assert( io.open(file,"r") )
  local value = handle:read("*line")
  while value do
    table.insert( arr, value )
    value = handle:read("*line")
    if value==nil then else gameName = split_string(value, ";") end
    local fi=io.open(pathFDD.."/"..gameName[2], "r")
    if fi==nil then
      print("Downloading game: "..gameName[2])
      local body, code = http.request(urlGames..gameName[2])
      local f = assert(io.open(pathFDD.."/"..gameName[2], "wb"))
      f:write(body)
      f:close()
    else
    end
  end
  handle:close()
  return arr
end

function load_games()
  n=0
  game = {}
  for l in io.lines(pathFDD.."/archive.lst") do
     n=n+1
     g = split_string(l, ";")
     table.insert(game, g[2])
  end
  max=n
  print(n.." games")
end

function next_game()
  if n==max then
    n=1
  else
    n=n+1
  end
  print("ch game: "..game[n])
end


-- FUNCIONES LOVE

function love.load()
  check_dependencies()
  download_gameList(urlGameList)
  load_games()
end

function love.update()
  if love.keyboard.isDown( "r" ) then
    rkey.x, rkey.y = 10,10
  else
    rkey.x, rkey.y = -100,-100
  end

end

function love.draw()
  love.graphics.setBackgroundColor( (170/255), (100/255), (70/255) )
  love.graphics.draw(disquete.img,disquete.x,disquete.y)
  draw_floppyDiskName(game[n])
  love.graphics.draw(rkey.img, rkey.x, rkey.y)
end

function love.keypressed(key)
  if key == "escape" then
    print("bye bye!")
    love.event.quit()
  end

  if key == "r" then
    print("re-check game repository")
    download_listGames(pathFDD.."/archive.lst")
  end

  if key == "right" then
    next_game()
    --download_listGames(pathFDD.."/archive.lst")
  end

  if key == "return" then
    print("run !")
    os.execute("tar xfvz "..pathFDD.."/"..game[n].." -C "..pathFDD.."/")
    os.execute("name=$(echo "..game[n].."|cut -d . -f 1); bash "..pathFDD.."/$name/dos-*")
  end

end
