local maputils = {}

maputils.CONST = {}

maputils.CONST.STARTX = -8
maputils.CONST.STARTY = 90


function maputils.newEmptyMap()
  return {
    {00,00,00,00,00,00,00,00,00,00,00,00},
    {00,00,00,00,00,00,00,00,00,00,00,00},
    {00,00,00,00,00,00,00,00,00,00,00,00},
    {00,00,00,00,00,00,00,00,00,00,00,00},
    {00,00,00,00,00,00,00,00,00,00,00,00},
    {00,00,00,00,00,00,00,00,00,00,00,00},
    {00,00,00,00,00,00,00,00,00,00,00,00},
  }
end

function maputils.newFullMap()
  return {
    {01,01,01,01,01,01,01,01,01,01,01,01},
    {01,01,01,01,01,01,01,01,01,01,01,01},
    {01,01,01,01,01,01,01,01,01,01,01,01},
    {01,01,01,01,01,01,01,01,01,01,01,01},
    {01,01,01,01,01,01,01,01,01,01,01,01},
    {01,01,01,01,01,01,01,01,01,01,01,01},
    {01,01,01,01,01,01,01,01,01,01,01,01},
  }
end

function maputils.isInMask(x, y, ox, oy, shape, size, direction)
  local direction = direction or 1
  local shape = shape or "point"
  if shape == "point" then
    return ((x == ox) and (y == oy))
  elseif shape == "square" then
    local x1 = ox - math.floor(size/2)
    local x2 = ox + math.ceil(size/2)

    local y1 = oy - math.floor(size/2)
    local y2 = oy + math.ceil(size/2)
    return ((x >= x1) and (x <= x2) and (y >= y1) and (y <= y2))
  elseif shape == "circle" then
    local lenght = utils.math.pointDistance(x, y, ox, oy)
    return (lenght <= size)
  elseif shape == "fullheight" then
    local x2 = ox + (size*direction)
    return ((x >= ox) and (x <= x2))
  elseif shape == "fullwidth" then
    local y2 = oy + (size*direction)
    return ((y >= oy) and (y <= y2))
  elseif shape == "line" then
    local x2 = ox + (size*direction)
    return ((y == oy) and (x >= ox) and (x <= x2))
  elseif shape == "column" then
    local y2 = oy + (size*direction)
    return ((x == ox) and (y >= oy) and (y <= y2))
  elseif shape == "everything" then
    return true
  else
    if shape == nil then
      shape = "nil"
    end
    core.debug:warning("maputils", "shape " .. shape .. " doesn't exist")
  end
end

function maputils.maskToMap(ox, oy, shape, size, direction)
  local map = maputils.newEmptyMap()

  for i, line in ipairs(map) do
    for j, case in ipairs(line) do
      local isInMask = maputils.isInMask(j, i, ox, oy, shape, size, direction)
      if (isInMask) then
        map[i][j] = 1
      else
        map[i][j] = 0
      end
    end
  end

  return map
end

function maputils.gridToPixel(x, y, center)
  local pixelx, pixely
  local center = center or false
  local x, y = x, y

  if (center) then
    x = x + .5
    y = y + .5
  end

  pixelx = maputils.CONST.STARTX + ((x-1) * 31) + ((y-1) * 10)
  pixely = maputils.CONST.STARTY + ((y-1) * 20)

  return math.floor(pixelx), math.floor(pixely)
end

function maputils.sortBattlers(a, b)
  local astats = a.actor:getStats()
  local bstats = b.actor:getStats()
  local aspeed = astats.speed / 1.5 * a.number
  local bspeed = bstats.speed / 1.5 * b.number


  if (aspeed == bspeed) then
    if (a.actor.isHero == b.actor.isHero) then
      return (a.actor.id > b.actor.id)
    else
      return a.actor.isHero
    end
  else
    return (aspeed > bspeed)
  end
end

return maputils
