local Scene = require "core.modules.scenes"

local BattleSystem = Scene:extend()

local World         = require "scenes.battlesystem.world"
local MenuSystem    = require "scenes.battlesystem.menus.controller"
local Turns         = require "scenes.battlesystem.controllers"

local VictoryScreen = require "scenes.battlesystem.screens.victory"
local GameOverScreen = require "scenes.battlesystem.screens.gameover"

local TweenManager = require "game.modules.tweenmanager"

function BattleSystem:new(battleData)
  BattleSystem.super.new(self)

  self.assets:batchImport("scenes.battlesystem.assets")

  self:playMusic(battleData.music)

  self:initManagers(battleData)

  self:register()

  self:startBattle()

  self.screen = nil
  self.tweens = TweenManager(self)
end

function BattleSystem:playMusic(music)
  self.assets:setMusic("assets/music/" .. music .. ".mp3")
  self.assets:playMusic()
end

function BattleSystem:initManagers(battleData)
  self.datas = {}
  self.world        = World(self)
  self.menu         = MenuSystem(self)
  self.turns        = Turns(self, battleData)
end

function BattleSystem:startBattle()
  self.turns:startBattle()
end

function BattleSystem:finishBattle()
  self.assets:setMusic("assets/music/victory.mp3")
  self.assets:playMusic()
  self.screen = VictoryScreen(self)
end

function BattleSystem:fleeBattle()
  self.tweens:newTimer(2, "flee")
end

function BattleSystem:timerResponse(name)
  if (name == "flee") then
    --placeholder, pour l'instant on retourne juste au menu
    scenes.debug.menu()
  end
end

function BattleSystem:looseBattle()
  self.screen = GameOverScreen(self)
end

function BattleSystem:haveMenus()
  for k,v in pairs(self.menusystem.menus) do
    return true
  end
  return false
end

function BattleSystem:update(dt)
  self.tweens:update(dt)
  self.world:update(dt)
  self.turns:update(dt)
  if (self.screen ~= nil) then
    self.screen:update(dt)
  end
end

function BattleSystem:draw()
  self.world:draw()
  self.turns:draw()
  if (self.screen ~= nil) then
    self.screen:draw()
  end
end

function BattleSystem:exit()
  self.world:destroy()
  self.battlearena = nil

  collectgarbage()
end

return BattleSystem
