speedometer = {}
-- TO_MILE = 0.621371

local floor, random = math.floor, math.random
local speed
local veh

function speedometer:Init()
    -- isKilometers = GetInt("savegame.mod.options.distance_units") == 0
    speed       = 0
    veh         = GetPlayerVehicle()
    gadget = nitro_reloading
	engineColorGood_R, engineColorGood_G, engineColorGood_B         = 1, 1, 1
	engineColorNormal_R, engineColorNormal_G, engineColorNormal_B   = 1, 0.84, 0.24
	engineColorBroken_R, engineColorBroken_G, engineColorBroken_B   = 1, 0.36, 0.24
	engineColorMylti = 1
    timeFlash = 0.5

    if gadget and gadget.Init ~= nil then
        gadget:Init()
    end

    timer_manager:CreateTimer("update_player_speed", 0.1, true, function(v)
        if veh > 0 then
            local vehBody = GetVehicleBody(veh)
            local velocity = GetBodyVelocity(vehBody)
            velocity[2] = 0
            local tempSpeed = VecLength(velocity)
            -- speed = isKilometers and floor(tempSpeed * 3.6) or floor(tempSpeed * 3.6 * TO_MILE)
            speed = floor(tempSpeed * 3.6)
        end
    end, veh)
end

function speedometer:Draw(dt)
	if not (veh > 0 and gadget) then return end

    UiPush()
        UiTranslate(UiWidth() - 340, UiHeight() - 340)
        UiWindow(300, 300)
        UiImage("MOD/ui/hud/speedometer/back.png")

        UiPush()
            UiColor(0,0,0,1)
            UiImage("MOD/ui/hud/gadgets/nitro_back.png")
        UiPop()

        UiPush()
            UiTranslate(UiCenter(), UiMiddle())
            if gadget and gadget.Draw ~= nil then
                gadget:Draw()
            end

            -- local clampSpeed = math:Clamp(speed, 0, isKilometers and 120 or 120*TO_MILE)
            -- self:drawArrow(clampSpeed/(isKilometers and 120 or 120*TO_MILE))
            local clampSpeed = math:Clamp(speed, 0, 120)
            self:drawArrow(clampSpeed/120)
        UiPop()

        UiTranslate(UiCenter(), 138)
        self:drawSpeed(speed)
    UiPop()

    local enginePict = "MOD/ui/hud/speedometer/engine.png"
    UiPush()
        UiTranslate(UiWidth() - 108, UiHeight() - 69)
        UiAlign("right bottom")
        local engineHealth = GetFloat("level.player.engine_health")

        if engineHealth < 0.75 then
            local currentColor_G, currentColor_B

            if engineHealth < 0.5 then
                currentColor_G, currentColor_B = engineColorBroken_G, engineColorBroken_B
            else
                currentColor_G, currentColor_B = engineColorNormal_G, engineColorNormal_B
            end

            if engineColorMylti == 1 then
                SetValue("engineColorMylti", 0, "linear", timeFlash)
            elseif engineColorMylti == 0 then
                SetValue("engineColorMylti", 1, "linear", timeFlash)
            end

            local r = engineColorGood_R
            local g = math:Lerp(currentColor_G, engineColorGood_G, engineColorMylti)
            local b = math:Lerp(currentColor_B, engineColorGood_B, engineColorMylti)
            UiColor(r, g, b)
        end
        UiImage(enginePict)
    UiPop()
end

function speedometer:drawArrow(percentSpeed)
    UiPush()
        local mixAngle
        if percentSpeed <= 0.05 then
            mixAngle = 0
        elseif percentSpeed >= 0.35 then
            mixAngle = random(-2, 1)
        else
            mixAngle = random(-1, 1)
        end
        local angle = math:Lerp(130, -130, percentSpeed) + mixAngle
        UiAlign("center bottom")
        UiRotate(angle)
        UiPush()
            UiTranslate(0, -90)
            UiImage("../../ui/hud/speedometer/speedometer_arrow.png")
        UiPop()
    UiPop()
end

function speedometer:drawSpeed(speed)
    UiPush()
        UiPush()
            UiScale(1.4)
            UiFont(DIGIT_FONT_BOLD, 75)
            UiAlign("center middle")
            local w, h
            if speed > 99 then
                w = 114 -- Size width for 3 digits in speedometer
            elseif speed > 9 then
                w = 76 -- Size width for 2 digits in speedometer
            else
                w = 38 -- Size width for 1 digit in speedometer
            end
            h = 58 -- Size width for 1 digit in speedometer
            UiWindow(w, h)
            UiTranslate(0, UiMiddle())
            UiAlign("left middle")
            UiTextShadow(0, 0, 0, 0.5, 3.0)
            UiText(speed)
        UiPop()
        UiPush()
            UiTranslate(0, 65)
            UiFont("bold.ttf", 40)
            UiAlign("center middle")
            local text = "loc@KMH_UPPERCASE"
            UiTextShadow(0, 0, 0, 0.5, 3.0)
            UiText(text)
        UiPop()
    UiPop()
end
