using UnityEngine;

/// <summary>
/// Manages the boundaries of the game world.
/// </summary>
public class BoundaryManager : MonoBehaviour
{
    public Camera mainCamera; // Reference to the main camera

    public static BoundaryManager instance; // Singleton instance of BoundaryManager

    /// <summary>
    /// Gets the minimum bounds of the game world.
    /// </summary>
    public Vector2 MinBounds { get; private set; }

    /// <summary>
    /// Gets the maximum bounds of the game world.
    /// </summary>
    public Vector2 MaxBounds { get; private set; }

    /// <summary>
    /// Initializes the singleton instance and sets the bounds based on the camera size.
    /// </summary>
    private void Awake()
    {
        instance = this;

        // Set the bounds based on the camera size
        SetBounds();
    }

    /// <summary>
    /// Calculates and sets the bounds of the game world based on the main camera size.
    /// </summary>
    private void SetBounds()
    {
        float camHalfHeight = mainCamera.orthographicSize;
        float camHalfWidth = camHalfHeight * mainCamera.aspect;

        MinBounds = new Vector2(-camHalfWidth, -camHalfHeight);
        MaxBounds = new Vector2(camHalfWidth, camHalfHeight);
    }

    /// <summary>
    /// Checks if a given position is within the bounds of the game world.
    /// </summary>
    /// <param name="position">The position to check.</param>
    /// <returns>True if the position is within bounds, false otherwise.</returns>
    public bool IsWithinBounds(Vector3 position)
    {
        // Convert the position to screen space
        Vector3 screenPoint = Camera.main.WorldToScreenPoint(position);

        // Check if the given position is within the screen boundaries
        return screenPoint.x > 0 && screenPoint.x < Screen.width &&
               screenPoint.y > 0 && screenPoint.y < Screen.height;
    }
}
