local Map = Object:extend()

local maputils = require "scenes.battlesystem.utils"
local TweenManager = require "game.modules.tweenmanager"

local DURATION = 0.66
local OPACITY_MIN = 0
local OPACITY_MAX = 0.75

function Map:new(world, type, terrain)
  self.world = world
  self.assets = self.world.assets
  self.scene  = self.world.scene

  self.datas = {}
  self.datas.type       = type or "city"
  self.datas.terrains   = terrain or maputils.newEmptyMap()

  self.tweens = TweenManager(self)
  self.effectOpacity = OPACITY_MIN
  self:increaseOpacity()

  local zones = require "datas.gamedata.maps.shoot.zones"
  local datas = zones[self.datas.type]
  self.datas.background = datas.background
  self.datas.tiles      = datas.tiles
  self.datas.borders    = datas.borders

  local backpath = "assets/backgrounds/parallax/" .. self.datas.background
  self.assets:addImage("back1", backpath .. "-back.png")
  self.assets:addImage("back2", backpath .. "-fore.png")
  self.assets:addImage("cliff", backpath .. "-cliff.png")
end

-- GET FUNCTIONS
-- Get information from the map

function Map:getTerrain(x, y)
  if self.datas.terrains[y] ~= nil then
    return self.datas.terrains[y][x]
  else
    return nil
  end
end

function Map:isInGrid(x, y)
  return ( self:getTerrain(x, y) ~= nil )
end

function Map:update(dt)
  self.tweens:update(dt)
end

-- OPACITY FUNCTIONS
-- Simple functions to work on opacity

function Map:decreaseOpacity()
  self.tweens:newTween(0, DURATION/2, {effectOpacity = OPACITY_MIN}, "inExpo")
  self.tweens:newTimer(DURATION/2, "increaseOpacity")
end

function Map:increaseOpacity()
  self.tweens:newTween(0, DURATION/2, {effectOpacity = OPACITY_MAX}, "inExpo")
  self.tweens:newTimer(DURATION/2, "decreaseOpacity")
end

function Map:timerResponse(timer)
  if timer == "increaseOpacity" then
    self:increaseOpacity()
  elseif timer == "decreaseOpacity" then
    self:decreaseOpacity()
  end
end

-- DRAW FUNCTIONS
-- Draw the battle map

function Map:draw(activeGrid, effectGrid)
  self:drawBackgrounds()
  self:drawBorders()
  self:drawTerrains(activeGrid)
  if (effectGrid ~= nil) then
    self:drawEffectGrid(effectGrid)
  end
end

function Map:drawBackgrounds()

  local w, _    = core.screen:getDimensions()

  local w2, h2 = self.assets.images["back1"]:getDimensions()
  local imax = math.ceil(w / w2) + 1
  for i=1, imax do
    self.assets.images["back1"]:draw((i-1)*w2, 0, 0, 1, 1)
  end

  local w2, h2  = self.assets.images["back2"]:getDimensions()
  local imax = math.ceil(w / w2) + 1
  for i=1, imax do
    self.assets.images["back2"]:draw((i-1)*w2, maputils.CONST.STARTY-h2, 0, 1, 1)
  end

end

function Map:drawBorders()
  local border = self.datas.borders + 1
  for i=1, 7 do
    self.assets.tileset["borders"]:drawTile(border, (i-1)*80, maputils.CONST.STARTY-10  , 0, 1, 1)
    self.assets.tileset["borders"]:drawTile(border, (i-1)*80, maputils.CONST.STARTY+20*7, 0, 1, 1)
  end
end

function Map:drawTerrains(activeGrid)
  local vl, vhd, vd = 1, .7, .5
  local isActive = (activeGrid ~= nil)


  for i=1, 7 do
    for j= -2, 17 do
      local k = 1 + ((i + j) % 2)

      local terrain = self:getTerrain(j, i)
      local x, y    = maputils.gridToPixel(j, i, false)

      if (terrain ~= nil) then
        if (isActive) then
          if (activeGrid[i][j] == 1) then
            love.graphics.setColor(vl, vl, vl, 1)
          else
            love.graphics.setColor(vhd, vhd, vhd, 1)
          end
        else
          love.graphics.setColor(vl, vl, vl, 1)
        end

        self:drawTile(x, y, terrain, k)
      else
        love.graphics.setColor(vd, vd, vd, 1)
        self:drawTile(x, y, 0, k)
      end
    end
  end

  love.graphics.setColor(1, 1, 1, 1)
end

function Map:drawTile(x, y, type, variant)
  if type == 0 then
    local tiles = self.datas.tiles*2 + variant
    self.assets.tileset["normaltiles"]:drawTile(tiles, x, y)
  else
    self.assets.tileset["sptiles"]:drawTile(type, x, y)
  end
end

function Map:drawEffectGrid(effectGrid)
  for i=1,7 do
    for j=1,17 do
      if (effectGrid[i][j] == 1) then
        local x, y = maputils.gridToPixel(j, i)
        love.graphics.setColor(1, 1, 1, self.effectOpacity)
        self.assets.images["emptytile"]:draw(x, y)
        love.graphics.setColor(1, 1, 1, 1)
      end
    end
  end
end


return Map
