#include "ui/ui_extensions/ui_defs.lua"
#include "../../../data/ui/ui_extensions/ui_icon.lua"

prompt = {}
smooth = 1
targetSmooth = 0.2

local ico = {}
ico.__index = function(_, key)
	local ico = Ui.ActionIcons[key]
	ico.style = "white"
	return ico
end
setmetatable(ico, ico)
function prompt:new(registryButton, action, isBlinking)
    local obj = {
        registryButton = registryButton,
        action = action,
        isBlinking = isBlinking,

        Draw = function (self, dt)
            dt = dt or GetTimeStep()
            if smooth <= 0.2+dt*2 then
                targetSmooth = 1
                if isBlinking then SetValue("smooth", targetSmooth, "linear", 0.5) end
            elseif smooth >= 1-dt*2 then
                targetSmooth = 0.2
                if isBlinking then SetValue("smooth", targetSmooth, "linear", 0.5) end
            end
            local btn
            if LastInputDevice() == UI_DEVICE_GAMEPAD then
                if registryButton == "respawn" then
                    btn = "vehicle_raise"
                elseif registryButton == "gadget" then
                    btn = "interact"
                end
            else
                btn = GetString("savegame.mod.input." .. registryButton)
            end
            UiAlign("left middle")
            UiTextUniformHeight(true)
            UiColorFilter(1, 1, 1, smooth)
            UiFont("regular.ttf", 30)
            local text = GetTranslatedStringByKey("PRESS")
            local w, _ = UiGetTextSize(text)
            UiText(text)
            UiTranslate(w + 8, 0)
            local space
            UiPush()
                space = self:drawButton(btn)
            UiPop()

            UiTranslate(space + 8, 0)
            UiText(GetTranslatedStringByKey("TO_SPACED")..GetTranslatedStringByKey(action))
        end,

        findPromptWidth = function(self)
            local allWidth = 16
            local w, _
            UiPush()
                UiFont("regular.ttf", 30)
                w, _ = UiGetTextSize(GetTranslatedStringByKey("PRESS"))
                allWidth = allWidth + w
                w, _ = UiGetTextSize(GetTranslatedStringByKey("TO_SPACED")..GetTranslatedStringByKey(self.action))
                allWidth = allWidth + w + 40
            UiPop()

            return allWidth
        end,

        drawButton = function(self, btn)
            local icon
            local ico_h_offset = 0
            if LastInputDevice() == UI_DEVICE_GAMEPAD then
                icon = ico[btn]
                if btn == "vehicle_raise" then
                    ico_h_offset = icon.h / 4
                end
            else
                icon = ico["key:" .. btn]
            end
            local h = -icon.h / 2 + ico_h_offset
            UiPush()
                UiTranslate(0, h)
                icon:draw()
            UiPop()
            return icon.w
        end,
    }
    setmetatable(obj, {__index = self})
	return obj
end
