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

public class MainPanel : BasePanel
{
    protected override void Awake()
    {
        base.Awake();
        EventCenter.Getinstance().AddUpdateListener("RegisterPanel", () => { UIMgr.Getinstance().RegisterPanel<MainPanel>(this); });
    }

    private string currentSongId = "0";
    private int nextSongId = 0;
    private List<string> notesList = null;
    private int playNoteIndex = 0;
    private bool IsSuccess = false;

    public override void OnHide(object data = null)
    {

    }

    public override void OnInit(object data = null)
    {
        EventCenter.Getinstance().AddUpdateListener("FlyNote", AddSongProgress);
        GetHomeInfo();
        GetNextNoteList(nextSongId);
    }

    public override void OnRefresh(object data = null)
    {

    }
    public void GetHomeInfo()
    {
        HttpTool.Instance._Get("app/v1/watermelon/info", null, new Action<WatermelonInfoBean>((result) =>
        {
            GetControl<Text>("CashNumTv").text = "¥" + result.available;
            GetControl<Text>("FruitNumTv").text = result.count + "个";
            GetControl<Button>("MyMusic").onClick.AddListener(() => { UIMgr.Getinstance().ShowPanel<MusicPanel>(E_Layer.lower_top); });
        }), new Action<string, string>((code, msg) =>
        {
            Debug.unityLogger.Log("首页信息 " + code + " msg " + msg);
        }));
    }

    //监听碰撞时间增加音符
    private void AddSongProgress()
    {
        if (IsSuccess)
        {
            return;
        }
        if (notesList != null && notesList.Count > 0)
        {
            if (notesList.Count > playNoteIndex)
            {
                AudioManager.Getinstance().PlayAudio(notesList[playNoteIndex]);
                playNoteIndex++;
            }

            if (playNoteIndex == notesList.Count)
            {
                GetNoteCompleted();
                IsSuccess = true;
                Debug.unityLogger.Log("合成音符红包");
                //UIMgr.Getinstance().ShowPanel<MusicRedBagPanel>(E_Layer.mid);
            }

            GetControl<Text>("ProgressTv").text = playNoteIndex + "/" + notesList.Count;
            GetControl<Image>("ProgressBar").fillAmount = playNoteIndex / (float)notesList.Count;
        }
    }



    /// <summary>
    /// 获取接下来将要掉落的音符组及所属歌曲
    /// </summary>
    public void GetNextNoteList(int song_id)
    {
        //Dictionary<string, object> paras = new Dictionary<string, object>();
        //paras.Add("song_id", song_id);
        //HttpTool.Instance._Get("app/v1/watermelon/notes", paras, new Action<List<string>>((result) =>
        //{
        //    IsSuccess = false;
        //    playNoteIndex = 0;
        //    notesList = result;
        //    GetControl<Text>("ProgressTv").text = "0/" + result.Count;
        //    GetControl<Image>("ProgressBar").fillAmount = 0 / (float)result.Count;
        //    Debug.unityLogger.Log("");
        //}), new Action<string, string>((code, msg) =>
        //{
        //    Debug.unityLogger.Log("获取下一组生成的球 " + code + " msg " + msg);
        //}));
        Dictionary<string, object> paras = new Dictionary<string, object>();
        paras.Add("song_id", song_id);
        HttpTool.Instance._Get2("app/v1/watermelon/notes", paras, new Action<Response<List<string>>>((result) =>
        {
            currentSongId = result.result.song_id;
            nextSongId = result.result.next_song_id;
            IsSuccess = false;
            playNoteIndex = 0;
            notesList = result.result.data;
            GetControl<Text>("ProgressTv").text = "0/" + result.result.data.Count;
            GetControl<Image>("ProgressBar").fillAmount = 0 / (float)result.result.data.Count;
            Debug.unityLogger.Log("");
        }), new Action<string, string>((code, msg) =>
        {
            Debug.unityLogger.Log("获取下一组生成的球 " + code + " msg " + msg);
        }));
    }

    /// <summary>
    /// 合成歌曲上报
    /// </summary>
    /// <param name="song_id"></param>
    public void GetNoteCompleted()
    {
        Dictionary<string, object> paras = new Dictionary<string, object>();
        paras.Add("song_id", currentSongId);
        HttpTool.Instance._Get("app/v1/watermelon/note_completed", paras, new Action<EmptyBean>((result) =>
        {

        }), new Action<string, string>((code, msg) =>
        {
            Debug.unityLogger.Log("合成歌曲上报 " + code + " msg " + msg);
        }));
    }
}
