using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class WheelController : MonoBehaviour
{
    [SerializeField] private Wheel frontRightWheel;
    [SerializeField] private Wheel frontLeftWheel;
    [SerializeField] private Wheel rearRightWheel;
    [SerializeField] private Wheel rearLeftWheel;

    public float acceleration = 500f;
    public float breakingForce = 300f;
    [Range(15f,75f)] public float maxTurnAngle;

    private float currentAcceleration = 0f;
    private float currentBrakeForce = 0f;
    private float currentTurnAngle = 0f;

    private DebugInfo debugInfo;

    //inputs
    private InputMaster controls;
    private InputAction movement;
    private InputAction turn;
    private InputAction brake;

    private void Awake() {
        debugInfo = FindObjectOfType<DebugInfo>();
        controls = new InputMaster();
    }

    private void Update() {
        debugInfo.setRpmLabels(frontLeftWheel.GetWheelRPM(), frontRightWheel.GetWheelRPM(), rearLeftWheel.GetWheelRPM(), rearRightWheel.GetWheelRPM());
    }

    private void FixedUpdate() {
        //TODO: Update input info to new input system

        //forward reverse input
        //currentAcceleration = acceleration * Input.GetAxis("Vertical");
        

        //breaking input
        /*if(Input.GetKey(KeyCode.Space)){
            currentBrakeForce = breakingForce;
        }else{
            currentBrakeForce = 0f;
        }*/

        //apply acceleration to front wheels| (this determines which wheel drive is the car (fwd, awd, rwd))
        rearRightWheel.ApplyAcceleration(currentAcceleration);
        rearLeftWheel.ApplyAcceleration(currentAcceleration);

        //break applies to all four wheels
        frontLeftWheel.ApplyBrakeForce(currentBrakeForce);
        frontRightWheel.ApplyBrakeForce(currentBrakeForce);
        rearLeftWheel.ApplyBrakeForce(currentBrakeForce);
        rearRightWheel.ApplyBrakeForce(currentBrakeForce);

        //steering
        //currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal"); //getting steering input
        frontLeftWheel.ApplySteerAngle(currentTurnAngle);
        frontRightWheel.ApplySteerAngle(currentTurnAngle);

        //update wheel meshes
        frontLeftWheel.UpdateWheel();
        frontRightWheel.UpdateWheel();
        rearLeftWheel.UpdateWheel();
        rearRightWheel.UpdateWheel();
    }
}