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

public class LevelUpParticles : MonoBehaviour {
	
	public ParticleSystem subParticlePrefab;
	public ParticleSystem burstParticlePrefab;
	
	public Color[] runeColors;
	float deathTimer;
	float vertVel;
	List<Transform> particleXforms;

	void Start () {
		deathTimer = -1;
		vertVel = 10f;
		particleXforms = new List<Transform>();
		Vector3 spawnPos = transform.position + new Vector3(0,-1,0);
		float angle = 0;
		float deltaAngle = 360f/runeColors.Length;
				
		foreach (Color c in runeColors)
		{
			ParticleSystem ps = Instantiate<ParticleSystem>(subParticlePrefab);
			ps.transform.position = spawnPos + Quaternion.Euler (0,angle,0) * Vector3.forward * 2f;
			ps.startColor = c;
			ps.transform.SetParent(transform);
			particleXforms.Add (ps.transform);
			angle += deltaAngle;
		}

	}
	
	// Update is called once per frame
	void Update () {

		vertVel -= Time.deltaTime * 10f;
		if (vertVel < -10) vertVel = -10f;
		transform.Rotate(new Vector3(0,(180+(vertVel*10))*Time.deltaTime,0));
		float mag = 0;
		foreach (Transform t in particleXforms)
		{
			t.localPosition = (t.localPosition * (1f - (1.4f * Time.deltaTime))) + new Vector3(0,vertVel*Time.deltaTime,0);
			mag = t.localPosition.magnitude;
		}
		if ((mag < 0.3f) && (deathTimer < 0))
		{
			foreach (ParticleSystem ps in GetComponentsInChildren<ParticleSystem>())
				ps.enableEmission = false;
			/*
			ParticleSystem burst = Instantiate<ParticleSystem> (burstParticlePrefab);
			burst.transform.position = transform.position;
			burst.transform.SetParent(transform);
			burst.startColor = Color.white;*/
			deathTimer = 6;
		}
		if (deathTimer > 0)
		{
			deathTimer -= Time.deltaTime;
			if (deathTimer <= 0)
				Destroy (gameObject);
		}
	}
}
