------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------

local make_class = require 'src.utils.classes'
local UIElement  = require 'src.ui.element'


------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------

local Layout = make_class(UIElement)


------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------

function Layout:_init(x, y, w, h, spacing, float)
    UIElement._init(self, x, y, float)
    self.x        = x ~= nil and x or 0
    self.y        = y ~= nil and y or 0
    self.w        = w
    self.h        = h
    self.s        = spacing ~= nil and spacing or 10
    self.elements = {}
end


function Layout:add(element)
    table.insert(self.elements, element)
end


function Layout:load()
    -- Load required assets if needed and then compute the coordinates of each button.
    for _, v in pairs(self.elements) do
        if v.is_a[UIElement] then
            v:load()
        end
    end

    self:set_dimensions()
end


function Layout:unload()
    for _, v in pairs(self.elements) do
        if v.is_a[UIElement] then
            v:unload()
        end
    end
end


function Layout:update(dt)
    for _, v in pairs(self.elements) do
        v:update(dt)
    end
end


function Layout:draw()
    for _, v in pairs(self.elements) do
        v:draw()
    end
end


function Layout:mousemoved(x, y, dx, dy)
    for _, v in pairs(self.elements) do
        v:mousemoved(x, y, dx, dy)
    end
end


function Layout:mousepressed(x, y, btn)
    for _, v in pairs(self.elements) do
        v:mousepressed(x, y, btn)
    end
end


function Layout:mousereleased(x, y, btn)
    for _, v in pairs(self.elements) do
        v:mousereleased(x, y, btn)
    end
end


------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------

return Layout
