local Widget = {}

BaseWidget  = Object:extend()
DummyWidget = BaseWidget:extend()

function BaseWidget:new(controller)
  self.controller = controller

  self.destroyed  = false
  self.selectable = false
  self.selection_margin = 0
  self.margin = 2
  self.label = ""

  self.beginlabel = ""
  self.endlabel   = ""

  self.canvas = {}
  self.canvas.texture = nil
  self.canvas.needRedraw = true

  self:register()
end

function BaseWidget:register()
  self.controller:addWidget(self)
end

function BaseWidget:setCanvas(w, h)
  self.canvas = {}
  self.canvas.width   = w
  self.canvas.height  = h

  self.canvas.selected = {}
  self.canvas.selected.texture = nil
  self.canvas.selected.active = false

  self.canvas.base = {}
  self.canvas.base.texture = nil
  self.canvas.base.active = false
end

function BaseWidget:selectAction()
  -- Do nothing
end

function DummyWidget:new()
  DummyWidget.super.new(self)
  self.r = love.math.random(128)
  self.g = love.math.random(128)
  self.b = love.math.random(128)
  self.selectable = true
  self.label = "DUMMY WIDGET (see modules.menus.widget)"
end

function DummyWidget:draw(x, y, w, h)
  x = x + self.margin
  y = y + self.margin
  w = w - self.margin * 2
  h = h - self.margin * 2

  love.graphics.setColor(self.r,self.g,self.b,70)
  love.graphics.rectangle("fill", x, y, w, h)
  love.graphics.setColor(self.r,self.g,self.b)
  love.graphics.rectangle("line", x, y, w, h)
end

function BaseWidget:drawSelected(x,y,w,h)
    self:draw(x, y, w, h)
    x = x + self.selection_margin
    y = y + self.selection_margin
    w = w - self.selection_margin * 2
    h = h - self.selection_margin * 2

    love.graphics.setColor(0,0,0)
    love.graphics.rectangle("fill", x, y, 4, 8)
    love.graphics.rectangle("fill", x, y, 8, 4)

    love.graphics.rectangle("fill", x + w, y, -4, 8)
    love.graphics.rectangle("fill", x + w, y, -8, 4)

    love.graphics.rectangle("fill", x, y + h, 4, -8)
    love.graphics.rectangle("fill", x, y + h, 8, -4)

    love.graphics.rectangle("fill", x + w, y + h, -4, -8)
    love.graphics.rectangle("fill", x + w, y + h, -8, -4)

    love.graphics.setColor(255,255,255)
    love.graphics.rectangle("fill", x + 1, y + 1, 2, 6)
    love.graphics.rectangle("fill", x + 1, y + 1, 6, 2)

    love.graphics.rectangle("fill", x + w - 1, y + 1, -2, 6)
    love.graphics.rectangle("fill", x + w - 1, y + 1, -6, 2)

    love.graphics.rectangle("fill", x + 1, y + h - 1, 2, -6)
    love.graphics.rectangle("fill", x + 1, y + h - 1, 6, -2)

    love.graphics.rectangle("fill", x + w - 1, y + h - 1, -2, -6)
    love.graphics.rectangle("fill", x + w - 1, y + h - 1, -6, -2)

end

function BaseWidget:draw(x, y, w, h)
end

function BaseWidget:update(dt)
 -- N/A
end

function BaseWidget:action()
  self:destroy()
end

function BaseWidget:destroy()
  self.destroyed = true
end

function drawWidget_selected(x, y, w, h)
  love.graphics.setColor(0,0,0)
  love.graphics.rectangle("fill", x, y, 4, 8)
  love.graphics.rectangle("fill", x, y, 8, 4)

  love.graphics.rectangle("fill", x + w, y, -4, 8)
  love.graphics.rectangle("fill", x + w, y, -8, 4)

  love.graphics.rectangle("fill", x, y + h, 4, -8)
  love.graphics.rectangle("fill", x, y + h, 8, -4)

  love.graphics.rectangle("fill", x + w, y + h, -4, -8)
  love.graphics.rectangle("fill", x + w, y + h, -8, -4)

  love.graphics.setColor(255,255,255)
  love.graphics.rectangle("fill", x + 1, y + 1, 2, 6)
  love.graphics.rectangle("fill", x + 1, y + 1, 6, 2)

  love.graphics.rectangle("fill", x + w - 1, y + 1, -2, 6)
  love.graphics.rectangle("fill", x + w - 1, y + 1, -6, 2)

  love.graphics.rectangle("fill", x + 1, y + h - 1, 2, -6)
  love.graphics.rectangle("fill", x + 1, y + h - 1, 6, -2)

  love.graphics.rectangle("fill", x + w - 1, y + h - 1, -2, -6)
  love.graphics.rectangle("fill", x + w - 1, y + h - 1, -6, -2)

end


Widget.Base   = BaseWidget
Widget.Dummy  = DummyWidget

return Widget
