using UnityEditor;
using UnityEngine;

namespace Intrepid.Core.Utilities
{
    public static class GizmoUtils
    {
        public static void DrawWireCapsule(
            Vector3 position,
            Quaternion rotation,
            float radius,
            float height,
            Color color)
        {
            radius = Mathf.Max(0f, radius);
            height = Mathf.Max(height, radius * 2f);

            var oldColor = Handles.color;
            var oldMatrix = Handles.matrix;

            Handles.color = color;
            Handles.matrix = Matrix4x4.TRS(position, rotation, Vector3.one);

            var halfHeight = height * 0.5f;
            var cylinderHalfHeight = halfHeight - radius;

            var top = Vector3.up * cylinderHalfHeight;
            var bottom = Vector3.down * cylinderHalfHeight;

            // Rings
            Handles.DrawWireDisc(top, Vector3.up, radius);
            Handles.DrawWireDisc(bottom, Vector3.up, radius);

            // Side lines
            Handles.DrawLine(bottom + Vector3.forward * radius, top + Vector3.forward * radius);
            Handles.DrawLine(bottom - Vector3.forward * radius, top - Vector3.forward * radius);
            Handles.DrawLine(bottom + Vector3.right * radius, top + Vector3.right * radius);
            Handles.DrawLine(bottom - Vector3.right * radius, top - Vector3.right * radius);

            // Top hemisphere
            Handles.DrawWireArc(top, Vector3.right, Vector3.forward * radius, 180f, radius);
            Handles.DrawWireArc(top, Vector3.forward, Vector3.right * radius, -180f, radius);

            // Bottom hemisphere
            Handles.DrawWireArc(bottom, Vector3.right, -Vector3.forward * radius, 180f, radius);
            Handles.DrawWireArc(bottom, Vector3.forward, Vector3.right * radius, 180f, radius);

            Handles.color = oldColor;
            Handles.matrix = oldMatrix;
        }
        
    }
}