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

public class ToastManager : MonoBehaviour
{
    private static ToastManager Instance;

    public static ToastManager GetInstance()
    {
        return Instance;
    }

    private void Awake()
    {
        Instance = this;
    }

    public GameObject ToastPrefab;
    public List<ToastHandler> ToastList = new List<ToastHandler>();

    //private float Timer = 0;
    public float Interval;

    public Transform Parent;

    GameObject Toa;
    //通过这里来创建Toast预制体
    public void CreatToast(string str)
    {
        if (Toa == null)
        {
            Toa = Instantiate(ToastPrefab, Parent.transform.position, Parent.transform.rotation, Parent.transform);
            //Toa.transform.parent = GameObject.FindGameObjectWithTag("toast").transform;
            var comp = Toa.GetComponent<ToastHandler>();
            ToastList.Insert(0, comp);
            comp.InitToast(str, () => {
                ToastList.Remove(comp);
            });
            //有新的Toast出现，之前的Toast向上移动
            ToastMove(0.2f);
        }
        //Timer = 0;
       
    }

    public void ToastMove(float speed)
    {
        for (int i = 0; i < ToastList.Count; i++)
        {
            ToastList[i].Move(speed, i + 1);
        }
    }
}
