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

public class AudioManager : MonoBehaviour
{
    public AudioClip OnWireHeadUnPlugged;
    public AudioClip OnWireHeadPicked;
    public AudioClip OnWireHeadDropped;
    public AudioClip OnWireHeadPlugInvalid;
    public AudioClip OnWireHeadPlugValid;
    public AudioClip EmptyWireSocketWhiteNoise;
    public AudioClip GenericClick;
    public AudioClip EndOfTutorial;

    public AudioClip TransmissionVoiceStartEffect;
    public AudioClip TransmissionVoiceDropEffect;

    public AudioSource TransmissionVoiceAudioSource;
    public AudioSource ImmediateFeedbackVoiceAudioSource;
    public AudioSource BGMAudioSource;
    public AudioSource TutorialAudioSource;

    public AudioSource TapRollingAudioSource;

    private Coroutine TransmissionVoiceOverEffectCoroutine;
    private Coroutine TransmissionVoiceOverRollingEffectCoroutine;

    public Animator TapeMachineAnimator;

    public static AudioManager Instance
    {
        get
        {
            return _instance;
        }
    }
    private static AudioManager _instance;

    void Awake()
    {
        TapeMachineAnimator.speed = 0;
        _instance = FindObjectOfType<AudioManager>();
        if (_instance == null)
        {
            var newObj = new GameObject();
            newObj.name = "AudioManager";
            newObj.AddComponent<AudioManager>();
            _instance = FindObjectOfType<AudioManager>();
        }
    }

    public void PlayBGM(AudioClip clip)
    {
        if (clip == null)
        {
            return;
        }

        BGMAudioSource.Stop();
        BGMAudioSource.clip = clip;
        BGMAudioSource.Play();
    }

    public void PlayImmediateFeedBackVoice(AudioClip clip)
    {
        if (clip == null)
        {
            return;
        }

        ImmediateFeedbackVoiceAudioSource.Stop();
        ImmediateFeedbackVoiceAudioSource.clip = clip;
        ImmediateFeedbackVoiceAudioSource.Play();
    }

    public void PlayTransmissionVoice(AudioClip clip)
    {
        if (clip == null)
        {
            return;
        }

        if (TransmissionVoiceOverRollingEffectCoroutine != null)
        {
            TapRollingAudioSource.Stop();
            TapeMachineAnimator.speed = 0;
            StopCoroutine(TransmissionVoiceOverRollingEffectCoroutine);
        }

        if (TransmissionVoiceOverEffectCoroutine != null)
        {
            StopCoroutine(TransmissionVoiceOverEffectCoroutine);
        }

        TransmissionVoiceAudioSource.Stop();
        if (TransmissionVoiceStartEffect != null)
        {
            PlaySoundEffect(TransmissionVoiceStartEffect);
            TransmissionVoiceAudioSource.clip = clip;
            TransmissionVoiceAudioSource.PlayDelayed(TransmissionVoiceStartEffect.length);
            if (TransmissionVoiceDropEffect != null)
            {
                TransmissionVoiceOverEffectCoroutine = 
                    StartCoroutine(DelayTransmissionDropEffect(TransmissionVoiceStartEffect.length + clip.length, TransmissionVoiceDropEffect));
                TransmissionVoiceOverRollingEffectCoroutine =
                StartCoroutine(StartRollingTapSound(TransmissionVoiceStartEffect.length));
            }
        }
        else
        {
            TransmissionVoiceAudioSource.clip = clip;
            TransmissionVoiceAudioSource.Play();
            TransmissionVoiceOverEffectCoroutine =
                     StartCoroutine(DelayTransmissionDropEffect(clip.length, TransmissionVoiceDropEffect));
            TransmissionVoiceOverRollingEffectCoroutine =
                 StartCoroutine(StartRollingTapSound(0f));
        }
    }

    IEnumerator StartRollingTapSound(float delayTime)
    {
        TapeMachineAnimator.speed = 1;
        yield return new WaitForSeconds(delayTime);
        TapRollingAudioSource.Play();
        while (TransmissionVoiceAudioSource.isPlaying)
        {
            yield return new WaitForSeconds(Time.deltaTime);
        }
        TapRollingAudioSource.Stop();
        TapeMachineAnimator.speed = 0;
    }

    IEnumerator DelayTransmissionDropEffect (float delayTime, AudioClip clip)
    {
        yield return new WaitForSeconds(delayTime);
        PlaySoundEffect(clip);
    }

    public void StopTransmissionVoice()
    {
        TransmissionVoiceAudioSource.Stop();
    }

    public void PlaySoundEffect(AudioClip clip, float volume = 1f)
    {
        if (clip == null)
        {
            return;
        }

        AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position, volume);
    }

    public void VolumnUp()
    {
        
    }

    public void VolumnDown()
    {

    }
}
