local PauseMenu = Object: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)
  self.controller = controller
  self.active = false

  self.height = 56+16

  self.assets = self.controller.assets

  self.menu       = self.controller.menusystem.TextMenu(424/2, self.height+8, self.assets.fonts["menu"], 6)

  self.controller.menusystem:addMenu(self.menu)
  self.menu:centerText(180)
  self.menu:setSound(self.assets.sfx["select"])
  self.menu.focus = true

  self.menu:addWidget(ResumeWidget(self))
  self.menu:addWidget(RestartWidget(self))
  self.menu:addWidget(ExitWidget(self))

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

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

  love.graphics.draw(self.canvas, (424 - self.width)/2, self.height - 28)
  self.controller.menusystem:draw()
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(menusystem)
  ResumeWidget.super.new(self)
  self.label      = "resume"
  self.menusystem = menusystem
  self.controller = menusystem.controller
end

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

function RestartWidget:new(menusystem)
  RestartWidget.super.new(self)
  self.label      = "restart"
  self.menusystem = menusystem
  self.controller = menusystem.controller
end

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

function ExitWidget:new(menusystem)
  ExitWidget.super.new(self)
  self.label      = "exit"
  self.menusystem = menusystem
  self.controller = menusystem.controller
end

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


return PauseMenu
