using Sandbox;
using Sandbox.VR;
using System;
using VeloX.Utils;

namespace VeloX;


public partial class VeloXCar
{
	public bool ESCActive { get; private set; } = true;
	public static bool UseESC = true;
	private void UpdateESC()
	{
		ESCActive = false;

		if ( !UseESC )
			return;

		if ( TotalSpeed < 100 || CarDirection != 1 )
			return;

		float angle = Body.Velocity.SignedAngle( WorldRotation.Forward, WorldRotation.Up ); ;
		angle -= SteerAngle.yaw * 0.5f;
		float absAngle = angle < 0 ? -angle : angle;

		if ( Engine.RevLimiterActive || absAngle < 2f )
			return;

		foreach ( var wheel in Wheels )
		{
			if ( !wheel.IsOnGround )
				continue;


			float additionalBrakeTorque = -angle * Math.Sign( wheel.LocalPosition.y ) * 20f;
			if ( additionalBrakeTorque > 0 )
			{
				ESCActive = true;
				wheel.BrakeTorque += additionalBrakeTorque;
			}

		}
	}
}
