local Rect = require "birb.objects.2D.rect"

local GuiElement = Rect:extend()

function GuiElement:new(name, x, y, w, h)
    GuiElement.super.new(self, x, y, w, h)
    self.name = name

    self.isDestroyed = false
    self.isVisible = true
    self.isActive = true
    self.isLocked = false
    self.isAlwaysVisible = false

    self.depth = 0

    self:register()
end

function GuiElement:getGui()
    local scene = core.scenemanager.currentScene
    return scene.menusystem
end

function GuiElement:register()
    local gui = self:getGui()
    gui:addMenu(self.name, self)
end

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

-- VISIBILITY/ACTIVITY
-- Handle drawing and how we interact with

function GuiElement:setDepth(depth)
    self.depth = depth or 0
end

function GuiElement:setVisibility(visibility)
    if self.isLocked == false and self.isAlwaysVisible == false then
        self.isVisible = visibility
    else
        -- if the element is locked (thus is always active), it should also
        -- be always visible.
        self.isVisible = true
    end
end

function GuiElement:setActivity(activity)
    if self.isLocked == false then
        self.isActive = activity
    else
        self.isActive = true
    end
end

function GuiElement:lock(lock)
    self.isLocked = lock
end

function GuiElement:lockVisibility(lock)
    self.isAlwaysVisible = lock
end

function GuiElement:getFocus()
    local gui = self:getGui()
    gui.focusedMenu = self.name
end

function GuiElement:haveFocus()
    local gui = self:getGui()
    return (gui.focusedMenu == self.name)
end

-- UPDATE FUNCTIONS
-- Update the menu every game update

-- External update function
function GuiElement:updateElement(dt)
    self:update(dt)
end

-- Internal update function
function GuiElement:update(dt)
    -- Cette fonction ne contient rien par défaut
end

-- DRAW FUNCTIONS
-- Draw the menu and its content

function GuiElement:drawElement()
    self:draw()
end

function GuiElement:draw()
    -- nothing here
end

-- KEYBOARD FUNCTIONS
-- Handle key press

function GuiElement:keyreleased(key)
    -- Cette fonction ne contient rien par défaut
end

-- MOUSE FUNCTIONS
-- Handle pointers (clic/touch)

function GuiElement:mousemoved(x, y)
    -- Cette fonction ne contient rien par défaut
end

function GuiElement:mousepressed(x, y, button, istouch)
    -- Cette fonction ne contient rien par défaut
end

return GuiElement
