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;
        }

        //StartCoroutine(RumbleLoop());

        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.5f, 1f, 0f, MaxVibration);
        RumbleManager.StartRumble(-1, mapped, sensitive_mapped, 0.1f);

        //float ropeClamed = Mathf.Max(0, rope.Overshoot);
        //float sensitive_mapped = ropeClamed.Remap(0.2f, 0.8f, 0f, MaxVibration);
        //float mapped = ropeClamed.Remap(0.9f, 1f, 0f, MaxVibration);
        //rumbleStrength = sensitive_mapped;
    }

    //float rumbleStrength = 0;
    //private IEnumerator RumbleLoop()
    //{
    //    while (true)
    //    {
    //        if (rumbleStrength > 0.1f)
    //        {
    //            RumbleManager.StartRumble(-1, 0, rumbleStrength, 0.025f);
    //        }
    //        yield return new WaitForSecondsRealtime(0.2f);
    //    }
    //}
}