﻿using UnityEngine;
using System.Collections;

public enum status{stopped, jumping, running, crouched, walking, attacking, alighting, damage};

public class PlayerMoviment : MonoBehaviour
{
	public status playerStatus = status.stopped;
		//Variaveis de botões
		public KeyCode jump;
		public KeyCode right;
		public KeyCode left;
		public KeyCode down;
		public KeyCode atk;
		public KeyCode toRun;

		//Variaveis de movimentação do player
		public float speedWalk;
		public float speedRunning;
		public float heightJump;
		public float parableJump = -1000;

		//Posição original da fase
		public Vector3 posOrigin;

		public bool takingDamage;
		public bool jumping;		
		public float timeJump;
		float oriTimeJump;

		GameObject camera;
		public Transform flamePrefab;

		Animator anim;
		public Vector3 playerScale;

		// Use this for initialization
		void Start ()
		{
			anim = GetComponent<Animator>();
			camera = GameObject.FindGameObjectWithTag("MainCamera");
			posOrigin = transform.position;
			playerScale = transform.localScale;
			oriTimeJump = timeJump;		
		}
	void OnCollisionStay2D(Collision2D other)
	{
		if(Input.GetKey(jump) && other.gameObject.tag == "chao")
			jumping = true;
		
	}
		void OnCollisionEnter2D(Collision2D other)
		{
			if (other.gameObject.tag == "chao") 
				{
					timeJump = oriTimeJump;
					playerStatus = status.stopped;
					//Debug.Log ("Colidiu com o chão e ficou parado");
								
						if (Input.GetKey (toRun) && (Input.GetKey (right) || Input.GetKey (left))) 
						{
							timeJump = oriTimeJump;
							playerStatus = status.running;
							//Debug.Log ("Colidiu com o chão e seguiu correndo");
						}
						else if ((Input.GetKey (right) || Input.GetKey (left)))
							{
								timeJump = oriTimeJump;
								playerStatus = status.walking;
								//Debug.Log ("Colidiu com o chão e seguiu Andando");
							}
				}
		}
		void OnTriggerEnter2D(Collider2D other)
		{
			if(other.gameObject.tag == "DeslPersiguiçãoDaCamera")
				camera.GetComponent<CameraPos>().enabled = false;

		}
	// Update is called once per frame
	void Update ()
	{	
		if (takingDamage)
			playerStatus = status.damage;
			else if (jumping)
				playerStatus = status.jumping;
					else if (Input.GetKeyDown (atk)) 
						playerStatus = status.attacking;
						else if (!jumping && Input.GetKey (toRun) && (Input.GetKey (right) || Input.GetKey (left)))
							playerStatus = status.running;
							else if (!jumping && (Input.GetKey (right) || Input.GetKey (left)))
								playerStatus = status.walking;
								else
									playerStatus = status.stopped;
		

		if (timeJump < 0)
			playerStatus = status.alighting;		

		switch (playerStatus)
		{
		case status.attacking:
			anim.SetBool ("Attacking", true);
			anim.SetBool ("Stopped", false);
			anim.SetBool ("Jumping", false);
			anim.SetBool ("Walking", false);
			anim.SetBool ("Running", false);
			anim.SetBool ("Crouched", false);
			anim.SetBool ("Alighting", false);
			anim.SetBool ("Damage", false);

			//faz o cod do tiro rodar
			GetComponent<ShootGun>().shoot = true;
			
			break;

			case status.crouched:
				anim.SetBool ("Crouched", true);
				anim.SetBool ("Stopped", false);
				anim.SetBool ("Jumping", false);
				anim.SetBool ("Walking", false);
				anim.SetBool ("Running", false);
				anim.SetBool ("Attacking", false);
				anim.SetBool ("Alighting", false);
				anim.SetBool ("Damage", false);
			break;
			case status.damage:
				anim.SetBool ("Damage", true);
				anim.SetBool ("Attacking", false);
				anim.SetBool ("Stopped", false);
				anim.SetBool ("Jumping", false);
				anim.SetBool ("Walking", false);
				anim.SetBool ("Running", false);
				anim.SetBool ("Crouched", false);
				anim.SetBool ("Alighting", false);
				if (gameObject.GetComponent<PlayerFight> ().posEnemyDirection = true)
				{
					transform.Translate (-speedWalk * Time.deltaTime, 0, 0);
					transform.localScale = new Vector3 (playerScale.x, playerScale.y, 1);
				} else 
				{
					transform.Translate (speedWalk * Time.deltaTime, 0, 0);
					transform.localScale = new Vector3 (-playerScale.x, playerScale.y, 1);
				}
			break;

			case status.alighting:
				anim.SetBool ("Alighting", true);
				anim.SetBool ("Running", false);
				anim.SetBool ("Stopped", false);
				anim.SetBool ("Jumping", false);
				anim.SetBool ("Walking", false);
				anim.SetBool ("Crouched", false);
				anim.SetBool ("Attacking", false);
				anim.SetBool ("Damage", false);
				
				if (Input.GetKey (left) && Input.GetKey (toRun)) 
					{
						transform.Translate (-speedRunning * Time.deltaTime, 0, 0);
						transform.localScale = new Vector3 (-playerScale.x, playerScale.y, 1);
					}
					else if (Input.GetKey (right) && Input.GetKey (toRun)) 
						{
							transform.Translate (speedRunning * Time.deltaTime, 0, 0);
							transform.localScale = new Vector3 (playerScale.x, playerScale.y, 1);
						} 
						else if (Input.GetKey (right)) 
							{
								transform.Translate (speedWalk * Time.deltaTime, 0, 0);
								transform.localScale = new Vector3 (playerScale.x, playerScale.y, 1);
							} 
							else if (Input.GetKey (left)) 
								{
									transform.Translate (-speedWalk * Time.deltaTime, 0, 0);
									transform.localScale = new Vector3 (-playerScale.x, playerScale.y, 1);
								} 
			break;

			case status.running:
				anim.SetBool ("Running", true);
				anim.SetBool ("Stopped", false);
				anim.SetBool ("Jumping", false);
				anim.SetBool ("Walking", false);
				anim.SetBool ("Crouched", false);
				anim.SetBool ("Attacking", false);
				anim.SetBool ("Alighting", false);
				anim.SetBool ("Damage", false);

				if (Input.GetKey (right) && Input.GetKey (toRun)) 
					{
						transform.Translate (speedRunning * Time.deltaTime, 0, 0);
						transform.localScale = new Vector3 (playerScale.x, playerScale.y, 1);
					} 
					else if (Input.GetKey (left) && Input.GetKey (toRun))
						{
							transform.Translate (-speedRunning * Time.deltaTime, 0, 0);
							transform.localScale = new Vector3 (-playerScale.x, playerScale.y, 1);
						}
			break;

			case status.walking:
				anim.SetBool ("Walking", true);
				anim.SetBool ("Stopped", false);
				anim.SetBool ("Jumping", false);
				anim.SetBool ("Running", false);
				anim.SetBool ("Crouched", false);
				anim.SetBool ("Attacking", false);
				anim.SetBool ("Alighting", false);
				anim.SetBool ("Damage", false);

				if (Input.GetKey (right)) 
					{
						transform.Translate (speedWalk * Time.deltaTime, 0, 0);
						transform.localScale = new Vector3 (playerScale.x, playerScale.y, 1);
					} 
					else if (Input.GetKey (left)) 
						{
							transform.Translate (-speedWalk * Time.deltaTime, 0, 0);
							transform.localScale = new Vector3 (-playerScale.x, playerScale.y, 1);
						} 
				
			break;

			case status.jumping:
				anim.SetBool ("Damage", false);
				anim.SetBool ("Stopped", false);
				anim.SetBool ("Jumping", true);
				anim.SetBool ("Walking", false);
				anim.SetBool ("Running", false);
				anim.SetBool ("Crouched", false);
				anim.SetBool ("Attacking", false);
				anim.SetBool ("Alighting", false);


				GetComponent<Rigidbody2D> ().AddForce (transform.up * heightJump, ForceMode2D.Force);
				timeJump -= Time.deltaTime;

				if (timeJump < 0)
					jumping = false;


				if (Input.GetKey (left) && Input.GetKey (toRun)) 
					{
						transform.Translate (-speedRunning * Time.deltaTime, 0, 0);
						transform.localScale = new Vector3 (-playerScale.x, playerScale.y, 1);
					}
					else if (Input.GetKey (right) && Input.GetKey (toRun)) 
						{
							transform.Translate (speedRunning * Time.deltaTime, 0, 0);
							transform.localScale = new Vector3 (playerScale.x, playerScale.y, 1);
						} 
						else if (Input.GetKey (right)) 
							{
								transform.Translate (speedWalk * Time.deltaTime, 0, 0);
								transform.localScale = new Vector3 (playerScale.x, playerScale.y, 1);
							} 
							else if (Input.GetKey (left)) 
								{
									transform.Translate (-speedWalk * Time.deltaTime, 0, 0);
									transform.localScale = new Vector3 (-playerScale.x, playerScale.y, 1);
								} 

			break;

			case status.stopped:
			default:
				anim.SetBool ("Stopped", true);
				anim.SetBool ("Jumping", false);
				anim.SetBool ("Walking", false);
				anim.SetBool ("Running", false);
				anim.SetBool ("Crouched", false);
				anim.SetBool ("Attacking", false);
				anim.SetBool ("Alighting", false);
				anim.SetBool ("Damage", false);
			break;
		}
	}
}
