using UnityEngine;

public class ControlInputs : MonoBehaviour
{
    string horizontalInput = "Horizontal";
    string verticalInput = "Vertical";

    string rightHorizontalInput = "Joy X";
    string rightVerticalInput = "Joy Y";

    string rightTriggerInput = "RightTrigger";
    string leftTriggerInput = "LeftTrigger";

    public bool IsReady { get; private set; }
    int playerId;

    public Vector2 InputVector { get; private set; }
    public Vector2 RightJoystickInputVector { get; private set; }

    public bool RightTriggerPressed { get; set; }
    public bool LeftTriggerPressed { get; set; }

    public void InitControls(int _playerId)
    {
        _playerId++;
        playerId = _playerId;

        if (Input.GetJoystickNames().Length != 0)
        {
            horizontalInput += _playerId.ToString();
            verticalInput += _playerId.ToString();

            rightHorizontalInput += _playerId.ToString();
            rightVerticalInput += _playerId.ToString();

            rightTriggerInput += _playerId.ToString();
            leftTriggerInput += _playerId.ToString();
           
        }

        IsReady = true;
    }

    private void Update()
    {
        if(IsReady == true)
        {
            InputVector = new Vector2(Input.GetAxisRaw(horizontalInput), Input.GetAxisRaw(verticalInput));
            RightJoystickInputVector = new Vector2(Input.GetAxis(rightHorizontalInput), Input.GetAxis(rightVerticalInput));

            CheckTriggers();
        }
        
    }

    void CheckTriggers()
    {

        if (RightTriggerPressed == false && Input.GetAxis(rightTriggerInput) > 0.5f)
        {
            RightTriggerPressed = true;
        }
        else if (Input.GetAxis(rightTriggerInput) < 0.35f)
        {
            RightTriggerPressed = false;
        }

        if (LeftTriggerPressed == false && Input.GetAxis(leftTriggerInput) > 0.5f)
        {
            LeftTriggerPressed = true;
        }
        else if (Input.GetAxis(leftTriggerInput) < 0.35f)
        {
            LeftTriggerPressed = false;
        }
    }


}
