local menu = {}
local ListBox = require "core.modules.menusystem.listbox"
local MenuObject = ListBox:extend()

function MenuObject:new(scene, name)
  local screenwidth, screenheight = core.screen:getDimensions()
  local height, width, x, y
  height = screenheight - (88 + 24)
  width  = 240
  x = screenwidth/2   - width/2
  y = screenheight/2  - height/2 + 8

  MenuObject.super.new(self, scene.menusystem, name, x, y, width, height, 5)
  self.scene = scene

  self.isActive   = false
  self.isVisible  = false
  self.textbox    = game.gui.newTextBox("assets/sprites/gui/textbox/bluebox.png", width, height)
end

function MenuObject:draw()
  love.graphics.draw(self.textbox, self.x, self.y)
  MenuObject.super.draw(self)
end

menu.Main     = MenuObject:extend()
menu.Debug    = MenuObject:extend()
menu.Levels   = MenuObject:extend()
menu.Bonus    = MenuObject:extend()
menu.Pigs     = MenuObject:extend()
menu.Options  = MenuObject:extend()
menu.newMain  = require "scenes.debugmenu.mainmenu"

local widgets     = require "scenes.debugmenu.widgets"

local leveldata   = require "datas.levels"

function menu.Main:new(scene)
  menu.Main.super.new(self, scene, "mainmenu")
  widgets.Menu(self,"Level Select", "levels")
  widgets.Menu(self,"Debug Mode", "debug")
  widgets.Menu(self,"Options", "mainmenu")
  widgets.Menu(self,"Bonus", "mainmenu")
  widgets.Menu(self,"Back", "main")
  self:getFocus()
end

function menu.Debug:new(scene)
  menu.Debug.super.new(self, scene, "debug")
  widgets.Menu(self,"Manage Pigs", "debugpigs")
  widgets.Menu(self,"Manage Inventory", "debugitems")
  widgets.Menu(self,"Back", "mainmenu")
  self:setCancelWidget()
end

function menu.Levels:new(scene)
  menu.Levels.super.new(self, scene, "levels")
  for i,v in ipairs(leveldata) do
    widgets.Mission(self,v, 1, i)
  end
  widgets.Menu(self,"Back", "mainmenu")
  self:setCancelWidget()
end

function menu.Pigs:new(scene)
  menu.Pigs.super.new(self, scene, "debugpigs")
  widgets.AddPig(self)
  for i,v in ipairs(game.pigmanager.data) do
    widgets.Menu(self,i .. "- " .. v.name .. " the " .. v.race, 1, i)
  end
  widgets.Menu(self,"Back", "debug")
  self:setCancelWidget()
end

function menu.Options:new(scene)
  menu.Options.super.new(self, scene, "options")
  widgets.Save(self)
  widgets.Load(self)
  widgets.Menu(self,"Back", "mainmenu")
  self:setCancelWidget()
end

function menu.Create(scene)
  menu.newMain(scene)
  menu.Main(scene)
  menu.Debug(scene)
  menu.Levels(scene)
  menu.Pigs(scene)
  menu.Options(scene)
end

return menu
