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

public class SoundComponent : MonoBehaviour
{
    private Transform m_Transform;

    [SerializeField] private float m_StepInterval;
    [SerializeField] private SoundEffect m_FootstepsSoundEffect;
    [Space]
    [SerializeField] private float m_DragInterval;
    [SerializeField] private SoundEffect m_DragSoundEffect;

    private bool m_Moving;
    private Coroutine m_DragCoroutine;

    private void Awake()
    {
        m_Transform = transform;
    }

    private void Start()
    {
        StartCoroutine(FootstepsCoroutine());
    }

    public void SetSpeed(float speed)
    {
        m_Moving = speed > 0;
    }

    public void StartDragging()
    {
        m_DragCoroutine = StartCoroutine(DragCoroutine());
    }

    public void StopDragging()
    {
        StopCoroutine(m_DragCoroutine);
    }

    private IEnumerator FootstepsCoroutine()
    {
        while (Application.isPlaying)
        {
            if (m_Moving)
                m_FootstepsSoundEffect.Play(m_Transform.position);

            yield return new WaitForSeconds(m_StepInterval);
        }
    }

    private IEnumerator DragCoroutine()
    {
        while (Application.isPlaying)
        {
            if (m_Moving)
                m_DragSoundEffect.Play(m_Transform.position);

            yield return new WaitForSeconds(m_DragInterval);
        }
    }
}
