local love = assert( love )
local strings = require( "i18n" )
local settings = require( "settings" )
local t = {}

local currentSetting = false
local mouseOverSetting = false
local cachedSettingVal = nil
local menu = {}
local time = 0
for i, setting in ipairs( settings ) do
  menu[i] = { x = 350, y = 10 + i * 25, w = love.graphics.getWidth() - 410 , h = 25 }
end

local terrain = love.graphics.newMesh( {
    { 0, 0, 0,  0,  0.8, 0.8, 1, 0.4, },
    { 0, 1, 0,  1,  0.8, 0.8, 1, 0.4, },
    { 1, 1, 1,  1,  1, 0, 0, 0.01, },
    { 1, 0, 1,   0,  1, 0, 0, 0.01, },
  })

local function restoreCachedSetting()
  if (not currentSetting) or (cachedSettingVal == nil) then return end
  if settings[currentSetting].set then
    settings[currentSetting]:set(cachedSettingVal)
  else
    settings[currentSetting].val = cachedSettingVal
  end
  cachedSettingVal = nil
end

function t.keypressed( key, code, isRepeat )
  if currentSetting then
    if code == "escape" or code == "return" then
      if code == "escape" then restoreCachedSetting() end
      currentSetting = false
      return
    elseif settings[currentSetting].type == "keybind" then
      settings[currentSetting].val = code
      currentSetting = false
    end
    return
  end

  if code == "return" then
    local slideshow = require( "scenes.slideshow" )
    local main = require( "scenes.main" )
    return slideshow.play( strings.intro, main.play )
  end
end

function t.wheelmoved(x, y)
  if not currentSetting then return end
  local setting = settings[currentSetting]
  if setting.type == "number" then
    if y > 0 then setting.val = setting.val + 1
    elseif y < 0 then setting.val = setting.val - 1
    end
  end
  if setting.type == "list" then
    local list = setting.list
    local inc
    local next
    if y > 0 then 
      inc = 1
      next = 1
    elseif y < 0 then
      inc = -1
      next = #list
    end
    local idx = setting.idx + inc
    if not list[idx] then idx = next end
    setting.idx = idx
    setting:set( idx )
  end
end

function t.mousemoved( x, y )
  for i, button in ipairs( menu ) do
    if 
    x >  button.x and
    y >  button.y and
    y <= button.y + button.h then
      mouseOverSetting = i
      return
    end
  end
  mouseOverSetting = false
end

function t.mousepressed( x, y )
  for i, button in ipairs( menu ) do
    if 
    x >  button.x and
    y >  button.y and
    y <= button.y + button.h then
      restoreCachedSetting()
      currentSetting = i
      if settings[i].get then cachedSettingVal = settings[i]:get()
      else cachedSettingVal = settings[i].val end
      mouseOverSetting = false
    end
  end
end

function t.update( dt )
  time = time + dt
  for i = 1, terrain:getVertexCount() do
    --2: UV coordinate
    local x, y = terrain:getVertexAttribute( i, 1 )
    x = ( x > 0 ) and 0.5 or 0
    y = ( y > 0 ) and 0.5 or 0
    terrain:setVertexAttribute( i, 2, x + 0.01 * time, y + 0.00 * time )
  end
end

do
  local tex = love.graphics.newImage( "tex/terrain.jpg" )
  tex:setWrap( "repeat", "repeat" )
  terrain:setTexture( tex )
end

function t.draw()
  love.graphics.setScissor()
  love.graphics.setColor( 1, 1, 1, 1 )
  love.graphics.draw( terrain, 0, 0, 0, love.graphics.getWidth(), love.graphics.getHeight() )
  love.graphics.setScissor()
  love.graphics.setScissor( 350, 0, love.graphics.getWidth() - 400, love.graphics.getHeight() )

  love.graphics.setColor( 1, 0.1, 0.1, 0.2 )
  if mouseOverSetting then
    love.graphics.rectangle( "fill",
      menu[mouseOverSetting].x,
      menu[mouseOverSetting].y,
      menu[mouseOverSetting].w,
      menu[mouseOverSetting].h )
  end
  love.graphics.setColor( 1, 0.1, 0.1, 0.8 )
  if currentSetting then
    love.graphics.rectangle( "fill",
      menu[currentSetting].x,
      menu[currentSetting].y,
      menu[currentSetting].w,
      menu[currentSetting].h )
  end

  love.graphics.setColor( 1, 1, 1, 1 )
  love.graphics.print( strings.settingsMenuTitle, 360 , 0 )

  for i, setting in ipairs( settings ) do
    local x, y = menu[i].x, menu[i].y
    --love.graphics.rectangle( "line", x, y, love.graphics.getWidth() / 2, 25 )
    love.graphics.print( strings[setting.setting], x + 20, y )
    love.graphics.print( setting.val, x + 250, y )
  end

  if currentSetting then
    love.graphics.print( strings.settingsMenuBackspace, 360, love.graphics.getHeight() - 2 * love.graphics.getFont():getHeight()) 
    if settings[currentSetting].type == "number" or
    settings[currentSetting].type == "list"
    then
      love.graphics.print( strings.settingsMenuScroll, 360, love.graphics.getHeight() - 3 * love.graphics.getFont():getHeight()) 
    end
  else
    love.graphics.print( strings.toContinue, 360, love.graphics.getHeight() - love.graphics.getFont():getHeight()  )
  end

end

return t