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

public class AudioManager : D_MonoSingleton<AudioManager>
{
    AudioListener mAudioListener;
    AudioSource mMusicPlayer;
    AudioSource mSoundPlayer;

    private Dictionary<AudioClipType, List<AudioClip>> mAudioDic = new Dictionary<AudioClipType, List<AudioClip>>();

    protected override void Initialize()
    {
        for (int i = 0; i < AudioConfig.Instance.AudioPacks.Count; i++)
        {
            AudioPack tPack = AudioConfig.Instance.AudioPacks[i];
            if (!mAudioDic.ContainsKey(tPack.myType))
            {
                mAudioDic[tPack.myType] = new List<AudioClip>();
            }

            mAudioDic[tPack.myType] = tPack.myClips;
        }

        CheckAudioListner();
        CheckMusicPlayer();
        CheckSoundPlayer();
    }

    private void CheckAudioListner()
    {
        if (mAudioListener == null)
            mAudioListener = gameObject.AddComponent<AudioListener>();
    }

    private void CheckMusicPlayer()
    {
        if (mMusicPlayer == null)
            mMusicPlayer = gameObject.AddComponent<AudioSource>();
    }

    private void CheckSoundPlayer()
    {
        if (mSoundPlayer == null)
            mSoundPlayer = gameObject.AddComponent<AudioSource>();
    }

    //private void Update()
    //{
    //    if (AudioListener.pause && mIsListenerOn)
    //    {
    //        AudioListener.pause = false;
    //    }
    //    else if (!AudioListener.pause && !mIsListenerOn)
    //    {
    //        AudioListener.pause = true;
    //    }
    //}

    public void SetListenerOn(bool pIsOn)
    {
        AudioListener.pause = !pIsOn;
    }

    public void SetMusicOn(bool pIsOn)
    {
        mMusicPlayer.enabled = pIsOn;
    }

    public void SetSoundOn(bool pIsOn)
    {
        mSoundPlayer.enabled = pIsOn;
    }

    public void PlayBGMusic(AudioClipType pType, float pVolumeScale = 1)
    {
        CheckMusicPlayer();

        AudioClip tClip = GetClip(pType);
        if (tClip == null)
            return;

        if (mMusicPlayer.enabled)
        {
            mMusicPlayer.clip = tClip;
            mMusicPlayer.loop = true;
            mMusicPlayer.volume *= pVolumeScale;
            mMusicPlayer.Play();
        }
    }

    public void StopBGMusic()
    {
        CheckMusicPlayer();

        if (mMusicPlayer.enabled)
        {
            mMusicPlayer.Stop();
            mMusicPlayer.clip = null;
        }
    }

    public void PlaySound(AudioClipType pType, float pVolumeScale = 1)
    {
        CheckSoundPlayer();

        AudioClip tClip = GetClip(pType);
        if (tClip == null)
            return;

        if (mSoundPlayer.enabled)
        {
            mSoundPlayer.PlayOneShot(tClip, pVolumeScale);
        }
    }

    public void PlaySound(AudioClip pClip, float pVolumeScale = 1)
    {
        CheckSoundPlayer();

        AudioClip tClip = pClip;
        if (tClip == null)
            return;

        if (mSoundPlayer.enabled)
        {
            mSoundPlayer.PlayOneShot(tClip, pVolumeScale);
        }
    }

    public void PlaySound3D(AudioSource pAudioSrc, AudioClipType pType)
    {
        AudioClip tClip = GetClip(pType);
        if (tClip == null)
            return;

        if (pAudioSrc.enabled)
        {
            pAudioSrc.PlayOneShot(tClip);
        }
    }

    public AudioSource PlaySoundLoop(AudioClipType pType, float pVolumeScale = 1)
    {
        AudioClip tClip = GetClip(pType);
        if (tClip == null)
            return null;

        AudioSource tLoopAudio = gameObject.AddComponent<AudioSource>();
        tLoopAudio.clip = tClip;
        tLoopAudio.loop = true;
        tLoopAudio.volume *= pVolumeScale;
        tLoopAudio.Play();

        return tLoopAudio;
    }

    public void StopSoundLoop(AudioSource pAudioSrc)
    {
        if (pAudioSrc == null)
            return;

        pAudioSrc.Stop();
        Destroy(pAudioSrc);
    }

    private AudioClip GetClip(AudioClipType pType)
    {
        if (!mAudioDic.ContainsKey(pType) || mAudioDic[pType].Count == 0)
            return null;

        List<AudioClip> tClips = mAudioDic[pType];
        int tRandomPlayIndex = UnityEngine.Random.Range(0, tClips.Count);

        return tClips[tRandomPlayIndex];
    }
}