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

local love       = require 'love'
local make_class = require 'src.utils.classes'
local Button     = require 'src.ui.button'
local Color      = require 'src.utils.color'


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

local TextButton = make_class(Button)


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

function TextButton:_init(text, font, x, y, callback, float, base_col, sel_color, press_col)
    Button._init(self, x, y, callback, float)
    self.font      = font
    self.text      = text
    self.base_col  = base_col  ~= nil and base_col  or Color(255, 255, 255)
    self.sel_color = sel_color ~= nil and sel_color or Color(215, 0, 0)
    self.press_col = press_col ~= nil and press_col or Color(99, 99, 139)
    self.disbl_col = Color(95, 63, 75)
end


function TextButton:load()
    if not self.font:is_loaded() then
        self.font:load()
    end

    -- If the label's size has not been computed then do it.
    if self.w == nil or self.h == nil then self:set_dimensions() end
end


function TextButton:unload()
    self.font:unload()
end


function TextButton:is_loaded()
    return self.font:is_loaded() and self.w ~= nil and self.h ~= nil
end


function TextButton:set_dimensions()
    self.w = self.font:get_width(self.text)
    self.h = self.font:get_height(self.text)
end


function TextButton:draw()
    local color = self.callback == nil and self.disbl_col or ((self.pressed and self.press_col) or ((self.selected and self.sel_color) or self.base_col))

    love.graphics.setColor(color.r, color.g, color.b)
    self.font:set()
    love.graphics.print(self.text, self.x, self.y)
    self.font:unset()
    love.graphics.setColor()
end


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

return TextButton
