#include "ui_prompt.lua"

nitro_reloading = {
    prompt = prompt:new("gadget", "NITRO_SHORT", true)
}

local REGISTRY_PERCENT_RELOADING = "level.gadget.percent"

local ico = {}
ico.__index = function(_, key)
	local ico = Ui.ActionIcons[key]
	ico.style = "white"
	ico.iW = 32
	ico.iH = 32
	return ico
end
setmetatable(ico, ico)

function nitro_reloading:Draw(dt)
    local percent = GetFloat(REGISTRY_PERCENT_RELOADING)

    UiPush()
        UiTranslate(-UiCenter(), -UiMiddle())
        UiTranslate(50, 213)
        UiImage("MOD/ui/hud/gadgets/nitro_icn.png")
    UiPop()

    UiPush()
        UiTranslate(0, UiMiddle())
        UiAlign("center middle")
        if percent == 1 then
            local str = GetTranslatedStringByKey("PRESS").." "
            UiFont("regular.ttf", 22)
            local w, _ = UiGetTextSize(str)
            w = w + 42
            UiWindow(w, 44)
            UiTranslate(0, UiMiddle())
            UiAlign("left middle")
            UiText(str)
            local icon
            if LastInputDevice() == UI_DEVICE_GAMEPAD then
                icon = ico["interact"]
            else
                icon = ico["key:" .. GetString("savegame.mod.input.nitro")]
            end
            local _, ico_h = -icon.w - 10, -icon.h / 2 - 5
            UiTranslate(w - 42, ico_h + 5)
            icon:draw()
        end
    UiPop()

    UiPush()
        local maxSegments = 29
        local segments = math.floor((maxSegments + 1) * percent)
        local curSeg = 0
        while curSeg < segments do
            UiAlign("center bottom")
            UiPush()
                UiRotate(math:Lerp(130, -130, curSeg / maxSegments))
                UiTranslate(0, -90)
                UiImage("MOD/ui/hud/gadgets/nitro_bar.png")
            UiPop()
            curSeg = curSeg + 1
        end
    UiPop()
end
