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

public class Cannon : MonoBehaviour {

    [Header("Values")]
    [Tooltip("Delay before return power")]
    public PoweredObject power;
	public float delayAfterFire = 2f;
	public GameObject firingPosition;
	[Space(5)]
	public Transform bulletPrefab;
	public Transform firingEffectPrefab;

	float currentDelayTime = 0;
	// Use this for initialization
	void Start () {
        power = firingPosition.GetComponent<PoweredObject>();
	}

	void Update(){

		if (currentDelayTime > 0)
		{
			currentDelayTime -= Time.deltaTime;
		}
	}
	
	public void Fire()
	{
		if (bulletPrefab != null)
		{
			Vector3 angle = firingPosition.transform.rotation.eulerAngles;
			if (transform.localScale.x < 0 )
			{
				angle.z = 180 + angle.z;
			}
			BaseBullet bullet = 	Instantiate(bulletPrefab, firingPosition.transform.position, Quaternion.Euler(angle)).gameObject.GetComponent<BaseBullet>();
			bullet.owner = gameObject;
		}

		if (firingEffectPrefab != null)
		{
			Transform node = Instantiate(firingEffectPrefab, firingPosition.transform.position, Quaternion.identity);
			node.localScale = transform.localScale;
		}


		currentDelayTime = delayAfterFire;
	}

	public bool IsFinishCoolDown()
	{
		return currentDelayTime <= 0;
	}
}
