using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Basket : MonoBehaviour
{
    private int rockCount = 0;
    private int maxRocks = 5;

    public Button nextButton;
    public RockSpawner spawner;

    void Start()
    {
        if (nextButton != null)
            nextButton.interactable = false;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (!other.CompareTag("Rock"))
            return;

        rockCount++;


        if (rockCount >= maxRocks)
        {
            Debug.Log("Basket is full!");
            CompleteTask();
            Destroy(other.gameObject);
        }
    }

    void CompleteTask()
    {

        if (nextButton != null)
            nextButton.interactable = true;

        if (spawner != null)
            spawner.CancelInvoke(); // Stops the InvokeRepeating in your SpawnFromTop script

        GameObject[] remainingRocks = GameObject.FindGameObjectsWithTag("Rock");
        foreach (GameObject rock in remainingRocks)
        {
            Destroy(rock);
        }
        GameObject[] Basket = GameObject.FindGameObjectsWithTag("Basket");
        foreach (GameObject basket in Basket)
        {
            basket.SetActive(false);
        }

        gameObject.SetActive(false);
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (!other.CompareTag("Rock"))
            return;

        rockCount--;

        if (rockCount < 0)
            rockCount = 0;

    }
    private void Update()
    {
        Debug.Log("Current count: " + rockCount);

    }
}
