local gui = {}

local ProgressBar = Object:extend()

function gui.newBorder(id, width, height)
  local filename = "assets/sprites/gui/hborder.png"
  local texture = love.graphics.newImage(filename)
  local w, h    = texture:getDimensions()
  local quad    = love.graphics.newQuad(0, id*32, 48, 32, w, h)
  local back    = love.graphics.newQuad(0, id*32, 24, 32, w, h)
  local canvas  = love.graphics.newCanvas(width, height)

  local xstart  = w - 48

  love.graphics.setCanvas( canvas )

  for i=1, math.ceil(height / 32) do
    love.graphics.draw(texture, quad, xstart, (i-1)*32)
    for j=1, math.ceil(height / 32) do
      love.graphics.draw(texture, back, xstart - ((j)*24), (i-1)*32)
    end
  end

  love.graphics.setCanvas( )

  return canvas
end

function gui.newTextBox(filename, width, height)
  local baseimage = love.graphics.newImage(filename)
  local quad = {}
  quad[1] = love.graphics.newQuad(00, 00, 8, 8, 24, 24)
  quad[2] = love.graphics.newQuad(00, 08, 8, 8, 24, 24)
  quad[3] = love.graphics.newQuad(00, 16, 8, 8, 24, 24)
  quad[4] = love.graphics.newQuad(08, 00, 8, 8, 24, 24)
  quad[5] = love.graphics.newQuad(08, 08, 8, 8, 24, 24)
  quad[6] = love.graphics.newQuad(08, 16, 8, 8, 24, 24)
  quad[7] = love.graphics.newQuad(16, 00, 8, 8, 24, 24)
  quad[8] = love.graphics.newQuad(16, 08, 8, 8, 24, 24)
  quad[9] = love.graphics.newQuad(16, 16, 8, 8, 24, 24)
  local canvas  = love.graphics.newCanvas(width, height)

  love.graphics.setCanvas( canvas )


  for i=1, math.floor(width/8) do
    if (i == 1) then
      -- first line
      for j=1, math.floor(height/8) do
        if j == 1 then
          love.graphics.draw(baseimage, quad[1], (i-1)*8, (j-1)*8)
        elseif j == math.floor(height/8) then
          love.graphics.draw(baseimage, quad[3], (i-1)*8, (j-1)*8)
        else
          love.graphics.draw(baseimage, quad[2], (i-1)*8, (j-1)*8)
        end
      end
    elseif (i == math.floor(width/8)) then
      -- last line
      for j=1, math.floor(height/8) do
        if j == 1 then
          love.graphics.draw(baseimage, quad[7], (i-1)*8, (j-1)*8)
        elseif j == math.floor(height/8) then
          love.graphics.draw(baseimage, quad[9], (i-1)*8, (j-1)*8)
        else
          love.graphics.draw(baseimage, quad[8], (i-1)*8, (j-1)*8)
        end
      end
    else
      -- middle lines
      for j=1, math.floor(height/8) do
        if j == 1 then
          love.graphics.draw(baseimage, quad[4], (i-1)*8, (j-1)*8)
        elseif j == math.floor(height/8) then
          love.graphics.draw(baseimage, quad[6], (i-1)*8, (j-1)*8)
        else
          love.graphics.draw(baseimage, quad[5], (i-1)*8, (j-1)*8)
        end
      end
    end
  end

  love.graphics.setCanvas( )

  return canvas
end

function gui.newProgressBar(sprite, outerfont, innerfont, label, width)
  return ProgressBar(sprite, outerfont, innerfont, label, width)
end

function ProgressBar:new(sprite, outerfont, innerfont, label, width)
  local filepath = "assets/sprites/gui/progressbar/"
  self.image    = love.graphics.newImage(filepath .. sprite .. ".png")
  local backimg = love.graphics.newImage(filepath .. "backbar.png")
  self.quad     = {}
  self.quad[1]  = love.graphics.newQuad(0, 0, 2, 12, 5, 12)
  self.quad[2]  = love.graphics.newQuad(2, 0, 1, 12, 5, 12)
  self.quad[3]  = love.graphics.newQuad(3, 0, 2, 12, 5, 12)

  self.width = width

  self.outerfont = outerfont
  self.innerfont = innerfont

  self.ox = self.outerfont:getWidth(label) + 2

  self.frontcanvas  = love.graphics.newCanvas(width, 12)
  self.backcanvas   = love.graphics.newCanvas(self.ox + width, 12)

  -- On commence par préparer le canvas avant, qui sera utilisé pour dessiner
  -- la partie coloré de la barre de progres
  love.graphics.setCanvas( self.frontcanvas )


  for i=1, (self.width-4) do
    love.graphics.draw(self.image, self.quad[2], i + 1, 0)
  end

  love.graphics.draw(self.image, self.quad[1], 0, 0)
  love.graphics.draw(self.image, self.quad[3], width - 2, 0)

  -- On commence par préparer le fond de la barre
  love.graphics.setCanvas( self.backcanvas )

  for i=1, (width-4) do
    love.graphics.draw(backimg, self.quad[2], self.ox + i + 1, 0)
  end

  love.graphics.draw(backimg, self.quad[1], self.ox, 0)
  love.graphics.draw(backimg, self.quad[3], self.ox + width - 2, 0)

  self.outerfont:set()
  love.graphics.printf(label, 0, -1, self.ox, "left")

  love.graphics.setCanvas(  )

end

function ProgressBar:draw(x, y, stat, innerlabel)
  love.graphics.draw(self.backcanvas, x - self.ox, y)
  love.graphics.setScissor(x, y, self.width * stat, 12)
  love.graphics.draw(self.frontcanvas, x, y)
  love.graphics.setScissor()
end

function gui.newBanner(bannername, string)
  local Banner      = love.graphics.newImage( "assets/sprites/gui/" .. bannername )
  local BannerFont  = love.graphics.newImageFont("assets/fonts/large.png", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ", 1)
  local w, h        = Banner:getDimensions()
  bannercanvas      = love.graphics.newCanvas(w, h)

  love.graphics.setCanvas( bannercanvas )

  love.graphics.draw(Banner, 0, 0)
  love.graphics.setFont(BannerFont)
  love.graphics.printf(string, 0, 6, 480, "center")

  love.graphics.setCanvas( )

  return bannercanvas
end

return gui
