local cwd  = (...):gsub('%.textmenu$', '') .. "."
local Menu = require(cwd .. "parent")

local TextMenu = Menu:extend()

function TextMenu:new(menusystem, name, x, y, font, slots)
  self.slots      = slots
  TextMenu.super.new(self, menusystem, name, x, y, 0, 0)
  self.ox         = x
  self.oy         = y
  self.font       = font
  self.align      = "left"

  self.begin      = 1

  self:getBoundingBox()
end

function TextMenu:getBoundingBox()
  self:setWidthAuto()
  self.widget.h   = self.font:getHeight()
  self.h          = self.widget.h * self.slots

  if self.align == "right" then
    self.x = self.ox - self.w
  elseif self.align == "center" then
    self.x = self.ox - self.w / 2
  else
    self.x = self.ox
  end

  self.y = self.oy
end

function TextMenu:centerText()
  self:setAlign("center")
end

function TextMenu:setAlign(align)
  self.align = align
  self.font:setAlign("center")
end

function TextMenu:update(dt)
  self:getBoundingBox()
  if self.widget.selected ~= 0 then
    if self.widget.selected < self.begin then
      self.begin = self.widget.selected
    end
    if self.widget.selected > self.begin + self.slots - 1 then
      self.begin = self.widget.selected - self.slots + 1
    end
  end

  if self.begin < 1 then
    self.begin = 1
  end
end

function TextMenu:setWidthAuto()
  local width = self.w

  for i,v in ipairs(self.widget.list) do
    local stringWidth = self.font:getWidth(v.beginlabel .. v.label .. v.endlabel)
    width = math.max(stringWidth, width)
  end
  if width ~= self.w then
    self.canvas.needRedraw = true
  end
  self.w = width
end

function TextMenu:getWidth()
  self:setWidthAuto()
  return self.w
end

function TextMenu:getHeight()
  return self.h
end

function TextMenu:keyreleased(key, code)

  if key == 'up' then
    self:moveCursor(self.widget.selected - 1)
  end

  if key == 'down' then
    self:moveCursor(self.widget.selected + 1)
  end

  if key == "A" then
    if (self.widget.selected > 0) and (self.widget.selected <= #self.widget.list) then
      self.widget.list[self.widget.selected]:action()
    end
  end

  if key == "B" then
    self:cancelAction()
  end

end

function TextMenu:mousemoved(x, y)
  local selectedPrevous = self.widget.selected
  self.widget.selected = self.begin + math.floor(y / self.widget.h)
  if self.widget.selected < 1 then
    self.widget.selected = 1
  end
  if self.widget.selected > #self.widget.list then
    self.widget.selected = #self.widget.list
  end

  if self.widget.selected ~= selectedPrevious then
    self.canvas.needRedraw = true
  end
end

function TextMenu:mousepressed(x, y, button, isTouch)
  self.widget.selected = self.begin + math.floor(y / self.widget.h)
  if self.widget.selected < 1 then
    self.widget.selected = 1
  end
  if self.widget.selected > #self.widget.list then
    self.widget.selected = #self.widget.list
  end
  if #self.widget.list > 0 then
    self.widget.list[self.widget.selected]:action()
  end

  if self.widget.selected ~= selectedPrevious then
    self.canvas.needRedraw = true
  end
end

function TextMenu:drawCanvas()
  print("redraw menu")

  self.canvas.texture = love.graphics.newCanvas(self.w, self.h)
  love.graphics.setCanvas( self.canvas.texture )

  local ox, x

  local widgety = 0
  local ox = self.w / 2
  local x = 0

  self.font:set()
  for i, v in ipairs(self.widget.list) do
    if (i >= self.begin) and (i < self.begin + self.slots) then
      self:drawWidget(i, widgety)

      widgety = widgety + self.widget.h
    end
  end
  utils.draw.resetColor()

  love.graphics.setCanvas( )
end

function TextMenu:drawWidget(widgetID, y)
  local widget = self.widget.list[widgetID]
  print(widget)
  if widget.canvas.needRedraw == true then
    self:drawWidgetCanvas(widget)
    widget.canvas.needRedraw = false
  end

  if self.widget.selected == widgetID and self.focus == true then
    love.graphics.setColor(1, 1, 0, 1)
  else
    love.graphics.setColor(1, 1, 1, 1)
  end

  love.graphics.draw(widget.canvas.texture, 0, y)
end

function TextMenu:drawWidgetCanvas(widget)
  widget.canvas.texture = love.graphics.newCanvas(self.w, self.widget.h)

  love.graphics.setCanvas( widget.canvas.texture )

  self.font:draw(widget.label, math.floor(self.w / 2), 0, -1, self.align)
  self.font:draw(widget.beginlabel, math.floor(0), 0, -1, "left")
  self.font:draw(widget.endlabel, math.floor(self.w), 0, -1, "right")

  love.graphics.setCanvas( self.canvas.texture )
end

function TextMenu:resetView()
  self.begin = 1
  self.canvas.needRedraw = true
end

function Menu:moveView(begin, absolute)
  --local absolute = absolute or true
  self.widget.selected = 0

  if (absolute) then
    self.begin = begin
  else
    self.begin = self.begin + begin
  end
  -- ne sert à rien ici, c'est juste pour éviter des crash

  self.canvas.needRedraw = true
end

function Menu:getView()
  return self.begin
end

function Menu:isViewAtBeggining()
  return (self.begin <= 1)
end

function Menu:isViewAtEnd()
  return ((self.begin + self.slots) > (#self.widget.list))
end

return TextMenu
