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

public class PlayerShip : MonoBehaviour {

	public Vector2 velocity;
	public float orientation;

	public bool thrusting;

	public float power = .3f;
	public float turnSpeed = 270.0f;

	public float shipMass = 10.0f;

	public float maxDistance = 200.0f;

	public AudioSource rocketIdle;
	public AudioSource rocketMovement;

	public AudioSource bhDeath;

	public AudioSource dangerHit;

	public ParticleSystem warpEffect;

	public ParticleSystem thrustPoop;
	public ParticleSystem dangerSplode;

	private Gameplay gameplay;

	bool dying = false;
	float deathTime = 0.0f;

	// Use this for initialization
	void Start () {
		gameplay = gameObject.GetComponentInParent<Gameplay>();

		velocity = new Vector2(0,0);
		orientation = 0;
		deathTime = 0;
		dying = false;
		transform.position = new Vector3(-50,0,0);
		transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
	}

	public void Restart()
	{
		Start();
	}
	
	// Update is called once per frame
	void Update () {

		if (dying)
		{
			if (deathTime <= 1.0f)
			{
				GetComponentInParent<Gameplay>().EndGame();
				return;
			}
			else
			{

				float deathFactor = (deathTime - 1.0f) / 4.5f;
				transform.localScale = new Vector3((Random.value + 0.5f) * deathFactor, (Random.value + 0.5f) * deathFactor, 1.0f);
			}

			deathTime -= Time.deltaTime;
			return;
		}

		thrusting = Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.UpArrow);
		if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.LeftArrow))
		{
			orientation += turnSpeed * gameplay.timeMultipler * Time.deltaTime;
		}
		if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.RightArrow))
		{
			orientation -= turnSpeed * gameplay.timeMultipler * Time.deltaTime;
		}
			
		if (thrusting)
			{
				float factor = power * gameplay.timeMultipler * Time.deltaTime;
				float Degrees2Pi = Mathf.PI / 180.0f;
				Vector2 acceleration = new Vector2(-Mathf.Sin(orientation * Degrees2Pi) * factor, Mathf.Cos(orientation * Degrees2Pi) * factor);
				velocity += acceleration;
			}

		if (thrusting)
		{
			thrustPoop.Play();
			rocketIdle.volume = 0.2f;
			if (!rocketIdle.isPlaying)
			{
				rocketIdle.Play();
			}
		}
		else
		{
			thrustPoop.Stop();
			if (rocketIdle.isPlaying)
				{
					if (rocketIdle.volume > 0)
						rocketIdle.volume -= 1.0f * Time.deltaTime;
					if (rocketIdle.volume < 0)
					{
						
						rocketIdle.Stop();	
					}
				}
		}

		BlackHoleGravity blackHole = GetComponentInParent<Gameplay>().GetComponentInChildren<BlackHoleGravity>();

		velocity += blackHole.doGravity(transform.position, shipMass);

		gameObject.transform.position += new Vector3(velocity.x, velocity.y, 0) * gameplay.timeMultipler;
		gameObject.transform.rotation = Quaternion.Euler(0, 0, orientation);

		// bounds check invisible wall
		if (gameObject.transform.position.magnitude > maxDistance)
			{
				gameObject.transform.position = gameObject.transform.position.normalized * maxDistance;
				velocity = new Vector2(0,0);
			}

		float warpFactor = blackHole.calcWarp(GetComponentInChildren<PlayerShip>().transform.position);

		Vector3 warpVec = (blackHole.transform.position - GetComponentInChildren<PlayerShip>().transform.position).normalized;
		
		warpEffect.transform.rotation = Quaternion.Euler(0,0,Mathf.Atan2(-warpVec.x, warpVec.y) / Mathf.PI * 180);

		if (warpFactor <= 0)
		{
			if (!gameplay.storyManager.CheckLastMessage())
			{
				rocketIdle.Stop();	
				thrusting = false;
				dying = true;
				deathTime = 5.5f;
				bhDeath.Play();				
			}
		}
	} 
}
