-- On charge le module de scene et créer l'objet de scene des niveaux
local Scene       = require "core.modules.scenes"
local Level       = Scene:extend()

-- Chargement des controllers
local World   = require "scenes.levels.world"
local Camera = require "scenes.levels.camera"
local PlayerManager = require "scenes.levels.players"

local leveldatas  = require "datas.levels"

-- INIT FUNCTIONS
-- Initialize and launch the level

function Level:new(levelID, missionID)
  self:reset()
  Level.super.new(self)
  self:loadSounds()
  self:loadSprites()
  previousMenu = previous
  self:loadMission(levelID, missionID)

  self:register()
end

function Level:reset()
  self.datas        = {}

  self.pause        = false
  self.map          = nil
  self.backcolor    = {0, 0, 0}

end

function Level:loadMission(levelID, missionID)
  local datas, missiondatas
  self.datas = require("datas.levels." .. levelID)

  self.world = World(self, self.datas.mapfile)
  self.world:load()
  self.assets:setMusic("assets/music/" .. self.datas.music)

  self:launchMission()
end

function Level:loadSounds()
  self.assets:newSFX("jump", "assets/sfx/jump.wav")
  self.assets:newSFX("break", "assets/sfx/break.wav")
  self.assets:newSFX("collectcoin", "assets/sfx/collectcoin.wav")
end

function Level:loadSprites()
  self.assets:addSprite("coin", "assets/sprites/items/coin")
  self.assets:addSprite("coin5", "assets/sprites/items/coin5")
  self.assets:addSprite("coin10", "assets/sprites/items/coin10")

  self.assets:addTileset("weapon",  "assets/sprites/weapon")

  self.assets:addSprite("sparkle",  "assets/sprites/gfx/sparkle")
  self.assets:addSprite("poof",  "assets/sprites/gfx/poof")

  local pigfolder = "assets/sprites/pigs/"
  self.assets:addSprite("cochon",  pigfolder .. "cochon")
  self.assets:addSprite("bling-bling",  pigfolder .. "bling-bling")

  self.assets:addTileset("weapon",  "assets/sprites/weapon")
  self.assets:addTileset("block",  "assets/sprites/items/block")

  self.assets:addImage("debris", "assets/sprites/debris.png")

  self.assets:addTileset("lifeicons",  "assets/sprites/pigs/lifeicons")

  self.assets:addImageFont("small", "assets/fonts/small", 1)
  self.assets:addImageFont("medium", "assets/fonts/medium", 1)
end

function Level:launchMission()
  self.playermanager = PlayerManager(self)
  --self:resetSpawnAndEntities()
  self.assets:silence()
  self.assets:playMusic()

  self.playermanager:addPlayer(1)
  self.playermanager:spawnPlayer(1)

  self.camera = Camera(self, self.playermanager.startx, self.playermanager.starty)
end


-- UPDATE FUNCTIONS
-- Update the level

function Level:update(dt)
  if (self.pause == false) then
    self.playermanager:update(dt)
    self.world:update(dt)
    self.camera:update(dt)
  end

  if self.keys["start"].isPressed then
    self.pause = (self.pause == false)
  end
end

-- DRAW FUNCTIONS
-- draw level elements

function Level:draw(dt)
  utils.graphics.resetColor()

  self.camera:floorCoord()
  self.world:draw()

  if (self.pause == false) then
    self.playermanager:drawHUD()
  end
end

function Level:exit()
  self.controller:reset()
end

return Level
