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

public class Burst : MonoBehaviour {

	public Transform ShardPrefab;
	public int Count;
	public float ArcDeg;
	public float SpredDist;
	public float Speed;
	public float Duration;

	private List<float> timeline = new List<float>();
	private List<float> angles = new List<float>();

	private bool bursted=false;
	private GameController gc;
	
	void Start () {
		GameObject go = GameObject.Find ("GameController");
		gc = go.GetComponent<GameController>();

		float start = (transform.localEulerAngles.z+90) - ArcDeg/2;

		for(int i=0; i<Count; i++)
		{
            float t = Time.fixedTime + Duration * Random.Range(0f, 1.0f);
			timeline.Add (t);

			float angle = start + ArcDeg*((float)i/(float)Count);
			angles.Add(angle);
		}

	}
	
	void FixedUpdate () 
	{

		if(timeline.Count <= 0)
		{
			GameObject.Destroy(gameObject);
		}

		foreach(float t in timeline.ToArray())
		{
			if(Time.fixedTime > t)
			{
				BurstOne(ListHelper.RemoveFromAtRandom(angles));
				timeline.Remove(t);
			}
		}

		/*if(!bursted)
		{
			DoBurst ();
			bursted = true;
			gc.CurrentLevel.Transforms.Remove (transform);
			GameObject.Destroy(gameObject);
		}
		*/
	}

	private void BurstOne(float angle)
	{
		//  convert to deltapos
		Vector2 deltaPos = PosFromAngle(angle);
	
		Transform shard = (Transform)GameObject.Instantiate (ShardPrefab);
		//  mult by SpredDist to set position
		shard.position = transform.position + ((Vector3)deltaPos).normalized * SpredDist;
		//  assign rotation
		shard.localEulerAngles = new Vector3(0,0, AngleFromDelta(deltaPos));
		//  if rb2d addForce 
		if(shard.GetComponent<Rigidbody2D>() != null)
		{
			Vector2 force = deltaPos.normalized * Speed * shard.GetComponent<Rigidbody2D>().mass/Time.fixedDeltaTime;
			shard.GetComponent<Rigidbody2D>().AddForce(force);
		}

	}

	public void DoBurst()
	{
		float start = (transform.localEulerAngles.z+90) - ArcDeg/2;

		for(int i=0; i<Count; i++)
		{
			// foreach shard
			Transform shard = (Transform)GameObject.Instantiate (ShardPrefab);
			//  calculate angle
			float angle = start + ArcDeg*((float)i/(float)Count);
			//  convert to deltapos
			Vector2 deltaPos = PosFromAngle(angle);

			//  mult by SpredDist to set position
			shard.position = transform.position + ((Vector3)deltaPos).normalized * SpredDist;
			//  assign rotation
			shard.localEulerAngles = new Vector3(0,0, AngleFromDelta(deltaPos));
			//  if rb2d addForce 
			if(shard.GetComponent<Rigidbody2D>() != null)
			{
				Vector2 force = deltaPos.normalized * Speed * shard.GetComponent<Rigidbody2D>().mass/Time.fixedDeltaTime;
				shard.GetComponent<Rigidbody2D>().AddForce(force);
			}

		}
	}

	private Vector2 PosFromAngle(float rot)
	{
		float rads = rot * Mathf.PI/180;
		Vector2 pos = new Vector3(Mathf.Cos(rads), Mathf.Sin(rads));
		return pos;
	}

	private float AngleFromDelta(Vector2 deltaPos)
	{
		float dx = deltaPos.x;
		float dy = deltaPos.y;
		float aimangle = Mathf.Atan2(dx, dy) * -180/Mathf.PI;
		return aimangle;
	}
}
