using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{

	public static PlayerMovement instance;

	public float movementSpeed, rotateAcc, copLightRange, drugMoveMult, drugSpeed;
	public GameObject bribeObject, noDealObject;
    public Vector3 startPosition;
    private float particleRate, startSpeed;
    private ParticleSystem particles;

	private Rigidbody2D rigidThing;

    public AudioSource source;

    public AudioClip arrestNoise, bribeNoise, bagNoise, sellNoise, grabNoise;

    void Awake()
	{
		instance = this;
        particles = transform.GetComponentInChildren<ParticleSystem>();
        particleRate = particles.emissionRate;
        particles.emissionRate = 0f;
    }

	void Start ()
	{
		rigidThing = GetComponent<Rigidbody2D> ();
        startPosition = transform.position;
        startSpeed = movementSpeed;
        reset();
	}

    public void reset()
    {
        transform.position = startPosition;
        transform.rotation = Quaternion.identity;
        movementSpeed = startSpeed;
    }

    void startParticles()
    {
        CancelInvoke();
        particles.emissionRate = particleRate;
        /*
        if (UpgradeController.instance.dashLevel == 0)
            Invoke("stopParticles", .25f);
        else*/
            Invoke("stopParticles", .5f);
    }

    void stopParticles()
    {
        particles.emissionRate = 0f;
    }

	void Update ()
	{
		updateMovement ();
	}

	void updateMovement()
	{
		if ((Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
            && GameController.instance.points > 0)
		{
            source.PlayOneShot(bagNoise);
			GameController.instance.points--;
            movementSpeed /= 1f - ((1f - drugMoveMult) / UpgradeController.instance.drugMult);
            transform.GetComponentInChildren<ParticleSystem>().Stop();
            transform.GetComponentInChildren<ParticleSystem>().Play();
            startParticles();

            //GameObject drug = (GameObject)GameObject.Instantiate(drugObject, transform.position + (Vector3)MathHelper.getVectorFromAngle(getAngle() + Mathf.PI, 3f),
            //                                                   Quaternion.identity);
            //drug.GetComponent<Drug>().dontSpawn = true;
        }
		Vector2 goalVelocity = Vector2.zero;
		if (Input.GetKey(KeyCode.LeftArrow))
		{
			goalVelocity.x = -1f * movementSpeed;
		}
		else if (Input.GetKey(KeyCode.RightArrow))
		{
			goalVelocity.x = movementSpeed;
		}
		
		if (Input.GetKey(KeyCode.UpArrow))
		{
			goalVelocity.y = movementSpeed;
		}
		else if (Input.GetKey(KeyCode.DownArrow))
		{
			goalVelocity.y = -1f * movementSpeed;
		}

        //rigidThing.velocity = goalVelocity;

        if (UpgradeController.instance.gameObject.activeInHierarchy)
        {
            goalVelocity = Vector2.zero;
        }
		else if (goalVelocity != Vector2.zero)
        {
            if (particles.emissionRate > 0f && UpgradeController.instance.dashLevel > 0)
                goalVelocity = MathHelper.resizeVector(goalVelocity, drugSpeed * UpgradeController.instance.dashMult);
            else
                goalVelocity = MathHelper.resizeVector(goalVelocity, movementSpeed + UpgradeController.instance.speedMult);
        }   

		float diff = rotateAcc * Time.deltaTime * UpgradeController.instance.speedMult;

		if ((rigidThing.velocity - goalVelocity).magnitude <= diff)
		{
			rigidThing.velocity = goalVelocity;
		}
		else
		{
            rigidThing.velocity += MathHelper.resizeVector(goalVelocity - rigidThing.velocity, diff);
		}


		//Debug.Log (rigidThing.velocity);

		/*
		if (goalVelocity != Vector2.zero)
		{
			float goalAngle = MathHelper.getVectorAngle(goalVelocity),
			diff = rotateAcc * Time.deltaTime;

			if (Mathf.Abs (MathHelper.getAngleDifference(goalAngle, getAngle())) <= diff)
			{
				Debug.Log("GOALLL");
				setAngle(goalAngle);
			}
			else if (MathHelper.getAngleDifference(goalAngle, getAngle()) < 0f)
			{
				setAngle(getAngle () - diff);
			}
			else
			{
				setAngle(getAngle () + diff);
			}
		}*/

	}

	void OnCollisionEnter2D(Collision2D other)
	{
		if (other.gameObject.tag == "Cop")
		{
            if (GameController.instance.money < 200)
            {
                UpgradeController.instance.gameObject.SetActive(true);
                UpgradeController.instance.loseScreen.SetActive(true);
                UpgradeController.instance.winner = false;
                source.PlayOneShot(arrestNoise);
                rigidThing.velocity = Vector2.zero;
            }
            else
            {
                GameObject.Instantiate(bribeObject, transform.position - new Vector3(0f, -.1f, 0f), Quaternion.identity);
                GameController.instance.money -= 200;
                Destroy(other.gameObject);
                source.PlayOneShot(bribeNoise);
            }
		}
		
	}
	void OnTriggerEnter2D(Collider2D other)	
	{
		if (other.gameObject.tag == "Drug")
		{
			GameController.instance.points++;
			movementSpeed *= 1f - ((1f - drugMoveMult) / UpgradeController.instance.drugMult);
			Destroy(other.gameObject);
            GameController.instance.drugCount--;
            source.PlayOneShot(grabNoise);
		}
		else if (other.gameObject.tag == "Home")
		{
			if (GameController.instance.points > 0)
			{
                if (GameController.instance.day == 7)
                {
                    UpgradeController.instance.gameObject.SetActive(true);
                    UpgradeController.instance.loseScreen.SetActive(true);
                    UpgradeController.instance.winner = true;
                    rigidThing.velocity = Vector2.zero;
                }
                else
                {
                    UpgradeController.instance.gameObject.SetActive(true);
                    rigidThing.velocity = Vector2.zero;
                }
			}
            else
            {
                Instantiate(noDealObject, other.transform.position + new Vector3(0f, 2f, -.1f), Quaternion.identity);
            }
		}
		
	}
	
	void setAngle(float angle)
	{
		transform.rotation = Quaternion.EulerAngles (0f, 0f, angle - (Mathf.PI / 2f));
	}
	
	float getAngle()
	{
		return ((transform.eulerAngles.z* Mathf.PI / 180f) + (Mathf.PI / 2f));
	}

}
