using System.Collections;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;

public class RopeRumbling : MonoBehaviour
{
    [SerializeField] private float RopeRubleTolerance = 0.5f;
    [SerializeField] private float MaxVibration = 0.8f;

    private PlayerInput pInput;
    private Gamepad pad;

    // Rope
    [SerializeField] private RopeSimulator rope;

    private void Start()
    {
        Invoke("LateStart", 0.1f);
    }

    void LateStart()
    {
        pInput = GetComponent<PlayerInput>();
        pad = Gamepad.all.ElementAtOrDefault(pInput.PlayerNum);
        if (pad == null)
        {
            this.enabled = false;
        }
        else
        {
            
        }

        hasInit = true;
    }

    bool hasInit = false;
    private void Update()
    {
        if (!hasInit) return;

        float ropeClamed = Mathf.Max(0, rope.Overshoot);
        float sensitive_mapped = ropeClamed.Remap(0.2f, 1f, 0f, MaxVibration);
        float mapped = ropeClamed.Remap(0.9f, 1f, 0f, MaxVibration);
        RumbleManager.StartRumble(-1, 0, sensitive_mapped, 0.1f);
    }
}