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

public sealed class TextEffects : MonoBehaviour {
	[SerializeField]private TextEffectController effectPrefab = null;
	[SerializeField]private List<TextEffectController> effectsPool = new List<TextEffectController>();

	private static TextEffects instance = null;

	public static TextEffects Instance {
		get {
			if (instance == null) {
				instance = Component.FindObjectOfType<TextEffects>();
			}
			if (instance == null) {
				instance = Utils.Create<TextEffects>();
			}
			return instance;
		}
	}

	private void Awake() {
		effectPrefab = Resources.Load<TextEffectController>("Prefabs/TextEffectPrefab");
	}

	public static void Show(Vector3 position, string text, Color textColor) {
		Instance.ShowEffect (position, text, textColor);
	}

	private void ShowEffect (Vector3 position, string text, Color textColor) {
		foreach (var effect in effectsPool) {
			if (!effect.gameObject.activeSelf) {
				effect.Init (position, text, textColor);
				return;
			}
		}
		var newEffect = Instantiate<TextEffectController>(effectPrefab, transform);
		newEffect.Init (position, text, textColor);
		effectsPool.Add(newEffect);
	}
}
