using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class TetraManager : MonoBehaviour
{
    public GameObject[] tetraPrefabs;
    private float m_speed = 4f;
    private float gridSize = 1f;
    private bool canMove = true; // Track movement ability
    private bool isWaitingForSpawn = false; // Track if we're waiting to spawn a new Tetra
    [SerializeField] private GameObject grid;

    private float gridLeftBound = -4f;
    private float gridRightBound = 4f;
    private float gridBottomBound = -9f;
    private float gridTopBound = 10f;

    private List<GameObject> spawnedTetras = new List<GameObject>();
    private float moveTimer;

    void Start()
    {
        SpawnTetra();
        moveTimer = 0f;
    }

    void Update()
    {
        if (canMove && !isWaitingForSpawn) // Check if can move and not waiting
        {
            moveTimer += Time.deltaTime;

            if (moveTimer >= 1f / m_speed)
            {
                MoveTetrasDown();
                moveTimer = 0f;
            }

            if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
            {
                MoveLeft();
            }

            if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
            {
                MoveRight();
            }
        }

        // Check if the last Tetra has hit the bottom
        if (spawnedTetras.Count > 0)
        {
            GameObject lastTetra = spawnedTetras[spawnedTetras.Count - 1];

            if (lastTetra != null && HasHitBottom(lastTetra.transform.position))
            {
                canMove = false; // Stop movement
                StartCoroutine(SpawnTetraAfterDelay()); // Start coroutine for spawning new Tetra
            }
        }
    }

    void MoveTetrasDown()
    {
        foreach (GameObject tetraInstance in spawnedTetras)
        {
            if (tetraInstance != null)
            {
                Vector3 newPosition = tetraInstance.transform.position;
                newPosition.y -= gridSize;

                if (IsWithinGrid(newPosition) && !HasHitBottom(newPosition))
                {
                    tetraInstance.transform.position = newPosition;
                }
                else if (HasHitBottom(newPosition)) // Check if it hits bottom
                {
                    canMove = false; // Stop movement when hit the bottom
                }
            }
        }
    }

    void SpawnTetra()
    {
        if (tetraPrefabs.Length == 0)
        {
            Debug.LogError("No tetra prefabs defined in the array.");
            return;
        }

        int randomIndex = Random.Range(0, tetraPrefabs.Length);
        GameObject tetraPrefab = tetraPrefabs[randomIndex];

        Vector3 spawnPosition = new Vector3(
            Mathf.Round(transform.position.x / gridSize) * gridSize,
            Mathf.Round(transform.position.y / gridSize) * gridSize,
            0);

        Renderer prefabRenderer = tetraPrefab.GetComponent<Renderer>();
        if (prefabRenderer != null)
        {
            float tetraHeight = prefabRenderer.bounds.size.y;

            switch (randomIndex)
            {
                case 0:
                    spawnPosition.y -= tetraHeight / 3.9f;
                    break;
                case 1:
                case 2:
                case 4:
                    spawnPosition.y -= tetraHeight / 5.5f;
                    break;
                case 3:
                    spawnPosition.y -= Mathf.Round(tetraHeight / 4f);
                    break;
            }
        }

        GameObject spawnedTetra = Instantiate(tetraPrefab, spawnPosition, Quaternion.identity);
        spawnedTetras.Add(spawnedTetra);
    }

    IEnumerator SpawnTetraAfterDelay()
    {
        isWaitingForSpawn = true; // Set waiting flag to true
        yield return new WaitForSeconds(1f); // Wait for 1 second
        SpawnTetra(); // Spawn new Tetra
        canMove = true; // Allow movement for the new Tetra
        isWaitingForSpawn = false; // Reset waiting flag
    }

    void MoveLeft()
    {
        if (!canMove) return; // Prevent moving if not allowed

        foreach (GameObject tetraInstance in spawnedTetras)
        {
            if (tetraInstance != null)
            {
                Vector3 newPosition = tetraInstance.transform.position;
                newPosition.x -= gridSize;

                if (IsWithinGrid(newPosition) && !HasHitBottom(newPosition))
                {
                    tetraInstance.transform.position = newPosition;
                }
            }
        }
    }

    void MoveRight()
    {
        if (!canMove) return; // Prevent moving if not allowed

        foreach (GameObject tetraInstance in spawnedTetras)
        {
            if (tetraInstance != null)
            {
                Vector3 newPosition = tetraInstance.transform.position;
                newPosition.x += gridSize;

                if (IsWithinGrid(newPosition) && !HasHitBottom(newPosition))
                {
                    tetraInstance.transform.position = newPosition;
                }
            }
        }
    }

    bool IsWithinGrid(Vector3 position)
    {
        return position.x >= gridLeftBound && position.x <= gridRightBound &&
               position.y >= gridBottomBound && position.y <= gridTopBound;
    }

    bool HasHitBottom(Vector3 position)
    {
        return position.y <= gridBottomBound; // Adjusted to check if position is below or at bottom
    }
}


```

fix the issue where it doesn't spawn in the piece after the one before it has reached the bottom. (remove all the comments too)