using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MailboyMove : MonoBehaviour {

	public float speed = 10f;
	private Rigidbody rb;
		public string pName;
		bool facingRight = true;
		bool grounded = false;
		public Transform groundCheck;
		float groundRadius = 0.2f;
		public LayerMask whatIsGround;
		public float jumpForce = 50f;
		public bool doubleJump = false;
		public Vector2 vel;
		public bool crouch;
		Animator anim;
		public bool hit = false;
		public bool hitInit = false;
		public float hitTimer = 0;
		public float hitCd = 1f;
		public float colX;
	public int bombCount = 3;
	public GameObject bomb;

	public CapsuleCollider player;

	public float punchForceX = 500f;
	public float punchForceY = 500f;

	public bool canDrop = true;
	public float dropTimer = 2.0f;
	// Use this for initialization
	void Start () {
		
		rb = GetComponent<Rigidbody> ();
				anim = GetComponent<Animator> ();
				crouch = false;
	}
	
	// Update is called once per frame
	void Update () {
		if (GameManager.GM.GetState () == GS.Ready || GameManager.GM.GetState () == GS.Playing) {
			float move = Input.GetAxis (pName + " X-Axis");
			anim.SetFloat ("speed", Mathf.Abs (move));

			if (grounded)
				doubleJump = true;

			if (grounded && Input.GetButtonDown (pName + " B")) {
				rb.AddForce (new Vector3 (0, jumpForce));

			} else if (doubleJump && Input.GetButtonDown (pName + " B")) {
				vel = rb.velocity;
				vel.y = 0;
				rb.velocity = vel;

				rb.AddForce (new Vector3 (0, jumpForce));
				doubleJump = false;
			}

			if (Input.GetButtonDown (pName + " Y") && canDrop) {
				StartCoroutine (bombCD ());
				Instantiate (bomb, new Vector3 (transform.position.x, transform.position.y - 1, transform.position.z), transform.rotation);
				bomb.GetComponent<Rigidbody> ().AddForce (new Vector3 (1, 0), ForceMode.Impulse);
			}
		}
		if (grounded && Input.GetAxis (pName + " Y-Axis") < 0 && Input.GetAxis (pName + " X-Axis") == 0) {
			crouch = true;
			anim.SetBool ("crouching", crouch);
		}
		if (Input.GetAxis (pName + " Y-Axis") >= 0) {
			crouch = false;
			anim.SetBool ("crouching", crouch);
		}
	}

	void FixedUpdate () {
		if (GameManager.GM.GetState () == GS.Ready || GameManager.GM.GetState () == GS.Playing) {
			//grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
			anim.SetBool ("ground", grounded);
			anim.SetFloat ("vSpeed", rb.velocity.y);
			float move = Input.GetAxis (pName + " X-Axis");

			//anim.SetFloat ("speed", Mathf.Abs (move));

			if (!GetComponent<PlayerAttack> ().attacking && !crouch && !hit && !hit2) {
				rb.velocity = new Vector3 (move * speed, rb.velocity.y);
			}
						
			if (hit) {
//						Debug.Log (transform.position.x);
//						Debug.Log (colX);
//						bool right = transform.position.x > colX;
//						if (hitInit && right) {
//								rb.AddForce (new Vector3 (1000, 1000));
//								hitInit = false;
//						}
//						if (hitInit && !right) {
//								rb.AddForce (new Vector3 (-1000, 1000));
//								hitInit = false;
//						}
//				}
//						if (move > 0 && !facingRight)
//								Flip ();
//						else if (move < 0 && facingRight)
//								Flip ();

				bool right = transform.position.x > colX;
				if (hitInit && right) {
					rb.AddForce (new Vector3 (punchForceX, punchForceY));
					hitInit = false;
				}
				if (hitInit && !right) {
					rb.AddForce (new Vector3 (-punchForceX, punchForceY));
					hitInit = false;
				}
			}
			if (move > 0 && !facingRight)
				Flip ();
			else if (move < 0 && facingRight)
				Flip ();

			grounded = false;
		}
		else {
			rb.velocity = new Vector3 ();
		}
	}

		void Flip () {
				facingRight = !facingRight;
				Vector3 scale = transform.localScale;
				scale.x *= -1;
				transform.localScale = scale;

		}

		/*public void Hit(float colliderx){
				hit = true;
				hitInit = true;
				colX = colliderx;
				anim.SetBool ("hit", true);
				hitTimer = hitCd;
				if (hit) {
						if (hitTimer > 0)
								hitTimer -= Time.deltaTime;
						else {
								hit = false;
								anim.SetBool ("hit", false);

						}
				}

		} */

	IEnumerator bombCD() {
		canDrop = false;
		yield return new WaitForSeconds (dropTimer);

		canDrop = true;
	}

		public void Hit(float colliderX) {
				colX = colliderX;
				hitInit = true;
				StartCoroutine (getHit ());
		}

		IEnumerator getHit() {
				hit = true;
				anim.SetBool ("hit", true);
				AudioController.AC.playPunch ();
		if (pName.Equals ("P1"))
			AudioController.AC.playMail ();
		else if (pName.Equals ("P2"))
			AudioController.AC.playMsg ();
		else if (pName.Equals ("P3"))
			AudioController.AC.playCloud ();
		else if (pName.Equals ("P4"))
			AudioController.AC.playPhone ();
				yield return new WaitForSeconds (hitCd);
				hit = false;
				anim.SetBool ("hit", false);
		}

	public void Hit2()
	{

		StartCoroutine(getHit2());
	}

	bool hit2 = false;

	IEnumerator getHit2()
	{
		hit2 = true;
		anim.SetBool("hit", true);
		yield return new WaitForSeconds(hitCd);
		hit2 = false;
		anim.SetBool("hit", false);
	}

	void OnCollisionStay(Collision collisionInfo) {
		if (collisionInfo.gameObject.CompareTag ("Ground")) {
			grounded = true;
		}
	}
}
