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

public class MusicMgr : BaseMgr<MusicMgr>
{
    public bool Mute = false;

    public GameObject BKMusic;
    public AudioSource BKMusicAudioSource;

    private GameObject Music;
    private List<AudioSource> audiosourcelist=new List<AudioSource>();

    private GameObject DragingMusic;

    public MusicMgr() 
    {
        MonoMgr.Getinstance().AddUpdateListener(DelateMusic);
    }

    //播放背景音乐API
    public void PlayBKMusic(string path)
    {
        if (BKMusic == null)
        {
            BKMusic = new GameObject("BKMusic");
            GameObject.DontDestroyOnLoad(BKMusic);
            BKMusicAudioSource = BKMusic.AddComponent<AudioSource>();
        }
        ResMgr.Getinstance().LoadAsync<AudioClip>(path, (clip) =>
        {
            BKMusicAudioSource.clip = clip;
            BKMusicAudioSource.loop = true;

            BKMusicAudioSource.volume = Mute? 0 : 1;
            BKMusicAudioSource.Play();
        });
    }

    public void StopBKMusic() 
    {
        if (BKMusic!=null)
        {
            AudioSource source = null;
            BKMusic.transform.TryGetComponent(out source);
            if (source!=null)
            {
                source.Stop();
            }
        }
    }

    //播放音乐API
    public void PlayMusic(string path)
    {
        if (Music == null)
        {
            Music = new GameObject("Music");
            GameObject.DontDestroyOnLoad(Music);
        }
        AudioClip AC = ResMgr.Getinstance().Load<AudioClip>(path);

        AudioSource AS = Music.AddComponent<AudioSource>();
        audiosourcelist.Add(AS);
        AS.clip = AC;
        AS.loop = false;
        AS.volume = Mute ? 0 : 1;
        AS.Play();
    }

    //内置删除音乐组件方法
    private void DelateMusic()
    {
        for (int i = 0; i < audiosourcelist.Count; i++)
        {
            if (audiosourcelist[i].isPlaying == false)
            {
                GameObject.Destroy(audiosourcelist[i]);
                audiosourcelist.RemoveAt(i);
            }
        }
    }

    //静音/取消静音（全局）方法
    public void UpdateMusicVolume(bool Mute)
    {
        this.Mute = Mute;

        if (BKMusicAudioSource != null && BKMusicAudioSource.clip != null)
        {
            BKMusicAudioSource.volume = Mute ? 0 : 1;
        }

        if (DragingMusic && DragingMusic.GetComponent<AudioSource>())
        {
            BKMusicAudioSource.volume = Mute ? 0 : 1;
        }

        for (int i = 0; i < audiosourcelist.Count; i++)
        {
            audiosourcelist[i].volume = Mute ? 0 : 1;
        }
    }

    public void Clear()
    {
        audiosourcelist = new List<AudioSource>();
    }

    public void PlayDragingMusic()
    {
        if (!DragingMusic)
        {
            DragingMusic = new GameObject("DragingMusic");
            GameObject.DontDestroyOnLoad(DragingMusic);
            DragingMusic.AddComponent<AudioSource>();
        }

        var AS = DragingMusic.GetComponent<AudioSource>();

        if (AS.isPlaying)
            return;
        
        AS.clip = AS.clip ?? ResMgr.Getinstance().Load<AudioClip>("Audio/Draging");

        AS.volume = Mute ? 0 : 1;

        AS.loop = true;

        AS.Play();
    }

    public void StopDragingMusic() 
    {
        if (DragingMusic)
        {
            DragingMusic.GetComponent<AudioSource>().Pause();
        }
    }
}
