using UnityEngine;
using System.Collections;

public class FlashPill : Pill {

	private float colorTime = 0.15f;
	private float timeLeft;
	
	private float targetAngle;
	
	private Camera camera;

	private Transform quad;

	private Color prevColor;
	private Color nextColor;
	
	public override void attach(World world, Camera camera, Owl owl) {
		this.camera = camera;
		quad = Instantiate (owl.flashQuadPrefab) as Transform;
		timeLeft = colorTime;
		prevColor = quad.renderer.material.color;
		nextColor = randColor ();
	}

	private Color randColor() {
		return new Color(Random.value, Random.value, Random.value, 0.5f);
	}

	public override void update(float dt, float totalTimeLeft) {
		timeLeft -= dt;
		if (timeLeft <= 0.0f) {
			prevColor = nextColor;
			nextColor = randColor ();
			timeLeft += colorTime;
		}
		quad.renderer.material.color = new Color(
			Interpolate.EaseInOutCubic(prevColor.r, nextColor.r - prevColor.r, colorTime - timeLeft, colorTime),
			Interpolate.EaseInOutCubic(prevColor.g, nextColor.g - prevColor.g, colorTime - timeLeft, colorTime),
			Interpolate.EaseInOutCubic(prevColor.b, nextColor.b - prevColor.b, colorTime - timeLeft, colorTime),
			0.5f);
		quad.position = new Vector3(camera.transform.position.x, camera.transform.position.y - 10, camera.transform.position.z);
	}
	
	public override Color getColor() {
		return Color.green;
	}

	public override void destroy() {
		Destroy (quad.gameObject);
	}
}
