using UnityEngine;

public class GunShotManager : MonoBehaviour {
    public float shootDelay;
    public float shootDuration = 1;
    public Vector3 projectileOffset;
    public GameObject projectilePrefab;
    public ParticleSystem ballPart;
    public ParticleSystem specsPart;
    public ParticleSystem littleExplosionPart;
    public AudioClip chargingSound;
    public AudioClip shootSound;
    public SphereCollider noiseSphere;
    public float shootingNoise = 25;
    public PlayerGunManager gunManager;
    public ShrapnelGamePlayerMover mover;

    AudioSource mySource;

    private void Awake() {
        mySource = GetComponent<AudioSource>();
    }
    //void Start() {
    //    Shoot();
    //}

    public void Shoot() {
        // Play charging sound effect
        mySource.PlayOneShot(chargingSound);

        // start animating specs and charge ball
        ballPart.Play();
        specsPart.Play();

        // Invoke a thing in a certain time
        Invoke("CreateProjectile", shootDelay);
    }

    void CreateProjectile() {
        noiseSphere.radius = shootingNoise;
        mover.shooting = true;
        Invoke("TurnOffShooting", shootDuration);
        //noiseSphere.GetComponent<Animator>().SetTrigger("Shoot");
        littleExplosionPart.Play();
        mySource.PlayOneShot(shootSound);
        GameObject projectileClone = Instantiate(projectilePrefab, transform.position + projectileOffset, transform.rotation);
        gunManager.AddCamRecoil();

        Collider[] hitObjects = Physics.OverlapSphere(transform.position, shootingNoise);

        for (int i = 0; i < hitObjects.Length; i++) {
            if (hitObjects[i].tag == "Tiger") {
                TigerController tiger = hitObjects[i].GetComponent<TigerController>();
                tiger.HeardPlayer();
                tiger.moveSpeed = tiger.shotHeardSpeed;
            }
        }
    }

    void TurnOffShooting() {
        mover.shooting = false;
    }
}
