FlowBox = Menu:extend()

function FlowBox:new(x,y,w,h,slots_hor,slots_vert)
  ListBox.super.new(self, x, y, w, h)
  self.slots = slots_hor * slots_vert
  self.slots_hor = slots_hor
  self.slots_vert = slots_vert
  self.begin = 1
  self.widgetsH = math.floor( self.h / slots_vert )
  self.widgetsW = math.floor( self.w / slots_hor )
  self.h = slots_vert * self.widgetsH -- On fait en sorte que la hauteur
  self.w = slots_hor * self.widgetsW -- et la largeur
  -- soit un multiple du nombre de slot et de leur dimensions
end

function FlowBox:update(dt)
  local col, line = self:getCoord(self.selected)
  local begincol, beginline = self:getCoord(self.begin)

  if line < beginline then
    beginline = line
  end

  if line > beginline + self.slots_vert - 1 then
    beginline = line - self.slots_vert + 1
  end

  if beginline < 0 then
    beginline = 0
  end

  self.begin = beginline * self.slots_hor + 1
end

function FlowBox:getCoord(id_selected)
  id_selected = id_selected - 1 -- On simplifie les calcul en prenant 0 comme départ
  local line, col
  line = math.floor(id_selected / self.slots_hor)
  col = id_selected - (line * self.slots_hor)
  return col, line
end

function FlowBox:moveCursor(new_col, new_line)
  local col, line = self:getCoord(self.selected)
  local lastcol, lastline = self:getCoord(#self.listWidget)


  if new_line < 0 then
    new_line = lastline
  end

  if new_line > lastline then
    new_line = 0
  end

  if (new_line == lastline) then
    if new_col < 0 then
      new_col = lastcol
    end

    if new_col > lastcol then
      if (line == lastline) then
      new_col = 0
      else
      new_col = lastcol
      end
    end
  else
    if new_col < 0 then
      new_col = self.slots_hor - 1
    end

    if new_col == self.slots_hor then
      new_col = 0
    end
  end

  self.selected = (new_line * self.slots_hor) + new_col + 1
end

function FlowBox:keyreleased(key, code)
  local col, line = self:getCoord(self.selected)
  if key == 'left' then
    self:moveCursor(col - 1, line)
  end

  if key == 'right' then
    self:moveCursor(col + 1, line)
  end

  if key == 'up' then
    self:moveCursor(col, line - 1)
  end

  if key == 'down' then
    self:moveCursor(col, line + 1)
  end

  if key == "A" then
    self.listWidget[self.selected]:action()
  end
end

function FlowBox:mousemoved(x, y)
  local col, line = self:getCoord(self.selected)
  local begincol, beginline = self:getCoord(self.begin)
  local newcol, newline

  newline = beginline + math.floor(y / self.widgetsH)
  newcol  = math.floor(x / self.widgetsW)
  self.selected = (newline * self.slots_hor) + newcol + 1

  if self.selected < 1 then
    self.selected = 1
  end
  if self.selected > #self.listWidget then
    self.selected = #self.listWidget
  end
end

function FlowBox:mousepressed(x, y, button, isTouch)
  local col, line = self:getCoord(self.selected)
  local begincol, beginline = self:getCoord(self.begin)
  local newline, newcol

  newline = beginline + math.floor(y / self.widgetsH)
  newcol  = math.floor(x / self.widgetsW)
  self.selected = (newline * self.slots_hor) + newcol + 1

  if self.selected < 1 then
    self.selected = 1
  end
  if self.selected > #self.listWidget then
    self.selected = #self.listWidget
  end

  if #self.listWidget > 0 then
    self.listWidget[self.selected]:action()
  end
end

function FlowBox:draw()
  local widgety = self.y
  local widgetx = self.x
  for i,v in ipairs(self.listWidget) do
    if (i >= self.begin) and (i < self.begin + self.slots) then
      v:draw(widgetx, widgety, self.widgetsW, self.widgetsH)
      if self.selected == i and self.focus == true then
        v:drawSelected(widgetx, widgety, self.widgetsW, self.widgetsH)
      else
        v:draw(widgetx, widgety, self.widgetsW, self.widgetsH)
      end
      widgetx = widgetx + self.widgetsW
      if widgetx == (self.x + self.w) then
        widgetx = self.x
        widgety = widgety + self.widgetsH
      end
    end
  end
end
