using System.Collections;
using UnityEngine;
using matnesis.TeaTime;

public class FlowyBehaviour : MonoBehaviour 
{
	public GameObject bullet;
	public int maxBulletCap;
	public int inBetweenDelay;
	public Transform shootPlace;
	public Transform player;

	private Animator anim;
	private TeaTime shoot;
	private TeaTime recharge;
	private bool rechargeAgain = true;

	// Use this for initialization
	void Start () 
	{
		anim = GetComponent<Animator> ();
		player = GameObject.FindGameObjectWithTag(Tags.Player).transform;
		recharge = this.tt();
		recharge.Wait (() => rechargeAgain).Add (() => 
			{
				if(Vector3.Distance(transform.position, player.position) < 17)
				{
					Shoot();
				}
			}).Add(inBetweenDelay).Repeat().Immutable();
	}
	
	// Update is called once per frame
	void Shoot () 
	{
		rechargeAgain = false;
		anim.SetBool ("Shooting", true);
		shoot = this.tt ();
		for(int i = 0; i < Random.Range(1, maxBulletCap) ;i++)
		{
			shoot.Add (0.3f, () =>
				{
					GameObject bulletobj = (GameObject) Instantiate (bullet, shootPlace.position, shootPlace.rotation);
				});
		}
		shoot.Add (() => {
			rechargeAgain = true;
			anim.SetBool ("Shooting", false);
		});
	}
}
