UiYOffset = GetIntParam("UiYOffset", 0)
soundPath = GetStringParam("soundPath")
imgPath = GetStringParam("imagesPath")

function init()
    enabled = true

    sensorFrontRight = FindLocation("sensor_front_right")
    sensorFrontLeft = FindLocation("sensor_front_left")
    sensorFrontFarRight = FindLocation("sensor_front_far_right")
    sensorFrontFarLeft = FindLocation("sensor_front_far_left")
    sensorTailRight = FindLocation("sensor_tail_right")
    sensorTailLeft = FindLocation("sensor_tail_left")
    sensorTailFarRight = FindLocation("sensor_tail_far_right")
    sensorTailFarLeft = FindLocation("sensor_tail_far_left")

    sensorFrontRightLocalPosBase = GetLocationTransform(sensorFrontRight).pos
    sensorFrontLeftLocalPosBase = GetLocationTransform(sensorFrontLeft).pos
    sensorFrontFarRightLocalPosBase = GetLocationTransform(sensorFrontFarRight).pos
    sensorFrontFarLeftLocalPosBase = GetLocationTransform(sensorFrontFarLeft).pos
    sensorTailRightLocalPosBase = GetLocationTransform(sensorTailRight).pos
    sensorTailLeftLocalPosBase = GetLocationTransform(sensorTailLeft).pos
    sensorTailFarRightLocalPosBase = GetLocationTransform(sensorTailFarRight).pos
    sensorTailFarLeftLocalPosBase = GetLocationTransform(sensorTailFarLeft).pos

    vehicle = FindVehicle("EVF")
    vehicleTransform = GetVehicleTransform(vehicle)

    sensorFrontRightLocalPos = TransformToLocalPoint(vehicleTransform, sensorFrontRightLocalPosBase)
    sensorFrontLeftLocalPos = TransformToLocalPoint(vehicleTransform, sensorFrontLeftLocalPosBase)
    sensorFrontFarRightLocalPos = TransformToLocalPoint(vehicleTransform, sensorFrontFarRightLocalPosBase)
    sensorFrontFarLeftLocalPos = TransformToLocalPoint(vehicleTransform, sensorFrontFarLeftLocalPosBase)
    sensorTailRightLocalPos = TransformToLocalPoint(vehicleTransform, sensorTailRightLocalPosBase)
    sensorTailLeftLocalPos = TransformToLocalPoint(vehicleTransform, sensorTailLeftLocalPosBase)
    sensorTailFarRightLocalPos = TransformToLocalPoint(vehicleTransform, sensorTailFarRightLocalPosBase)
    sensorTailFarLeftLocalPos = TransformToLocalPoint(vehicleTransform, sensorTailFarLeftLocalPosBase)

    if soundPath == "" then
        soundPath = "MOD/extra-scripts/snd"
    end
    if imgPath == "" then
        imgPath = "MOD/extra-scripts/ui/parkingsensor"
    end

    sensorChime = LoadLoop(soundPath .. "/parkingsensor.ogg")
    sensorChimeOneShot = LoadSound(soundPath .. "/parkingsensor.ogg")

    h1 = false h2 = false h3 = false h4 = false h5 = false h6 = false h7 = false h8 = false
    d1 = 0 d2 = 0 d3 = 0 d4 = 0 d5 = 0 d6 = 0 d7 = 0 d8 = 0

    inVehicle = false
end

function SensorCheck(origin, direction)
    vehicleTransform = GetVehicleTransform(vehicle)
    worldSensorPos = TransformToParentPoint(vehicleTransform, origin)
    QueryRejectVehicle(vehicle)
    hit, dist = QueryRaycast(worldSensorPos, direction, 1.8, 0.25)
    return hit, dist
end

function GetMaxSensorDistance()
    h1, d1 = SensorCheck(sensorFrontFarLeftLocalPos, TransformToParentVec(vehicleTransform, Vec(-0.6, 0, -1)))
    h2, d2 = SensorCheck(sensorFrontLeftLocalPos, TransformToParentVec(vehicleTransform, Vec(0, 0, -1)))
    h3, d3 = SensorCheck(sensorFrontRightLocalPos, TransformToParentVec(vehicleTransform, Vec(0, 0, -1)))
    h4, d4 = SensorCheck(sensorFrontFarRightLocalPos, TransformToParentVec(vehicleTransform, Vec(0.6, 0, -1)))
    h5, d5 = SensorCheck(sensorTailFarRightLocalPos, TransformToParentVec(vehicleTransform, Vec(0.6, 0, 1)))
    h6, d6 = SensorCheck(sensorTailRightLocalPos, TransformToParentVec(vehicleTransform, Vec(0, 0, 1)))
    h7, d7 = SensorCheck(sensorTailLeftLocalPos, TransformToParentVec(vehicleTransform, Vec(0, 0, 1)))
    h8, d8 = SensorCheck(sensorTailFarLeftLocalPos, TransformToParentVec(vehicleTransform, Vec(-0.6, 0, 1)))
    if h1 or h2 or h3 or h4 or h5 or h6 or d7 or d8 then
        min = math.min(d1,math.min(d2,math.min(d3,math.min(d4,math.min(d5, math.min(d6, math.min(d7, d8)))))))
        frontHit = false
        tailHit = false
        if h1 or h2 or h3 or h4 then
            frontHit = true
        end
        if h5 or h6 or h7 or h8 then
            tailHit = true
        end
        return frontHit, tailHit, min
    end
    return false, false, 0
end

function tick()
    if GetPlayerVehicle() == vehicle then
        inVehicle = true
        if InputPressed("b") then
            enabled = not enabled
            local pitch = 1.5
            if enabled then
                pitch = 0.5
            end
            PlaySound(sensorChimeOneShot, GetVehicleTransform(vehicle).pos, 0.55, pitch)
        end
        if enabled then
            local t = GetVehicleTransform(vehicle)
            local vel = TransformToLocalVec(t, GetBodyVelocity(GetVehicleBody(vehicle)))
            local speed = math.abs(-vel[3])
            if speed < 9 then
                frontHit, tailHit, dist = GetMaxSensorDistance()
                if frontHit or tailHit then
                    PlayLoop(sensorChime, GetVehicleTransform(vehicle).pos, 0.8)
                    if dist < 0.3 then
                        if (GetSoundLoopProgress(sensorChime) > 0.09) then
                            SetSoundLoopProgress(sensorChime, 0)
                        end
                        if speed > 0.1 and not (InputDown("up") or InputDown("down")) then
                            DriveVehicle(vehicle, 0, 0, true)
                        end
                    elseif dist < 0.6 then
                        if (GetSoundLoopProgress(sensorChime) > 0.15) then
                            SetSoundLoopProgress(sensorChime, 0)
                        end
                    elseif dist < 0.9 then
                        if (GetSoundLoopProgress(sensorChime) > 0.4) then
                            SetSoundLoopProgress(sensorChime, 0)
                        end
                    elseif dist < 1.2 then
                        if (GetSoundLoopProgress(sensorChime) > 0.65) then
                            SetSoundLoopProgress(sensorChime, 0)
                        end
                    elseif dist < 1.5 then
                        if (GetSoundLoopProgress(sensorChime) > 0.85) then
                            SetSoundLoopProgress(sensorChime, 0)
                        end
                    end
                end
            end
        end
    else
        inVehicle = false
    end
end


function draw()
    if not GetBool("hud.disable") and inVehicle then
        --Hint
        UiPush()
            UiTranslate(UiWidth()-140-20, UiHeight()-334-UiYOffset)
            UiAlign("top right")
            UiPush()
                UiAlign("center middle")
                UiPush()
                    UiColor(0,0,0,0.5)
                    UiImageBox("ui/common/box-solid-6.png", 280, 70, 6, 6)
                    UiTranslate(0, -7)
                    UiFont("regular.ttf", 27)
                    UiColor(1,1,1,1)
                    if enabled then
                        UiText("Parking Sensor: Active")
                    else
                        UiText("Parking Sensor: Disabled")
                    end
                    UiTranslate(0, 25)
                    UiColor(1, 1, 1, 0.3)
                    UiText("Press B to toggle")
                UiPop()
            UiPop()
        UiPop()
        --Icon
        if enabled and (h1 or h2 or h3 or h4 or h5 or h6 or h7 or h8) then
            UiPush()
                UiTranslate(UiWidth()-113-20, UiHeight()-590-UiYOffset)
                UiAlign("top right")
                UiPush()
                    UiAlign("center middle")
                    UiPush()
                        UiColor(0,0,0,0.5)
                        UiImageBox("ui/common/box-solid-6.png", 226, 406, 6, 6)
                        UiColor(1,1,1,1)
                        UiImage(imgPath .. "/main.png")
                        if h1 then
                            drawSection(1, d1)
                        end
                        if h2 then
                            drawSection(2, d2)
                        end
                        if h3 then
                            drawSection(3, d3)
                        end
                        if h4 then
                            drawSection(4, d4)
                        end
                        UiRotate(180)
                        if h5 then
                            drawSection(1, d5)
                        end
                        if h6 then
                            drawSection(2, d6)
                        end
                        if h7 then
                            drawSection(3, d7)
                        end
                        if h8 then
                            drawSection(4, d8)
                        end
                    UiPop()
                UiPop()
            UiPop()
        end
    end
end

function drawSection(sectionNumber, dist)
    if dist < 0.6 then
        UiImage(imgPath .. "/h" .. sectionNumber .. "_d5.png")
    elseif dist < 0.9 then
        UiImage(imgPath .. "/h" .. sectionNumber .. "_d4.png")
    elseif dist < 1.2 then
        UiImage(imgPath .. "/h" .. sectionNumber .. "_d3.png")
    elseif dist < 1.5 then
        UiImage(imgPath .. "/h" .. sectionNumber .. "_d2.png")
    else
        UiImage(imgPath .. "/h" .. sectionNumber .. "_d1.png")
    end
end