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

public class MusicCell : BasePanel
{
    public SongItemBean bean;

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

    public override void OnInit(object data = null)
    {
        SongItemBean bean = data as SongItemBean;
        this.bean = bean;

        GetControl<Button>("btnPlayMusic").onClick.AddListener(() => 
        {
            MusicPanel panel = UIMgr.Getinstance().GetPanel<MusicPanel>();
            panel.ChangeMusic(this);
        });

        GetControl<Text>("txtNum").text = bean.song_id.ToString();
        GetControl<Text>("txtNum").color = bean.song_id <= 3 ? Color.red : Color.black;

        GetControl<Text>("txtMusicName").text = bean.song_name;

        MonoMgr.Getinstance().StartCoroutine(GetTexture(bean.song_cover, GetControl<Image>("imgMusicPic")));
    }

    public override void OnRefresh(object data = null)
    {
        
    }

    public void OnStartMusic() 
    {
        GetControl<Image>("btnPlayMusic").sprite = Resources.Load<SpriteAtlas>("Atlas/MusicPanel").GetSprite("Start");

        //开始获取音频
        MonoMgr.Getinstance().StartCoroutine(GetAudioClip(bean.song_play,clip=>
        {
            MusicPanel panel = UIMgr.Getinstance().GetPanel<MusicPanel>();

            if (panel == null)
                return;

            //如果当前面板播放的音乐不是自己 
            if (panel.PlayingCell != this)
                return;

            //如果当前正在播放
            if (panel.IsPlayingMusic())
                return;

            panel.PlayMusic(clip);
        }));
    }

    public void OnStopMusic() 
    {
        GetControl<Image>("btnPlayMusic").sprite = Resources.Load<SpriteAtlas>("Atlas/MusicPanel").GetSprite("Pause");

        MusicPanel panel = UIMgr.Getinstance().GetPanel<MusicPanel>();

        panel.StopMusic();
    }

    //设置图片的方法
    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)
            {
                Debug.Log(www.error);
            }
            else
            {
                callback.Invoke(DownloadHandlerAudioClip.GetContent(www));
            }
        }
    }

}
