local ListBox   = require "core.modules.menusystem.listbox"
local PauseMenu = ListBox:extend()

local Widget = require "core.modules.menusystem.widgets"

local ResumeWidget    = Widget.Base:extend()
local RestartWidget   = Widget.Base:extend()
local ExitWidget      = Widget.Base:extend()

function PauseMenu:new(controller)
  local height, width, x, y
  height = 72
  width  = 64
  x = 424/2 -  width/2
  y = 240/2 - height/2

  PauseMenu.super.new(self, controller.menusystem, "pauseMenu", x, y, width, height, 3)
  self.controller = controller

  self:setSound(self.controller.assets.sfx["select"])
  self.haveFocus  = false
  self.isActive   = false
  self.isVisible  = false

  ResumeWidget(self)
  RestartWidget(self)
  ExitWidget(self)

  self.canvas = nil
  self.activeCanvas = false
  self.width = 0
end

function PauseMenu:draw()
  if (self.activeCanvas == false) then
    local width = self:getWidth() or 10
    self.width = self:drawCanvas(width, 64)
  end

  love.graphics.draw(self.canvas, (424 - self.width)/2, self.height - 28)
  PauseMenu.super.draw(self)

end

function PauseMenu:drawCanvas(width, height)
  local width = (math.floor( width / 16 ) + 1) * 16
  local height = height or 80
  self.canvas = love.graphics.newCanvas(width + 64, height + 64)

  core.screen:cease()
  love.graphics.setCanvas( self.canvas )

  --self.controller.gui.textbox["solid"]:draw(32, 32, width, height)
  self.controller.assets.fonts["title"]:draw("PAUSE", (width + 64)/2, 12, -1)

  love.graphics.setCanvas( )
  core.screen:cease()

  self.activeCanvas = true

  return width + 64
end

--- MENU WIDGETS

function ResumeWidget:new(menu)
  ResumeWidget.super.new(self, menu)
  self.label      = "resume"
end

function ResumeWidget:action()
  self.controller.pause = false
end

function RestartWidget:new(menu)
  RestartWidget.super.new(self, menu)
  self.label      = "restart"
end

function RestartWidget:action()
  self.controller:restartLevel()
end

function ExitWidget:new(menu)
  ExitWidget.super.new(self, menu)
  self.label      = "exit"
end

function ExitWidget:action()
  self.controller:exitLevel()
end


return PauseMenu
