local task = require "lib.utils.task"
local easing = require "lib.utils.easing"
local Element = require "lib.simple_ui.element"
local Rect = require "lib.simple_ui.rect"
local SkillRow = require "lib.simple_ui.level.skill_row"
local Bars = require "lib.simple_ui.level.bottom_bars"
local EndTurnButton = require "lib.simple_ui.level.end_turn"

--- @class CharacterPanel : UIElement
--- @field animationTask Task
--- @field alpha number
--- @field state "show" | "idle" | "hide"
--- @field skillRow SkillRow
--- @field bars BottomBars
--- @field endTurnButton EndTurnButton
local characterPanel = setmetatable({}, Element)
characterPanel.__index = characterPanel

function characterPanel.new(characterId)
    local t = {}
    t.state = "show"
    t.skillRow = SkillRow(characterId)
    t.bars = Bars(characterId)
    t.endTurnButton = EndTurnButton {}
    t.alpha = 0 -- starts hidden/animating
    return setmetatable(t, characterPanel)
end

function characterPanel:show()
    self.state = "show"
    self.animationTask = task.tween(self, { alpha = 1 }, 300, easing.easeOutCubic)
    self.animationTask(function() self.state = "idle" end)
end

function characterPanel:hide()
    self.state = "hide"
    self.animationTask = task.tween(self, { alpha = 0 }, 300, easing.easeOutCubic)
end

--- @type love.Canvas
local characterPanelCanvas;

function characterPanel:update(dt)
    -- Tasks update automatically via task.update(dt) in main.lua
    self.skillRow:update(dt)
    self.bars.bounds = Rect {
        width = self.skillRow.bounds.width,
        x = self.skillRow.bounds.x,
        y = self.skillRow.bounds.y
    }
    self.bars:update(dt)

    self.bounds = Rect {
        x = self.bars.bounds.x,
        y = self.bars.bounds.y,
        width = self.bars.bounds.width,
        height = self.bars.bounds.height + self.skillRow.bounds.height
    }

    self.endTurnButton:layout()
    self.endTurnButton.bounds.x = self.bounds.x + self.bounds.width + 32
    self.endTurnButton.bounds.y = self.bounds.y + self.bounds.height / 2 - self.endTurnButton.bounds.height / 2

    self.endTurnButton:update(dt)

    if not characterPanelCanvas then
        characterPanelCanvas = love.graphics.newCanvas(self.bounds.width, self.bounds.height)
    end

    --- анимация появления
    local revealShader = Tree.assets.files.shaders.reveal
    revealShader:send("t", self.alpha)
end

function characterPanel:draw()
    self.skillRow:draw()

    --- @TODO: переписать этот ужас с жонглированием координатами, а то слишком хардкод (skillRow рисуется относительно нуля и не закрывает канвас)
    love.graphics.push()
    local canvas = love.graphics.getCanvas()
    love.graphics.translate(0, self.bars.bounds.height)
    love.graphics.setCanvas(characterPanelCanvas)
    love.graphics.clear()
    love.graphics.draw(canvas)
    love.graphics.pop()

    love.graphics.push()
    love.graphics.translate(-self.bounds.x, -self.bounds.y)
    self.bars:draw()
    self:drawBorder("outer")
    love.graphics.pop()

    --- рисуем текстуру шейдером появления
    love.graphics.setCanvas()
    love.graphics.setShader(Tree.assets.files.shaders.reveal)
    love.graphics.setColor(1, 1, 1, 1)

    self.endTurnButton:draw()

    love.graphics.push()
    love.graphics.translate(self.bounds.x, self.bounds.y)
    love.graphics.draw(characterPanelCanvas)
    love.graphics.setColor(1, 1, 1)
    love.graphics.pop()
    love.graphics.setShader()
end

return characterPanel.new
