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

public class Cordeiro : MonoBehaviour
{
	public Vector3 ascendAmount;
	public float ascendDelay;
	public float ascendTime;
	public iTween.EaseType easeAscend;

	public AudioClip saveSound;

	private Respawner respawner;

	private Animator animator;

	public static List<Cordeiro> allCordeiros = new List<Cordeiro>();

	public static UnityEvent OnAnyCordeiroRemove = new UnityEvent();

	private void Awake()
	{
		animator = GetComponentInChildren<Animator>();

		allCordeiros.Add(this);
	}

	private void OnTriggerEnter2D(Collider2D other)
	{
		if(other.CompareTag("Player"))
		{
			if(saveSound != null) AudioSource.PlayClipAtPoint(saveSound, Camera.main.transform.position);

			GetComponent<Collider2D>().enabled = false;

			animator.SetTrigger("Got");

			iTween.MoveAdd(gameObject, iTween.Hash("amount", ascendAmount, "time", ascendTime,
			"easetype", easeAscend, "delay", ascendDelay, "oncomplete", "AscendComplete"));

			iTween.FadeTo(gameObject, iTween.Hash("to", 0, "time", ascendTime,
			"easetype", easeAscend, "delay", ascendDelay));

			GetComponent<AudioSource>().Play();
		}
	}

	private void AscendComplete()
	{
		Destroy(gameObject);
	}

	private void OnDestroy()
	{
		allCordeiros.RemoveAll(c => c == this);
		OnAnyCordeiroRemove.Invoke();
	}

	[ContextMenu("Print Cordeiros")]
	private void PrintCordeiros()
	{
		foreach(var c in allCordeiros) print(c.gameObject);
	}
}
