using UnityEngine;
using System.Collections;

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

	public float amountUp = 105;

	private void ArcoBegin()
	{
		iTween.Stop(gameObject);

		iTween.ScaleAdd(gameObject, iTween.Hash("amount", ascendAmount, "time", ascendTime,
		"easetype", easeAscend, "delay", ascendDelay, "oncomplete", "Finish", "Space", Space.Self));

//		iTween.ValueTo(gameObject, iTween.Hash("from", 0, "to", 1, "time", ascendTime*0.9f,
//		"easetype", easeAscend, "delay", ascendDelay, "onupdate", "SetAlpha"));

//		iTween.ValueTo(gameObject, iTween.Hash("from", 1, "to", 0, "time", ascendTime*0.9f,
//		"easetype", easeAscend, "delay", ascendDelay, "onupdate", "SetAlpha"));

	}

	private void Finish()
	{
		transform.parent = null;

		print("finish!");
		GetComponentInChildren<SpriteRenderer>().flipX = true;
		transform.position += Vector3.up * amountUp;
		transform.Rotate(Vector3.forward * 180);

		iTween.ScaleAdd(gameObject, iTween.Hash("amount", -ascendAmount, "time", ascendTime,
		"easetype", easeAscend, "delay", 0.5f, "oncomplete", "AscendComplete", "Space", Space.Self));
	}

	public void SetAlpha(float value)
	{
		foreach(var renderer in gameObject.GetComponentsInChildren<SpriteRenderer>())
		{
			float r = renderer.color.r;
			float g = renderer.color.g;
			float b = renderer.color.b;

			renderer.color = new Color(r, g, b, value);
		}
	}
}