local Grid = require "core.modules.menusystem.grid"
local MenuObject = Grid:extend()

local Widget    = require "core.modules.menusystem.widgets"
local MainWidget    = Widget.Base:extend()
local MapWidget     = MainWidget:extend()
local ExitWidget    = MainWidget:extend()
local OptionsWidget = MainWidget:extend()

local DIMVALUE      = .5

function MenuObject:new(scene)
  self.scene = scene
  local screenwidth, screenheight = core.screen:getDimensions()
  local menuwidth   = screenwidth   - 32 *  2
  local menuheight  = screenheight  - 48 - 16

  local colNumber, lineNumber
  colNumber   = math.floor(menuwidth / 16)
  lineNumber  = math.floor(menuheight / 16)

  menuwidth  = colNumber * 16
  menuheight = lineNumber * 16

  print(colNumber, lineNumber)

  MenuObject.super.new(self, scene.menusystem, "main", 32, 48, menuwidth, menuheight, colNumber, lineNumber)
  self.isVisible = true
  self.isActive  = true

  MapWidget(self)
  MainWidget(self, "MANAGE PIGS", "blue", "manage")
  MainWidget(self, "BONUS", "green", "bonus")
  OptionsWidget(self)
  ExitWidget(self)
  self:addSlot(1,  0,  0, 13,  6)
  self:addSlot(2, 13,  0,  6,  6)
  self:addSlot(3, 19,  0,  7,  6)
  self:addSlot(4, 13,  6, 13,  3)
  self:addSlot(5, 13,  9, 13,  3)
end

function MainWidget:new(menusystem, label, color, icon)
  MainWidget.super.new(self, menusystem)
  self.scene = menusystem.scene
  self.label = label or ""
  self.color = color or "blue"
  self.icon  = icon  or "exit"
end

function MainWidget:redrawCanvas()
  self.width, self.height = self.menu:getWidgetSize(self.id)
  local filename = "assets/sprites/gui/textbox/" .. self.color .. "box.png"
  self.textbox   = game.gui.newTextBox(filename, self.width, self.height)
  local iconname = "assets/sprites/menu/" .. self.icon .. ".png"
  self.icon      = love.graphics.newImage(iconname)
  MainWidget.super.redrawCanvas(self)
end

function MainWidget:drawCanvas()
  love.graphics.draw(self.textbox, 0, 0)
  if self.height >= 64 then
    love.graphics.printf(self.label, 0, 8, self.width, "center")
    love.graphics.draw(self.icon, self.width/2, self.height/2 + 12, 0, 1, 1, 19, 19)
  else
    love.graphics.printf(self.label, 64, 16, self.width, "left")
    love.graphics.draw(self.icon, 19+16, self.height/2, 0, 1, 1, 19, 19)

  end
end

function MainWidget:draw(x, y)
  if self.menu.isActive == false then
    love.graphics.setColor(DIMVALUE, DIMVALUE, DIMVALUE, 1)
  else
    utils.graphics.resetColor()
  end

  if self.canvas.texture ~= nil then
    love.graphics.draw(self.canvas.texture, x, y)
  end

  utils.graphics.resetColor()
end

function MapWidget:new(menusystem)
  MapWidget.super.new(self, menusystem, "WORLD MAP", "red", "worldmap")
end

function MapWidget:action()
  scenes.WorldMap()
end

function ExitWidget:new(menusystem)
  MapWidget.super.new(self, menusystem, "EXIT GAME", "grey")
end

function ExitWidget:action()
  love.event.quit()
end

function OptionsWidget:new(menusystem)
  OptionsWidget.super.new(self, menusystem, "OPTIONS", "yellow", "options")
end

function OptionsWidget:action()
  self.scene:selectMenu("mainmenu")
end


return MenuObject
