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

local menu = {}
menu.CustomMenu = ListBox:extend()
menu.BasicMenu = ListBox:extend()
menu.BoxedMenu = menu.BasicMenu:extend()
menu.BasicWidget = Widget.Text:extend()
menu.BoxedWidget = menu.BasicWidget:extend()

local CONST = {}
CONST.MENU = {}
CONST.MENU.ITEM_HEIGHT = 16

local gui = require "game.modules.gui"

-- Custom Menu
function menu.CustomMenu:new(scene, name, x, y, w, h, itemNumber)
  self.scene = scene
  menu.CustomMenu.super.new(self, scene.menusystem, name, x, y, w, h, itemNumber)
end

-- Basic menu
function menu.BasicMenu:new(scene, name, itemNumber, x, y, w)
  self.scene = scene
  local h = itemNumber * CONST.MENU.ITEM_HEIGHT
  menu.BasicMenu.super.new(self, scene.menusystem, name, x, y, w, h, itemNumber)
  self.cursorTexture = love.graphics.newImage("assets/gui/cursor-menulist.png")
  self.cursorTransition = 0
end

function menu.BasicMenu:update(dt)
  menu.BasicMenu.super.update(self, dt)

  local relativecursor = self.widget.selected - self.view.firstSlot

  local transition = self.cursorTransition - relativecursor

  if math.abs(transition) < 0.1 then
    self.cursorTransition = relativecursor
  else
    self.cursorTransition = (self.cursorTransition) + ((relativecursor) - (self.cursorTransition)) * dt*45
  end

end

function menu.BasicMenu:drawCursor()
  local addition = 17
  local x = self.x - 1
  local y = self.y + ((self.cursorTransition) * addition) + 1
  love.graphics.draw(self.cursorTexture, x, y)
end

-- Boxed menu
function menu.BoxedMenu:new(scene, name, itemNumber, x, y, w)
  menu.BoxedMenu.super.new(self, scene, name, itemNumber, x, y, w)
  self.box = gui.newTextBox("assets/gui/dialogbox.png", self.w+16, self.h+16)
end

function menu.BoxedMenu:draw()
  love.graphics.draw(self.box, self.x-8, self.y-8)
  menu.BoxedMenu.super.draw(self)
end

-- Basic Widget
function menu.BasicWidget:new(scene, menu_name, label, label2)
  local font = scene.assets.fonts["small"]
  self.scene = scene
  local widgetMenu = scene.menusystem.menus[menu_name]
  self.label2 = label2 or ""
  menu.BasicWidget.super.new(self, widgetMenu, font, label)
end

function menu.BasicWidget:drawCanvas()
  local h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
  love.graphics.setColor(0, 0, 0, 1)
  self.font:print(self.label, 9, h, "left")
  self.font:print(self.label2, self.width - 7, h, "right")
  utils.graphics.resetColor()
  self.font:print(self.label, 8, h, "left")
  self.font:print(self.label2, self.width - 8, h, "right")
end

-- Basic Widget
function menu.BoxedWidget:new(scene, menu_name, label, label2)
  menu.BoxedWidget.super.new(self, scene, menu_name, label, label2)
  self.box = gui.newChoiceBack(self.menu.w + 24)
end

function menu.BoxedWidget:drawCanvas()
  love.graphics.draw(self.box, 0, 0)

  local h = math.floor(self.height / 2) - (self.font:getHeight() / 2) - 1
  love.graphics.setColor(0, 0, 0, .8)
  self.font:print(self.label, 17, h, "left")
  self.font:print(self.label2, self.width - 8, h, "right")
  utils.graphics.resetColor()
  self.font:print(self.label, 16, h, "left")
  self.font:print(self.label2, self.width - 9, h, "right")
end


return menu
