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

local make_class = require 'src.utils.classes'
local Asset      = require 'src.utils.asset'
local Drawable   = require 'src.graphics.drawable'
local Label      = require 'src.ui.label'
local TextButton = require 'src.ui.textbtn'


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

local VBox = make_class(Drawable, Asset)


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

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


function VBox:add_text_button(text, font, callback, float, base_col, sel_color, press_col)
    table.insert(self.elements, TextButton(text, font, 0, 0, callback, float, base_col, sel_color, press_col))
end


function VBox:add_label(text, font, color, float)
    table.insert(self.elements, Label(0, 0, text, font, color, float))
end


function VBox:load()
    local _y = self.y

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

        if v.float_right then
            v.x = self.w - self.x - v.w
        else
            v.x = self.x
        end

        v.y = _y
        _y = _y + v.h + self.s
    end
end


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


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


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


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


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


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


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

return VBox
