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

local love       = require 'love'
local make_class = require 'src.utils.classes'
local collisions = require 'src.utils.colls'
local UIElement  = require 'src.ui.element'
local Color      = require 'src.utils.color'


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

---@class CharInput: UIElement
local TextInput = make_class(UIElement)


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

---@param text string
---@param font Font
---@param x number
---@param y number
---@param callback function
---@param value function
---@param float boolean
---@param base_col Color
---@param sel_color Color
---@param press_col Color
function TextInput:_init(text, font, x, y, callback, value, float, base_col, sel_color, press_col)
    UIElement._init(self, x, y, float, true)
    self.callback  = callback
    self.value     = value
    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)
    self.selected  = false
    self.pressed   = false
end


function TextInput:load()
    self.font:load()

    -- 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 TextInput:unload()
    self.font:unload()
end


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


function TextInput:set_dimensions()
    self.w = self.font:get_width(self.text .. 'm')
    self.h = self.font:get_height(self.text)
end


function TextInput:draw()
    local color = self.callback == nil and self.disbl_col or (((self.pressed or self:is_focused()) 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()

    if self:is_focused() then
        love.graphics.print(self.text .. ': ?', self.x, self.y)
    else
        love.graphics.print(self.text .. ': ' .. self.value(), self.x, self.y)
    end

    self.font:unset()
    love.graphics.setColor()
end


function TextInput:mousemoved(x, y)
    -- Select if the pointer is inside the button's bounding rect.
    self.selected = collisions.point_in_square(x, y, self.x, self.y, self.w, self.h)
end


function TextInput:mousepressed(_, _, btn)
    -- If the pointer was inside the button when it was pressed then mark the button as pressed.
    if btn == 0 and self.selected then
        self:focus()
    end
end


function TextInput:keypressed(key)
    if self:is_focused() then
        if #key == 1 then
            if self.callback ~= nil then self.callback(key) end
            self:blur()
        end
    end
end


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

return TextInput
