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

[RequireComponent(typeof(SpriteRenderer))]
public class Wave : MonoBehaviour {

	//private fields
	bool fadeIn;
	SoundWave parent;
	float smoothFade;
	float time;
	Collider2D coll;

	//public fields
	public float smoothFadeIn = .3f;
	public float smoothFadeOut = .1f;
	public int waveFactor;
	public SpriteRenderer sprite;


	void Awake() {
		sprite = GetComponent<SpriteRenderer> ();
		coll = GetComponent<Collider2D> (); 
		smoothFade = (float) (10f / (waveFactor * 3)) ;
		Reset ();
	}//end awake

	// Use this for initialization
	void Start () {
		parent = transform.parent.gameObject.GetComponent<SoundWave> ();
	}//end start
	
	// Update is called once per frame
	void Update () {
		if (fadeIn) {
			sprite.color = Color.Lerp (sprite.color, Color.white, time += Time.deltaTime * smoothFadeIn);
			if (sprite.color.a > 0.95) {
				fadeIn = false;
				time = 0f;
			}
		} else {
			sprite.color = Color.Lerp (sprite.color, Color.clear, time += Time.deltaTime * smoothFadeOut * smoothFade);
			if (sprite.color.a < 0.01f){
				coll.enabled = false;
				if (waveFactor == 4) // <--- hardcode here
					parent.Recycle();
			}//end-if
		}//end else
	}//end update

	void OnEnable() {
		Reset ();
	}//end enable

	void Reset() {
		fadeIn = true;
		sprite.color = Color.clear;
		time = 0f;
		coll.enabled = true;
	}//end reset

}
