#include "../helpers/mathfr.lua"

ui_wrecked_ai_window = {}

-- Time in seconds
local statesTime = {
    0.5, -- Time to appearance
    1,   -- Time to show
    2,   -- Time to vanishing
}

function ui_wrecked_ai_window:new()
    local obj = {
        state       = nil,
        name        = "",
        minW        = 257,
        minH        = 38,
        currentTime = 0.0,


        init = function(self, name)
            self.name   = name
            self.state  = 1
        end,

        draw = function(self, dt)
            UiPush()
                UiFont("bold.ttf", 22)
                local str = GetTranslatedStringByKey(self.name).." "..GetTranslatedStringByKey("WRECKED_UPPERCASE")
                local w, _ = UiGetTextSize(str)
                local mainW, mainH = self.minW, self.minH
                local state = self.state
                w = w + 36
                if w > mainW then mainW = w end

                if state == 1 then
                    UiTranslate(UiWidth() - 347, 183)
                    UiAlign("right top")

                    UiScale(self.currentTime * 2, 1)
                    self:drawWindow(str, mainW, mainH)

                    if self.currentTime == statesTime[state] then
                        self:toOtherState(state + 1)
                    end
                elseif state == 2 then
                    UiTranslate(UiWidth() - 347, 183)
                    UiAlign("right top")
                    self:drawWindow(str, mainW, mainH)

                    if self.currentTime == statesTime[state] then
                        self:toOtherState(state + 1)
                    end
                elseif state == 3 then
                    local currentTime = self.currentTime
                    local DistanceToVanish = 183 + 38
                    local multipleToDistanceVanish = DistanceToVanish / statesTime[state]
                    local alphaToVanish = 1 - (currentTime / statesTime[state])

                    UiTranslate(UiWidth() - 347, 183 - (multipleToDistanceVanish * currentTime))
                    UiAlign("right top")
                    self:drawWindow(str, mainW, mainH, alphaToVanish)

                    if currentTime == statesTime[state] then
                        self:toOtherState(state + 1)
                    end
                end
            UiPop()

            if self.state > 3 then
                self:delete()
                return true
            else
                -- Update animation time
                self.currentTime = math:Clamp(self.currentTime + dt, 0.0, statesTime[self.state])
                return false
            end
        end,

        drawWindow = function(self, str, w, h, alpha)
            alpha = alpha ~= nil and alpha or 1
            UiPush()
                UiWindow(w, h)
                UiColor(1, 1, 1, alpha)
                UiTranslate(UiCenter(), UiMiddle())
                UiAlign("center middle")
                PAGE_COMPONENTS:drawRoundedColorRect(UiWidth(), UiHeight(), 6, 0, 0, 0, 0.5)
                UiText(str)
            UiPop()
        end,

        toOtherState = function(self, newState)
            self.currentTime    = 0.0
            self.state          = newState
        end,

        delete = function(self)
            self.state          = nil
            self.name           = nil
            self.minW           = nil
            self.minH           = nil
            self.currentTime    = nil
        end,
    }

	setmetatable(obj, {__index = self})
	return obj
end
