local VictoryScreen = Object:extend()

local TweenManager = require "game.modules.tweenmanager"

local BATTLECOMPLETE_START = 2
local BATTLECOMPLETE_STOP  = 4

local gui         = require "game.modules.gui"

local charutils   = require "game.utils.characters"

local tw, th = 128, 32

function VictoryScreen:new(scene)
  self.scene  = scene
  self.assets = scene.assets
  self.turnSystem = scene.turns

  self:setVariables()

  self.continueBox    = gui.newTextBox("assets/gui/dialogbox.png", tw, th)

  self.tweens = TweenManager(self)
  self:prepareAnimation()
end

function VictoryScreen:setVariables()
  -- Vignette Opacity
  self.vignetteOpacity  = 0

  -- Battle FInished Label
  self.labelOpacity     = 0
  local width, height = core.screen:getDimensions()
  self.labelX           = width/2

  -- Infobox
  self.tbSize     = 0.6
  self.tbOpacity  = 0

  self.continue = false
  self.continueValue = -1
end

function VictoryScreen:prepareAnimation()
  -- Vignette
  self.tweens:newTween(0, 0.6, {vignetteOpacity=0.75}, 'inExpo')

  -- Label
  self.tweens:newTween(0, 0.6, {labelOpacity=1}, 'inExpo')
  self.tweens:newTween(0.9, 0.4, {labelX=4}, 'inExpo')

  -- Infobox
  self.tweens:newTween(1.4, 0.4, {tbSize=1, tbOpacity=1}, 'inExpo')

  self.tweens:newSwitch(1.8, {"continue"})
end

function VictoryScreen:update(dt)
  self.tweens:update(dt)

  if (self.continue) then
    local keys =  self.scene:getKeys(1)
    if (keys["left"].isPressed) then
      self.continueValue = -1
    end
    if (keys["right"].isPressed) then
      self.continueValue = 1
    end
    if (keys["A"].isPressed) then
      --placeholder, pour l'instant on retourne juste au menu
      scenes.debug.menu()
    end
  end

end

function VictoryScreen:draw()
  local width, height = core.screen:getDimensions()
  self:drawVignette(width, height)
  self:drawLabel(width/2, height/4, self.labelX, self.labelOpacity)

  self:drawContinueBox(width/2, height/2 + 32, self.continueValue, self.tbSize, self.tbOpacity)
end

function VictoryScreen:drawVignette(width, height)
  love.graphics.setColor(0, 0, 0, self.vignetteOpacity)

  love.graphics.rectangle("fill", 0, 0, width, height)
end

function VictoryScreen:drawLabel(x, y, x2, opacity)
  love.graphics.setColor(1, 1, 1, opacity)

  local w, h = self.assets.images["game"]:getDimensions()
  self.assets.images["game"]:draw(x - x2, y, 0, 1, 1, w, h/2)
  self.assets.images["over"]:draw(x + x2, y, 0, 1, 1, 0, h/2)
end

function VictoryScreen:drawContinueBox(x, y, continueValue, size, opacity)
  love.graphics.setColor(1, 1, 1, opacity)
  love.graphics.draw(self.continueBox, x, y, 0, size, size, tw/2, th/2)
  local text = "continue"
  if (continueValue == 1) then
    text = "quit"
  end
  self.assets.images["arrow"]:draw(x - continueValue*64, y, math.rad(-90) * continueValue, continueValue, 1, 13, 13)
  self.assets.fonts["SA2font"]:print(text, x, y-13, "center")
end

return VictoryScreen
