using UnityEngine;
using System.Collections.Generic;

public class BirdController : MonoSingleton<BirdController> {
	public float moveSpeed = 2f;
	public float flapDelay = .25f;
	public float flapForce = 250f;
	public Transform groundPoint;

	public bool JumpEnabled = true;
	public bool MoveEnabled = true;

	private float lastFlap = 0f;

	private float currentMovement = 0f;

	private int maxFlaps = 3;
	public int Flaps { get; set; }

	private bool grounded = false;

	Transform _transform;
	Rigidbody2D _body;
	BoxCollider2D _collider;

	BirdAnimationManager _animation;

	// Use this for initialization
	void Start () {
		_transform = GetComponent<Transform> ();
		_body = GetComponent<Rigidbody2D> ();
		_collider = GetComponent<BoxCollider2D> ();
		_animation = GetComponent<BirdAnimationManager> ();
		Flaps = 0;
	}
	
	// Update is called once per frame
	void Update () {

		//_body.AddForce (new Vector2 (Input.GetAxis ("Horizontal") * Time.deltaTime * flapForce, Time.deltaTime * flapForce/5));
//		_body.velocity = new Vector2 (Mathf.Min (moveSpeed, _body.velocity.x), _body.velocity.y);

		// PROBLEM: axis winds up and down even when it's set by button presses
		// TODO: immediately stop velocity on collision, movement in general will need another pass most likely
		if (MoveEnabled) {
			currentMovement = moveSpeed * Input.GetAxis ("Horizontal");
			if (currentMovement != 0) {
				_animation.changeState (BirdAnimationManager.AnimationStates.Walking);
			}
			if ((currentMovement > 0) && (transform.localScale.x < 0)) {
				transform.localScale = new Vector3 (-transform.localScale.x, transform.localScale.y, transform.localScale.z);
			} else if ((currentMovement < 0) && (transform.localScale.x > 0)) {
				transform.localScale = new Vector3 (-transform.localScale.x, transform.localScale.y, transform.localScale.z);
			}
		} else {
			currentMovement = 0;
		}
//		_body.velocity = new Vector2 (moveSpeed*Input.GetAxis("Horizontal"), _body.velocity.y);
		//		transform.position += new Vector3 (Input.GetAxis ("Horizontal") * Time.deltaTime * moveSpeed, 0f, 0f);

		if (Physics2D.Raycast (groundPoint.position, new Vector2(0f, -1f), 0.25f, 256).collider != null) {

				_animation.changeState(BirdAnimationManager.AnimationStates.Land);

			grounded = true;
		} else {
			grounded = false;
		}

		if (Input.GetButtonDown ("Jump")) {
			if(grounded) {
				Jump ();
			}
			else {
				if (Flaps > 0) {
					if (lastFlap < (Time.time - flapDelay)) {
						_animation.changeState(BirdAnimationManager.AnimationStates.Flutter);
						GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0f, flapForce));
						lastFlap = Time.time;
						Flaps--;
					}
				}
			}
		}
	}

	public void Jump()
	{
		if (JumpEnabled) {
			_animation.changeState(BirdAnimationManager.AnimationStates.TakeOff);
			GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0f, flapForce));
		}
	}

	public void EnableJump()
	{
		JumpEnabled = true;
	}

	void FixedUpdate() {
		_body.velocity = new Vector2 (currentMovement, _body.velocity.y);
		if (_body.velocity.y < 0) {
			_animation.changeState(BirdAnimationManager.AnimationStates.Glide);

		}
	}

}
