using UnityEngine;
using System.Collections;

public class FireHarpoon : MonoBehaviour {

	public GameObject Harpoon;
	GameObject HarpoonSpawn;
	//int delayTime = 3; after each shot set to 0 and at 3 seconds player can fire again
	int cooldown = 60;
	public int cooldownMax = 30;
	bool readyToReload;

	Vector3 MousePosition;

	bool CanFire;

	// Use this for initialization
	void Start () {
		
		HarpoonSpawn = GameObject.Find ("HarpoonSpawn");
		readyToReload = false;
		CanFire = true;
	}
	
	// Update is called once per frame
	void Update () {
		MousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		CheckIfCanFire ();
		CheckInput ();
		if (readyToReload == true) 
		{
			ReloadHarpoon ();
		}
	}

	void CheckInput()
	{
		if (Input.GetMouseButton (0) && CanFire == true) 
		{
			Vector2 direction = MousePosition - gameObject.transform.position;
			direction.Normalize ();
			Quaternion HarpoonRotation = Quaternion.Euler (0, 0, Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg - 90f);
			GameObject ShotHarpoon = (GameObject)Instantiate (Harpoon, HarpoonSpawn.transform.position, HarpoonRotation);

			CanFire = false;
			//Debug.Log ("Fire");
			//Instantiate (Harpoon, HarpoonSpawn.transform.position, Quaternion.identity);
			//Harpoon.transform.LookAt (MousePosition);
			cooldown = 0;

		}
	}
	void CheckIfCanFire()
	{
		if (cooldown == 0) 
		{
			readyToReload = true;
		}
	}
	void ReloadHarpoon()
	{
		cooldown = cooldown + 1;
		if (cooldown >= cooldownMax) 
		{
			readyToReload = false;
			CanFire = true;
		}
	}

}
