using UnityEngine;
using System.Collections;

public class PlayerScript : AnimatedObjectScript {

	public enum PlayerState {Stop,Moving, ReadingDialog};
	public PlayerState player_state;
	//private bool colidiu = false;
	public GameObject objetoInterativo;
	public float velMaxima = 10;
	Animator myAnim;
	// Use this for initialization

	public int testvariable;

	public Animator[] animation_storage;

	public enum current_player{mom,olderboy,youngerboy};

	public current_player selected_player;

	public int id;

	public AudioClip sound_steps;

	private AudioSource step_source;

	private bool hasplayed;

	void Awake()
	{
		hasplayed = false;
		DontDestroyOnLoad (this.gameObject);
		step_source = GetComponent<AudioSource> ();
	}

	void Start () {

		this.myAnim = this.gameObject.GetComponent<Animator>();
		this.player_state = PlayerState.Stop;
	}

	private static float soundDelay = 1.43f;
	private float timeSincePlayStep = soundDelay;

	// Update is called once per frame
	void Update () {
		
		Debug.Log (this.player_state);

		if (player_state != PlayerState.ReadingDialog)
		{
			if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw ("Vertical") != 0) {
					player_state = PlayerState.Moving;
			} else {
					player_state = PlayerState.Stop;
			}
		}

			if (this.player_state == PlayerState.Moving) {
						float hSpeed = Input.GetAxisRaw ("Horizontal");
						float vSpeed = Input.GetAxisRaw ("Vertical");

						if (timeSincePlayStep >= soundDelay) {
								PlaySound ();
								timeSincePlayStep = 0;
						}
						timeSincePlayStep += Time.deltaTime;

						//if(!hasplayed)
						if (Mathf.Abs (vSpeed) > Mathf.Abs (hSpeed)) {
								if (vSpeed > 0.0) { 
										//cima
										this.rigidbody2D.velocity = new Vector2 (0, 1) * velMaxima;
										this.Direcao = Facing.N;
								} else if (vSpeed < 0.0) { 
										//step_source.PlayOneShot(sound_steps,1.0f);//baixo
										this.rigidbody2D.velocity = new Vector2 (0, -1) * velMaxima;
										this.Direcao = Facing.S;
								}
						} else { 
								if (hSpeed > 0.0) { 
										//step_source.PlayOneShot(sound_steps,1.0f);//direita
										this.rigidbody2D.velocity = new Vector2 (1, 0) * velMaxima;
										this.Direcao = Facing.E;

								} else if (hSpeed < 0.0) { 
										//step_source.PlayOneShot(sound_steps,1.0f);//direita
										this.rigidbody2D.velocity = new Vector2 (-1, 0) * velMaxima;
										this.Direcao = Facing.W;

								} else {
										this.rigidbody2D.velocity = Vector2.zero;
								}
						}

						myAnim.SetInteger ("Direction", this.Direcao.GetHashCode ());

						myAnim.SetFloat ("Speed", this.rigidbody2D.velocity.magnitude);
				} else if (this.player_state == PlayerState.Stop) {
						myAnim.SetInteger ("Direction", this.Direcao.GetHashCode ());
						myAnim.SetFloat ("Speed", 0);
						rigidbody2D.velocity = Vector2.zero;
				} else {
						rigidbody2D.velocity = Vector2.zero;
				}
		/*if (this.rigidbody2D.velocity.magnitude > velMaxima) {
			this.rigidbody2D.velocity = this.rigidbody2D.velocity.normalized * velMaxima;
		}*/
		
		
		/*if (Input.GetButtonDown("Fire1")) {
			if(this.objetoInterativo != null){

			}
		}*/
	}

	void PlaySound()
	{

		step_source.PlayOneShot(sound_steps,1.0f);
		//hasplayed = true;

	}

	void StopSound()
	{
		step_source.Stop ();
	}

	void OnCollisionEnter2D(Collision2D coll){
		if (coll.gameObject.tag == "Enemy") {

		}
	}

}
