using System.Linq;
using Assets.Common;
using UnityEditor;
using UnityEngine;

namespace Assets.Utils
{
    public static class DebugUtils
    {
        public delegate Point PointExtractor<T>(T thing);
        
        public static void DisplayGraphVertices<T>(Graph<T> graph, PointExtractor<T> extractor, bool displayLabels = true)
        {
            var vertices = graph.Vertices.Select(p => extractor(p).ToVector3());
            var offset   = Vector3.right + Vector3.up;
            
            foreach (var (v, i) in vertices.Select((x, i) => (x, i)))
            {
                Gizmos.DrawSphere(v, 1);
                #if UNITY_EDITOR
                if (displayLabels) Handles.Label(v + offset, $"{i} at {v.x:F2}, {v.z:f2}");
                #endif
            }
        }

        public static void DisplayGraphVertices(Graph<Point> graph, bool displayLabels = true)
        {
            DisplayGraphVertices(graph, l => l, displayLabels);
        }
        
        public static void DisplayGraphEdges<T>(Graph<T> graph, PointExtractor<T> extractor)
        {
            var edges = graph.Edges
                .Select(edge => (graph.Vertices[edge.Item1], graph.Vertices[edge.Item2]))
                .Select(edge => (extractor(edge.Item1).ToVector3(), extractor(edge.Item2).ToVector3()));
            
            foreach (var (s, e) in edges) Gizmos.DrawLine(s, e);
        }

        public static void DisplayGraphEdges(Graph<Point> graph)
        {
            DisplayGraphEdges(graph, l => l);
        }
    }
}