local ParentMenu = require "game.modules.menus.parents.menu"
local Widget  = require "core.modules.menusystem.widgets"

local list = {}
list.ListMenu = ParentMenu:extend()
list.CenteredWidget = Widget.Text:extend()
list.DualTextWidget = list.CenteredWidget:extend()
list.SubMenuWidget  = list.DualTextWidget:extend()

local MENU_ITEM_HEIGHT = 16

local gui = require "game.modules.gui"

-- ListMenu
function list.ListMenu:new(scene, name, x, y, w, itemNumber, isBoxed)
  self.scene = scene
  self.name = name
  local h = itemNumber * MENU_ITEM_HEIGHT
  list.ListMenu.super.new(self, scene, name, x, y, w, h, itemNumber)
  self.cursorTexture = love.graphics.newImage("assets/gui/cursor-menulist.png")
  self.cursorTransition = 0
  self.submenus = {}
  self.itemNumber = itemNumber

  self.isBoxed = isBoxed
  if (self.isBoxed) then
    self.box = gui.newTextBox("assets/gui/dialogbox.png", w, h+16)
  end
end

function list.ListMenu:addSubMenu(name, nicerName)
  local submenu = self:clone(name)
  self:addSubMenuWidget(name, nicerName)
  table.insert(self.submenus, name)
end

function list.ListMenu:addSubMenuWidget(newmenu, name)
  list.SubMenuWidget(self.scene, self.name, newmenu, name)
end

function list.ListMenu:finalize(parent)
  if (parent ~= "" and parent ~= nil) then
    self:addSubMenuWidget(parent, "Back")
    self:setCancelWidget()
  end

  for i,name in ipairs(self.submenus) do
    if (self.menusystem.menus[name] ~= nil) then
      self.menusystem.menus[name]:finalize(self.name)
    end
  end
end

function list.ListMenu:clone(name)
  return list.ListMenu(self.scene, name, self.x, self.y, self.w, self.itemNumber, self.isBoxed)
end

function list.ListMenu:update(dt)
  list.ListMenu.super.update(self, dt)
  self:updateCursorPosition(dt)
end

function list.ListMenu:updateCursorPosition(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
    local speed = dt*45
    local movement = ((relativecursor) - (self.cursorTransition))
    self.cursorTransition = (self.cursorTransition) + movement * speed
  end
end

function list.ListMenu:drawCursor()
  local x, y = self:getCursorPosition()
  love.graphics.draw(self.cursorTexture, x, y)
end

function list.ListMenu:getCursorPosition()
  local addition = MENU_ITEM_HEIGHT
  local x = self.x + 6
  local y = self.y + ((self.cursorTransition) * addition) + 1
  return x, y
end

function list.ListMenu:draw()
  if (self.isBoxed) then
    love.graphics.draw(self.box, self.x, self.y - 8)
  end
  self:updateView()
  local widgety = self.y
  local widgetx = self.x
  for i,v in ipairs(self.widget.list) do
    if (i >= self.view.firstSlot) and (i < self.view.firstSlot + self.view.slotNumber) then
      v:draw(widgetx, widgety, self.w, self.widget.h)
      if self.widget.selected == i and self:haveFocus() == true then
        v:drawSelected(widgetx, widgety, self.w, self.widget.h)
      else
        v:draw(widgetx, widgety, self.w, self.widget.h)
      end
      widgetx, widgety = self:getNextPosition(widgetx, widgety, self.widget.h)
    end
  end
end

function list.ListMenu:getNextPosition(x, y, h)
  return x, y+h
end

-- Widgets
-- Tout les widgets pour la liste de base

-- Widget centré
function list.CenteredWidget:new(scene, menu_name, label)
  local font = scene.assets.fonts["small"]
  font:setFilter("shadow")
  self.scene = scene
  local widgetMenu = scene.menusystem.menus[menu_name]
  list.CenteredWidget.super.new(self, widgetMenu, font, label)
end

function list.CenteredWidget:drawCanvas()
  local h = math.floor(self.height / 2) - (self.font:getHeight() / 2) - 2
  self.font:setColor(self.color[1], self.color[2], self.color[3], 1)
  self.font:draw(self.label, self.width / 2, h, -1, "center")
  self.font:setColor(1, 1, 1, 1)
  utils.graphics.resetColor()
end

-- Widget avec deux bouts de textes
function list.DualTextWidget:new(scene, menu_name, label, label2)
  self.label2 = label2
  list.DualTextWidget.super.new(self, scene, menu_name, label)
end

function list.DualTextWidget:drawCanvas()
  local h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
  self.font:setColor(self.color[1], self.color[2], self.color[3], 1)
  self.font:draw(self.label, 16, h, -1, "left")
  self.font:draw(self.label2, self.width - 8, h, -1, "right")
  self.font:setColor(1, 1, 1, 1)
  utils.graphics.resetColor()
end

-- Widget de sous-menu
function list.SubMenuWidget:new(scene, menu_name, newmenu, label)
  local label2 = ""
  if (label ~= "Back") then
    label2 = ">"
  end
  list.SubMenuWidget.super.new(self, scene, menu_name, label, label2)
  self.newmenu = newmenu
end

function list.SubMenuWidget:action()
  self.scene.menusystem:switchMenu(self.newmenu)
  self.scene.menusystem.menus[self.newmenu]:activationAction()
end

return list
