using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

  public GameManager gameManager;
  public InputActionReference screenTabAction;

  public GameObject projectilePrefab;

  public float projectileSpeed = 1;

  // Start is called once before the first execution of Update after the MonoBehaviour is created
  void Start()
  {
    if (gameManager == null)
    {
      gameManager = FindFirstObjectByType<GameManager>();
    }
  }

  // Update is called once per frame
  void Update()
  {
    if (screenTabAction.action.WasPressedThisFrame()) {
      ShootProjectile();
    }
  }


  private void ShootProjectile()
  {

    if (gameManager != null)
    {
      if (gameManager.GetGameState() != GameState.GAME_RUNNING)
      {
        return;
      }
    }

    Debug.Log("Projectile Shot!");

    Vector3 spawnPosition = Camera.main.transform.position;
    Vector3 direction = Camera.main.transform.forward;

    GameObject spawnedProjectile = Instantiate(projectilePrefab);

    spawnedProjectile.transform.position = spawnPosition;

    Projectile projectileScript = spawnedProjectile.GetComponent<Projectile>();

    if (projectileScript != null) {
      projectileScript.SetValues(projectileSpeed, direction);
    }

    GameObject dynamicObject = GameObject.Find("_Dynamic");
    if (dynamicObject != null) {
      spawnedProjectile.transform.SetParent(dynamicObject.transform);
    }



  }


}
