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;
	public AudioClip BurstClip;

	public bool ConvertToPlayerProjectile = false;

	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 * (float)(gc.rand.NextDouble());
			timeline.Add (t);

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

	}
	
	void FixedUpdate () 
	{

		if(timeline.Count <= 0)
		{
			gc.CurrentLevel.Transforms.Remove (transform);
			GameObject.Destroy(gameObject);
		}

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

		if(!bursted)
		{
			if(BurstClip != null)
				iTween.Stab(gc.gameObject, iTween.Hash ("audiosource", gc.ShootingAudio, "audioclip", BurstClip, "volume", 0.4f*gc.Volume));

			bursted = true;
		}

	}

	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.rigidbody2D != null)
		{
			Vector2 force = deltaPos.normalized * Speed * shard.rigidbody2D.mass/Time.fixedDeltaTime;
			shard.rigidbody2D.AddForce(force);
		}

		if(ConvertToPlayerProjectile)
		{
			ProjectileCollision pc = shard.GetComponent<ProjectileCollision>();
			if(pc!=null)
				pc.ConvertProjectile();
		}
	}

	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.rigidbody2D != null)
			{
				Vector2 force = deltaPos.normalized * Speed * shard.rigidbody2D.mass/Time.fixedDeltaTime;
				shard.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;
	}
}
