using UnityEngine;
using System.Collections;

public class basic_movement : MonoBehaviour {

	public KeyCode moveLeft;
	public KeyCode moveRight;
	public KeyCode jump;

	public Rigidbody2D RockFab;


	private global_variables variable_script;

	private GameObject gameManager;

	private cutscene_manager manager_script;

	private GameObject Camera;

	float player_speed=3;

	private GameObject[]background;
	GameObject[]midground;
	GameObject[]foreground;


	float currentvelocity_y;

	float mousePosX;
	float mousePosY;

	float relativeProjectileX;
	float relativeProjectileY;

	private BoxCollider2D own_collider;

	Vector3 screenPosition;
	float cameraX;
	float cameraY;

	private float rock_position_x;

	private Animator animator;

	float projectileAngle;

	void Awake ()
	{
		gameManager = GameObject.FindGameObjectWithTag ("GameController").gameObject;
		variable_script = gameManager.GetComponent<global_variables> ();
		manager_script = gameManager.GetComponent<cutscene_manager> ();
		Camera = GameObject.FindGameObjectWithTag ("MainCamera").gameObject;
		own_collider = GetComponent<BoxCollider2D> ();
		animator = this.GetComponent<Animator> ();

		background = GameObject.FindGameObjectsWithTag ("background");
		midground = GameObject.FindGameObjectsWithTag ("midground");
		foreground = GameObject.FindGameObjectsWithTag ("foreground");

	}
	// Use this for initialization
	void Start () {
		variable_script.playerHealth = 6;
	}

	void background_parallaxright(){
		foreach (GameObject background_scenary in background){
			background_scenary.transform.position+=new Vector3((player_speed/100),0,0);
		}
	}

	void background_parallaxleft(){
		foreach (GameObject background_scenary in background){
			background_scenary.transform.position-=new Vector3((player_speed/100),0,0);
		}
	}

	void foreground_parallaxright(){
				foreach (GameObject foreground_scenary in foreground) {
						foreground_scenary.transform.position -= new Vector3 ((player_speed / 100), 0, 0);
				}
	}

	void foreground_parallaxleft(){
				foreach (GameObject foreground_scenary in foreground) {
						foreground_scenary.transform.position += new Vector3 ((player_speed / 100), 0, 0);
				}
		}
		
		// Update is called once per frame
	void Update () {
				
		if (variable_script.InCutscene==false) {
						currentvelocity_y = rigidbody2D.velocity.y;

						variable_script.playerX = transform.position.x;
						variable_script.playerY = transform.position.y;

					

						if (currentvelocity_y > 0) {
								variable_script.movingUp = true;
						} else {
								variable_script.movingUp = false;
						}

						if (Input.GetKey (moveRight)) {
								transform.localScale = new Vector3 (-1, 1, 1);
								rigidbody2D.velocity = new Vector2 (player_speed, currentvelocity_y);
								animator.SetFloat ("Movement", (float)player_speed);
								background_parallaxright ();
								foreground_parallaxright ();
						} else if (Input.GetKey (moveLeft)) {
								transform.localScale = new Vector3 (1, 1, 1);
								rigidbody2D.velocity = new Vector2 (-player_speed, currentvelocity_y);
								animator.SetFloat ("Movement", (float)player_speed);
								background_parallaxleft ();
								foreground_parallaxleft ();
						} else {
								rigidbody2D.velocity = new Vector2 (0, currentvelocity_y);
								animator.SetFloat ("Movement", (float)0);

						}

						if (Input.GetKey (jump) && variable_script.canJump == true) {
								rigidbody2D.AddForce (Vector2.up * 5, ForceMode2D.Impulse);
								animator.SetBool ("Jumping", true);
								variable_script.canJump = false;
						}

						if (Input.GetMouseButtonDown (1) && variable_script.rockFire == false) {
				
								mousePosX = Input.mousePosition.x;
								mousePosY = Input.mousePosition.y;
				
								screenPosition = Camera.camera.WorldToScreenPoint (gameObject.transform.position);
								cameraX = screenPosition.x;
								cameraY = screenPosition.y;
				
								relativeProjectileX = (mousePosX - cameraX) / 50;
								relativeProjectileY = (mousePosY - cameraY) / 50;
				
				
								variable_script.projection.Set (relativeProjectileX, relativeProjectileY);

				
								Rigidbody2D rockInstance;
							
							

								if (variable_script.projection.x > 0) {
										transform.localScale = new Vector3 (-1, 1, 1);
										rock_position_x = transform.position.x + (own_collider.size.x / 2) + (float)0.2;
								} else if (variable_script.projection.x <= 0) {
										transform.localScale = new Vector3 (1, 1, 1);
										rock_position_x = transform.position.x - (own_collider.size.x / 2) - (float)0.2;
								}

								rockInstance = Instantiate (RockFab, new Vector3 (rock_position_x, transform.position.y + (own_collider.size.x / 4), 0), Quaternion.identity) as Rigidbody2D;
				
							
				
				
								variable_script.rockFire = true;



						}
					

					
						if (transform.position.y >= -2.07 && variable_script.playerHealth > 0) {

								double cameraNewY = (-0.11 - transform.position.y);

							

								Camera.transform.localPosition = new Vector3 (0, (float)cameraNewY, -10);
						} else {
								Camera.transform.localPosition = new Vector3 (0, (float)2, -10);
						}
				}
		}

	void ChangeColor (){
		gameObject.GetComponent<SpriteRenderer> ().color = Color.white;
	}

	
	void OnCollisionEnter2D(Collision2D collision){
				if (collision.gameObject.tag == "platform") {
						animator.SetBool ("Jumping", false);
						variable_script.canJump = true;

				} else if (collision.gameObject.tag == "enemy" || collision.gameObject.tag == "projectile") {
						variable_script.playerHealth--;

						if (collision.gameObject.transform.position.x < transform.position.x) {
								rigidbody2D.AddForce (new Vector2 (5, 1), ForceMode2D.Impulse);
						} else {
								rigidbody2D.AddForce (new Vector2 (-5, 1), ForceMode2D.Impulse);
						}
						gameObject.GetComponent<SpriteRenderer> ().color = Color.red;
						Invoke ("ChangeColor", (float)0.1);
				} 
		}

		void OnTriggerEnter2D(Collider2D collision){
			if (collision.gameObject.tag == "cutscene_trigger") {
				animator.SetBool ("Jumping", false);
				animator.SetFloat ("Movement", (float)0);

				Camera.transform.localPosition = new Vector3 ((float)-(93.37-transform.position.x), (float)2, -10);
				manager_script.BeginCutscene ();
			}
		}
}
