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

public class PlayerFlyScript : MonoBehaviour {

	//A public variable to set speed
	public float batSpeed;

	public GameObject spriteManager;
	PlayerSpriteScript spriteControllerScript;

	//Grabs it's own rigidbody
	Rigidbody2D ownBody;

	bool deathVar = false;


	// Use this for initialization
	void Start () {

		ownBody = gameObject.GetComponent<Rigidbody2D>();
		spriteControllerScript = spriteManager.GetComponent<PlayerSpriteScript> ();
		
	}
	
	// Update is called once per frame
	void FixedUpdate () {

		if(deathVar == false)
			PlayerMoveFunction ();
		if (ownBody.velocity.x < 0) {
			spriteControllerScript.SpriteFlipperFunc (-1);
		} else {
			spriteControllerScript.SpriteFlipperFunc (1);
		}
		
	}

	void PlayerMoveFunction()
	{
		float climbFactor = 0.6f;
		float fallFactor = 1.4f;
		float horizontal = Input.GetAxis ("Horizontal") * batSpeed;
		float vertical = Input.GetAxis ("Vertical") * batSpeed;

		if (vertical > 0f)
			vertical *= climbFactor;
		else
			vertical *= fallFactor;


		
		Vector2 newForce = new Vector2 (horizontal,vertical);

		ownBody.AddForce (newForce);
				

	}

	public void SuccessfullyRespawned()
	{
		deathVar = false;
		gameObject.GetComponent<BoxCollider2D> ().enabled = true;
		ownBody.gravityScale = 0.01f;
	}

	public bool SendDeathVar()
	{

		return deathVar;
	}

	void OnCollisionEnter2D(Collision2D other)
	{
		if (other.gameObject.tag == "Hazard" || other.gameObject.tag == "OwlTag") {

			deathVar = true;

			GameObject managerObject;
			managerObject = GameObject.FindGameObjectWithTag ("GameController");
			GameManagerScript managerScript = managerObject.GetComponent<GameManagerScript> ();
			managerScript.RespawnCall ();


			ownBody.velocity *= 0;
			ownBody.AddForce (new Vector2 (0,300));
			gameObject.GetComponent<BoxCollider2D> ().enabled = false;
			ownBody.gravityScale = 1.0f;

		}
	}

	void OnTriggerEnter2D(Collider2D other)
	{

		//if (other.gameObject.tag == "Star") {

			Debug.Log ("Gud Star Spot");

		//}

	}
}
