using System.Collections.Generic;
using UnityEngine;

public class DemonLogic : MonoBehaviour
{
    private float maximalDistance = 10.0f; // The maximum distance to consider a collider visible
    private float spreadAngle = 45.0f; // The angle spread of the vision cone

    [SerializeField] private LineRenderer lineRenderer;

    List<Collider2D> visibleColliders = new List<Collider2D>(); // List of visible colliders


    private Vector3 myPostition;

    void Update()
    {
        /*
        // Clear the list of visible colliders
        visibleColliders.Clear();

        // Get the player's position
        myPostition = transform.position;

        // Cast multiple rays along the player's forward direction with a slight spread
        for (int i = 0; i < 18; i++)
        {
            float angle = (i * spreadAngle) / 17.0f;
            Vector2 rayDirection = Quaternion.Euler(0.0f, angle, 0.0f) * Vector2.forward;
            Lincast2D ray = new Linecast2D(myPostition, myPostition + rayDirection, 0.0f, maximalDistance);

            // Check if the ray intersects with any colliders within the specified maximum distance
            RaycastHit2D hit = new RaycastHit2D();
            if (Physics2D.Linecast(ray, out hit))
            {
                // Add the collider to the list of visible colliders
                visibleColliders.Add(hit.collider);
            }
        }

        // Sort the visible colliders by distance from the player
        visibleColliders.Sort((a, b) => Vector2.Distance(myPostition, a.transform.position) - Vector2.Distance(myPostition, b.transform.position));

        // Create outline fragments for each visible collider
        foreach (Collider2D collider in visibleColliders)
        {
            // Create a list of outline fragments for the collider
            List<Vector2> outlineFragments = new List<Vector2>();

            // Calculate the points on the collider's surface that are facing the player
            MeshCollider2D meshCollider = collider as MeshCollider2D;
            if (meshCollider != null)
            {
                for (int i = 0; i < meshCollider.mesh.vertexCount; i++)
                {
                    Vector2 vertexPosition = meshCollider.mesh.vertices[i];
                    Vector2 normalVector = meshCollider.mesh.normals[i];

                    // Check if the normal vector points towards the player
                    if (Vector2.Dot(normalVector, myPostition - vertexPosition) >= 0.0f)
                    {
                        outlineFragments.Add(vertexPosition);
                    }
                }
            }

            // Create line segments from the outline fragments
            for (int i = 1; i < outlineFragments.Count; i++)
            {

                lineRenderer.startWidth = 0.1f;
                lineRenderer.endWidth = 0.1f;
                lineRenderer.positionCount = 2;
                lineRenderer.SetPositions(new Vector2[] { outlineFragments[i - 1], outlineFragments[i] });
                Instantiate(lineRenderer);
            }
        }
        */
    }

}
