using UnityEngine;
using UnityEngine.InputSystem;

public class GunController : MonoBehaviour
{
  [SerializeField]
  private Transform muzzle;
  [SerializeField]
  private InputActionProperty trigger;

  public float shotsPerSecond = 1.0f;
  public float projectileSpeed = 1.0f;
  public float projectileLifeTime = 1.0f;

  public GameObject projectileObject;

  private float lastShotTimestamp;

  private float lastTriggerPress;




  // Start is called once before the first execution of Update after the MonoBehaviour is created
  void Start()
  {
    lastShotTimestamp = -1 * (1 / shotsPerSecond);
  }

  // Update is called once per frame
  void Update()
  {



    if (trigger.action.WasPressedThisFrame() && Time.time - lastShotTimestamp >= 1/shotsPerSecond)
    {
      GameObject spawnedObject = Instantiate(projectileObject);
      Projectile projectileScript = spawnedObject.GetComponent<Projectile>();

      GameObject dynamicContainer = GameObject.Find("_Dynamic");
      if (dynamicContainer != null)
      {
        spawnedObject.transform.parent = dynamicContainer.transform;
      }

      spawnedObject.transform.position = muzzle.position;
      spawnedObject.transform.rotation = transform.rotation;

      if (projectileScript != null) {
        projectileScript.maxLifeTime = projectileLifeTime;
        projectileScript.speed = projectileSpeed;
      }


      lastShotTimestamp = Time.time;

      Debug.Log("Shoot");
    }
  }
}
