using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Jovian.Utilities.Utilities {
    public static class GizmosUtility {

        public static bool IsGameObjectOrChildSelected(GameObject gameObject) {
#if UNITY_EDITOR
            return GameObjectUtilities.IsGameObjectAChildOfGameObject(Selection.activeGameObject, gameObject);
#else
            return false;
#endif
        }

        public static void DrawColliders(IEnumerable<Collider> colliders) {
            foreach(Collider collider in colliders) {
                if(collider != null) {
                    DrawCollider(collider);
                }
            }
        }

        public static void DrawWireColliders(IEnumerable<Collider> colliders) {
            foreach(Collider collider in colliders) {
                if(collider != null) {
                    DrawWireCollider(collider);
                }
            }
        }

        public static void DrawCollider(Collider collider)
            => DrawColliderInternal(collider, Gizmos.DrawSphere, Gizmos.DrawCube, Gizmos.DrawMesh);

        public static void DrawWireCollider(Collider collider)
            => DrawColliderInternal(collider, Gizmos.DrawWireSphere, Gizmos.DrawWireCube, Gizmos.DrawWireMesh);

        private static void DrawColliderInternal(Collider collider, Action<Vector3, float> drawSphere, Action<Vector3, Vector3> drawCube,
            Action<Mesh> drawMesh) {
            Gizmos.matrix = collider.transform.localToWorldMatrix;
            switch(collider) {
                case BoxCollider boxCollider:
                    drawCube(boxCollider.center, boxCollider.size);
                    break;
                case SphereCollider sphereCollider:
                    drawSphere(sphereCollider.center, sphereCollider.radius);
                    break;
                case CapsuleCollider capsuleCollider: {
                    Vector3 direction = GetAxis(capsuleCollider.direction);
                    drawSphere(capsuleCollider.center + direction * capsuleCollider.height * 0.5f, capsuleCollider.radius);
                    drawSphere(capsuleCollider.center - direction * capsuleCollider.height * 0.5f, capsuleCollider.radius);
                    break;
                }
                case MeshCollider meshCollider:
                    drawMesh(meshCollider.sharedMesh);
                    break;
                default:
                    throw new NotSupportedException($"Cannot draw collider of type '{typeof(Collider)}'");
            }
        }

        private static Vector3 GetAxis(int direction) {
            switch(direction) {
                case 0:
                    return Vector3.right;
                case 1:
                    return Vector3.up;
                case 2:
                    return Vector3.forward;
                default:
                    throw new NotSupportedException($"Direction '{direction}' does not map to an axis.");
            }
        }
    }
}
