local Scene    = require "core.modules.scenes"
local WorldMap = Scene:extend()

local LevelDot = require "scenes.worldmap.leveldot"

local menuutils       = require "core.modules.menusystem.widgets.utils"
local CURSOR_PADDING  = 2

function WorldMap:new()
  WorldMap.super.new(self)
  self.assets:addImage("background", "assets/backgrounds/worldmap.png")
  self.banner = game.gui.newBanner("banner.png", "Choose your level")
  self.cursor = {}
  self.cursor.x,      self.cursor.y     = 0, 0
  self.cursor.drawx,  self.cursor.drawy = 0, 0
  self.cursor.canMove = true
  self.cursor.dotAtPoint = nil
  self:addLevels()

  self.assets:addFont("small", "assets/fonts/PixelOperator.ttf", 15)

  self:register()
end

function WorldMap:addLevels()
  self.assets:addImage("leveldot", "assets/sprites/gui/leveldot.png")
  self.leveldots      = {}

  local levels = require "datas.levels"
  for i,v in ipairs(levels) do
    LevelDot(self, v)
  end
end

function WorldMap:registerDot(dot)
  table.insert(self.leveldots, dot)
end

function WorldMap:update(dt)
  if (self.keys["up"].isDown) and (self.cursor.canMove) then
    self.cursor.y       = math.max(0, self.cursor.y - 1)
    self.cursor.canMove = false
  end

  if (self.keys["down"].isDown) and (self.cursor.canMove) then
    self.cursor.y       = math.min(12, self.cursor.y + 1)
    self.cursor.canMove = false
  end

  if (self.keys["right"].isDown) and (self.cursor.canMove) then
    self.cursor.x       = math.min(27, self.cursor.x + 1)
    self.cursor.canMove = false
  end

  if (self.keys["left"].isDown) and (self.cursor.canMove) then
    self.cursor.x       = math.max(0, self.cursor.x - 1)
    self.cursor.canMove = false
  end

  local dx = self.cursor.x - self.cursor.drawx
  local dy = self.cursor.y - self.cursor.drawy
  local dist = (dt*6)

  if math.abs(dx) >= (dist) then
    self.cursor.drawx = self.cursor.drawx + (dist * utils.math.sign(dx))
  else
    self.cursor.drawx = self.cursor.x
  end

  if math.abs(dy) >= (dist) then
    self.cursor.drawy = self.cursor.drawy + (dist * utils.math.sign(dy))
  else
    self.cursor.drawy = self.cursor.y
  end

  if (self.cursor.drawy == self.cursor.y) and (self.cursor.drawx == self.cursor.x) then
    self.cursor.canMove     = true
  end

  if (self.keys["A"].isPressed) and (self.cursor.canMove) then
    if (self.cursor.dotAtPoint ~= nil) then
      self.cursor.dotAtPoint:action()
    end
  end

  self.cursor.dotAtPoint  = self:getDotAtPoint(self.cursor.x, self.cursor.y)
end

function WorldMap:mousemoved(x, y)
  if x >= 16 and y >= 48 then
    local xx = math.floor((x - 16) / 16)
    local yy = math.floor((y - 48) / 16)

    xx = math.max(0, math.min(xx, 27))
    yy = math.max(0, math.min(yy, 12))

    self.cursor.x,      self.cursor.y     = xx, yy
    self.cursor.drawx,  self.cursor.drawy = xx, yy
  end
end

function WorldMap:getDotAtPoint(x, y)
  local dot = nil
  for i,v in ipairs(self.leveldots) do
    if (v.data.x == x) and (v.data.y == y) then
      dot = v
    end
  end

  return dot
end

function WorldMap:draw()
  self.assets:drawImage("background", 0, 0)

  for i,v in ipairs(self.leveldots) do
    v:draw()
  end

  if (self.cursor.dotAtPoint ~= nil) then
    self.cursor.dotAtPoint:drawName()
  end

  love.graphics.draw(self.banner, 0, 8)
  local x, y =  16 + self.cursor.drawx * 16 - CURSOR_PADDING,
                48 + self.cursor.drawy * 16 - CURSOR_PADDING
  local size =  16 + CURSOR_PADDING * 2
  menuutils.drawCursor(x, y, size, size)
end

return WorldMap
