local BaseMap   = require "core.modules.world.maps.parent"
local ShootMap  = BaseMap:extend()

local TILESIZE = 31
local TESTZONE = "forest"

local zoneDatas = require "datas.gamedata.maps.shoot.zones"

function ShootMap:new(world, maptype, mapname)
  ShootMap.super.new(self, world)

  self:setPadding(0, 0, 0, 0)
  self:generateTextures(2, "tunnel")
end

function ShootMap:loadCollisions()
  local w, h = self:getDimensions()
  self.world:newCollision("floor", 0, 0, -16, w, h, 16)
end

function ShootMap:addBlock(x, y, w, h, top, bottom)
  -- Empty Placeholder function
end

function ShootMap:getDimensions()
  return 3000, 100
end

function ShootMap:loadPlayers()
  self.world:addPlayer(16, 50, 0, 1)
end

function ShootMap:loadActors()
  -- Empty Placeholder function
end

function ShootMap:generateTextures(tile, background)

  self.texture = {}
  self.texture.floor  = self:generateFloor(tile)
  self.texture.border = love.graphics.newImage("assets/backgrounds/borders.png")
  self.quads = {}
  local w, h = self.texture.border:getDimensions()
  self.quads.borders = love.graphics.newQuad(0, tile*10, 80, 10, w, h)

  self:addParallax(background)
end

function ShootMap:generateFloor(tile)
  local canvas = love.graphics.newCanvas(31*16, 100)
  local tile    = tile or 1

  local tileTexture = love.graphics.newImage("assets/backgrounds/normaltile.png")
  local tileQuad    = {}
  local w, h  = tileTexture:getDimensions()
  tileQuad[1] = love.graphics.newQuad( 0, tile*24, 40, 24, w, h)
  tileQuad[2] = love.graphics.newQuad(40, tile*24, 40, 24, w, h)

  love.graphics.setCanvas( canvas )

  for i=1, 5 do
    for j=0, 18 do
      local tiley = (i-1)*20 - 4
      local tilex = (j-2)*31 + (i-1)*10
      local variant = 1 + ((i + j) % 2)
      love.graphics.draw(tileTexture,   tileQuad[variant], tilex, tiley)
    end
  end

  love.graphics.setCanvas( )

  local imagedata = canvas:newImageData()
  local texture   = love.graphics.newImage( imagedata )
  imagedata:release()
  canvas:release()
  return texture
end

function ShootMap:draw()
  for i=1, 10 do
    --love.graphics.draw(self.texture.floor, ((i-1)*31*16), 0)
  end
end

function ShootMap:addParallax(filename)
  -- Empty Placeholder function
  local filename    = filename or "forest"
  local backfolder  = "assets/backgrounds/parallax/"
  filename    = backfolder .. filename
  self.texture.back1  = love.graphics.newImage(filename .. "-back.png")
  self.texture.back2  = love.graphics.newImage(filename .. "-fore.png")
  self.texture.cliff  = love.graphics.newImage(filename .. "-cliff.png")
end

function ShootMap:drawParallax(x, y, w, h)

  self:drawBackground(x, y, w, h)
  self:drawForeground(x, y + 10, w, h)
  local w2, _ =  self.texture.floor:getDimensions()
  for i=1, 2 do
    local x2 = x % w2
    love.graphics.draw(self.texture.floor, ((i-1)*31*16)-x2, -y)
  end

  self:drawBorder(x, y + 10)
  self:drawBorder(x, y - 100)
  self:drawCliff(x,  y - 110, w, h)
end

function ShootMap:drawBackground(x, y, w, h)
  local w2, h2 = self.texture.back1:getDimensions()
  local imax = math.ceil(w / w2) + 1
  for i=1, imax do
    local x1 = (x / 5) % w2
    love.graphics.draw(self.texture.back1, (i-1)*w2 - x1, 0, 0, 1, 1, 0, 0)
  end
end

function ShootMap:drawForeground(x, y, w, h)
  local w2, h2 = self.texture.back2:getDimensions()
  local imax = math.ceil(w / w2) + 1
  for i=1, imax do
    local x1 = (x / 2) % w2
    love.graphics.draw(self.texture.back2, (i-1)*w2 - x1, -y, 0, 1, 1, 0, h2)
  end
end

function ShootMap:drawBorder(x, y)
  for i=1, 7 do
    local x2 = x % 80
    love.graphics.draw(self.texture.border, self.quads.borders, (i-1)*80 - x2, -y, 0, 1, 1)
  end
end

function ShootMap:drawCliff(x, y, w, h)
  local w2, h2 = self.texture.cliff:getDimensions()
  local imax = math.ceil(w / w2) + 1
  for i=1, imax do
    local x1 = (x) % w2
    love.graphics.draw(self.texture.cliff, (i-1)*w2 - x1, -y, 0, 1, 1, 0, 0)
  end
end


return ShootMap
