using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using UnityEngine.UI;

public class MusicRedBagPanel : BasePanel
{

    public AudioSource AS;

    public override void OnHide(object data = null)
    {
        StopMusic();
    }

    public override void OnInit(object data = null)
    {
        AS = GetComponent<AudioSource>();
        GetControl<Button>("imgMusicBK").onClick.AddListener(()=> {

            if (IsPlayingMusic())
            {
                StopMusic();
            }
            else
            {
                PlayMusic(null);
            }
        
        });
        GetControl<Button>("btnOpen").onClick.AddListener(() =>
        {
            UIMgr.Getinstance().HidePanel<MusicRedBagPanel>();
            UIMgr.Getinstance().ShowPanel<MoneyRedBagPanel>(E_Layer.mid);

        });

        GetControl<Button>("btnClose").onClick.AddListener(() =>
        {
            UIMgr.Getinstance().GetPanel<MainPanel>().GetNextNoteList();
            UIMgr.Getinstance().HidePanel<MusicRedBagPanel>();
        });

        Dictionary<string, object> paras = new Dictionary<string, object>();
        paras.Add("song_id", UIMgr.Getinstance().GetPanel<MainPanel>().GetCurrentSongId);
        HttpTool.Instance._Get("app/v1/watermelon/songs_list", paras, new Action<SongBean>((result) =>
        {
            for (int i = 0; i < result.list.Count; i++)
            {
                if (UIMgr.Getinstance().GetPanel<MainPanel>().GetCurrentSongId == result.list[i].song_id)
                {
                    GetControl<Text>("txtMusicName").text = "你合成了歌曲“" + result.list[i].song_name + "”";
                    MonoMgr.Getinstance().StartCoroutine(GetTexture(result.list[i].song_cover, GetControl<Image>("imgMusicPic")));
                    //开始获取音频
                    MonoMgr.Getinstance().StartCoroutine(GetAudioClip(result.list[i].song_play, clip =>
                    {
                       PlayMusic(clip);
                    }));
                    break;
                }
            }

        }), new Action<string, string>((code, msg) =>
        {
            Debug.unityLogger.Log("获取歌曲列表 " + code + " msg " + msg);
        }));
    }

    public override void OnRefresh(object data = null)
    {

    }

    IEnumerator GetTexture(string URL, Image img)
    {
        using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(URL))
        {
            yield return uwr.SendWebRequest();

            if (uwr.isNetworkError || uwr.isHttpError)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                // Get downloaded asset bundle
                Texture2D texture = DownloadHandlerTexture.GetContent(uwr);

                Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
                img.sprite = sp;
            }
        }
    }
    //设置音频的方法
    IEnumerator GetAudioClip(string URL, UnityAction<AudioClip> callback)
    {
        using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(URL, AudioType.WAV))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                callback.Invoke(DownloadHandlerAudioClip.GetContent(www));
            }
        }
    }

    public bool IsPlayingMusic()
    {
        if (AS.clip == null)
            return false;

        return AS.isPlaying;
    }
    public void PlayMusic(AudioClip clip)
    {
        if(null == clip)
        {
            if (AS.clip!=null)
            {
                AS.Play();
            }
            return;
        }
        AS.clip = clip;
        AS.loop = true;
        AS.Play();
    }

    public void StopMusic()
    {
        AS.Pause();
       // AS.clip = null;
    }
}
