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

public class RayBehaviour : MonoBehaviour {

//	public RayoManager rm;
//	public int rayNumber;

	public Animator animator;
//	public Renderer rend;
	public RayoManager rm;
    public AnimationCurve magicCurve;

    public float timeInterval;
	public float minDifficulty, maxDifficulty;
    public float magicTimer;    

	void Awake () {
		timeInterval = 1f;
//		rend = GetComponent<Renderer> ();
		rm = transform.parent.GetComponent<RayoManager> ();
		animator = gameObject.GetComponent<Animator> ();
		//animator.speed = Random.Range (minDifficulty, maxDifficulty);

        //Congelamos para crear LA MAGIA
        animator.speed = 0;
        magicTimer = 0f;
        foreach (Collider c in GetComponentsInChildren<Collider>())
        {
            c.enabled = false;
        }
	}

    void Update()
    {
        if(animator.speed == 0)
        {
            magicTimer += Time.deltaTime;
            foreach(LineRenderer lr in GetComponentsInChildren<LineRenderer>())
            {
                lr.startColor = new Color(1f, 1f, 1f, magicCurve.Evaluate(magicTimer));
                lr.endColor = new Color(1f, 1f, 1f, magicCurve.Evaluate(magicTimer));
            }

            if(magicTimer > 3f)
            {
                foreach (Collider c in GetComponentsInChildren<Collider>())
                {
                    c.enabled = true;
                }

                foreach (LineRenderer lr in GetComponentsInChildren<LineRenderer>())
                {
                    lr.startColor = Color.white;
                    lr.endColor = Color.white;
                }

                animator.speed = Random.Range(minDifficulty, maxDifficulty);
				TheAudioManager.instance.PlayLongElectricity ();
            }            
        }
    }
		
// This is called from the animation
	public void EndAnimation(){
		if (!gameObject.CompareTag ("rayoprueba")) {
//			rend.enabled = false;
			Invoke ("DestroyThis", timeInterval);
		}
		// wait a second before destroying

	}

	public void DestroyThis(){
		rm.CreateWave ();
		TheAudioManager.instance.sfxLongElectricity.Stop ();
		Destroy (gameObject);
	}
}
