local state = require("modules.states.playstate")

local myMath = require("modules.math")
local myTypes = require("modules.types")
local files = require("modules.files")
local json = require("modules.json")
local logging = require("modules.logging")

local songs = json.parse(files.read_file("songs.json"))

local curSong = "dad-battle"
local curDiffList = {"erect"}
local curDiff = "erect"

local curState = nil
local stateLoaded = false

local pressed = {false,false,false}

-- for index, song in next, songs do
--   curSong = index
--   curDiff = song[1]
--   break
-- end

function love.update(dt)
  if curState and curState.loaded then
    if stateLoaded then
      curState.update(dt)
    else
      stateLoaded = true -- skipping this update frame
    end
  elseif not curState then
    if love.keyboard.isDown("return") then
      curState = state(curSong, curDiff)
      curState.load()
    elseif love.keyboard.isDown("down") and not pressed[1] then
      curSong = next(songs, curSong)
      if not curSong then
        curSong = next(songs)
        curDiffList = songs[curSong]
      else
        curDiffList = songs[curSong]
      end
      curDiff = curDiffList[1]
      pressed[1] = true
    elseif love.keyboard.isDown("right") and not pressed[2] then
      local curDiffNum = 0
      for index, diff in next, curDiffList do
        if diff == curDiff then
          curDiffNum = index + 1
        end
      end
      curDiff = curDiffList[curDiffNum] or curDiff
      pressed[2] = true
    elseif love.keyboard.isDown("left") and not pressed[3] then
      local curDiffNum = 0
      for index, diff in next, curDiffList do
        if diff == curDiff then
          curDiffNum = index - 1
        end
      end
      curDiff = curDiffList[curDiffNum] or curDiff
      pressed[3] = true
    elseif not love.keyboard.isDown("down") then
      pressed[1] = false
    elseif not love.keyboard.isDown("right") then
      pressed[2] = false
    elseif not love.keyboard.isDown("left") then
      pressed[3] = false
    end
  end
end

function love.draw()
  if curState and stateLoaded then
    curState.draw()
  else
    love.graphics.print(string.format("Song: %s, Difficulty: %s, List: %s", curSong, curDiff, logging.dump(curDiffList)), x, y, r, sx, sy, ox, oy, kx, ky)
  end
end
