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

public class MainPanel : BasePanel
{
    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)
    {

    }
    private 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)
            {
                IsSuccess = true;
                Debug.unityLogger.Log("合成音符红包");
            }

            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);
        }));
    }

}
