using System.Collections;
using UnityEngine;

public class Weapon : MonoBehaviour
{
    public GameObject bulletPrefab;
    public float bulletSpeed = 10f;
    public int bulletDamage = 1;
    public float bulletDespawnTime = 2f;
    public float shootCooldown = 0.2f;

    public bool canShoot = true;

    public static Weapon instance;

    private void Awake()
    {
        instance = this;    
    }

    // Method for shooting, can be overridden by subclasses
    public virtual void Shoot(Vector3 spawnPosition, Quaternion spawnRotation)
    {
        if (canShoot)
        {
            // Play the sound effect
            AudioManager.instance.PlayShootingSound();

            // Instantiate a bullet
            GameObject bullet = InstantiateBullet(spawnPosition, spawnRotation);

            // Set bullet properties (speed, damage, despawn time)
            BulletController bulletController = bullet.GetComponent<BulletController>();
            bulletController.Initialize(bulletSpeed, bulletDamage, bulletDespawnTime, this.gameObject);

            // Update our statistic
            Performance.shotsFired++;

            // Set the shoot cooldown flag to false
            canShoot = false;

            // Start the cooldown
            StartCoroutine(ShootCooldown());
        }
    }

    // Coroutine for the shoot cooldown
    public IEnumerator ShootCooldown()
    {
        yield return new WaitForSeconds(shootCooldown);

        // Set the shoot cooldown flag to true
        canShoot = true;
    }

    // Method to instantiate a bullet, can be overridden by subclasses
    protected virtual GameObject InstantiateBullet(Vector3 spawnPosition, Quaternion spawnRotation)
    {
        return Instantiate(bulletPrefab, spawnPosition, spawnRotation);
    }
}
