using System;
using UnityEngine;
using UnityEngine.Rendering;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace pdinklag.CPW.Common
{
    [Serializable]
    public struct BoneOrientation
    {
        public static BoneOrientation Default => new BoneOrientation(Axis.Forward, Axis.Up);

        public Axis ForwardAxis, UpAxis;

        public BoneOrientation(Axis fwd, Axis up)
        {
            ForwardAxis = fwd;
            UpAxis = up;
        }

        public Vector3 Forward() => ForwardAxis.Get();
        public Vector3 Back() => -ForwardAxis.Get();
        public Vector3 Up() => UpAxis.Get();
        public Vector3 Down() => -UpAxis.Get();
        public Vector3 Right() => Vector3.Cross(UpAxis.Get(), ForwardAxis.Get());
        public Vector3 Left() => -Vector3.Cross(UpAxis.Get(), ForwardAxis.Get());

        public Vector3 Forward(Transform xform) => ForwardAxis.For(xform);
        public Vector3 Back(Transform xform) => -ForwardAxis.For(xform);
        public Vector3 Up(Transform xform) => UpAxis.For(xform);
        public Vector3 Down(Transform xform) => -UpAxis.For(xform);
        public Vector3 Right(Transform xform) => Vector3.Cross(UpAxis.For(xform), ForwardAxis.For(xform));
        public Vector3 Left(Transform xform) => -Vector3.Cross(UpAxis.For(xform), ForwardAxis.For(xform));

#if UNITY_EDITOR
        public void DrawHandles(Transform bone, float size = 0.1f)
        {
            if (bone)
            {
                Handles.zTest = CompareFunction.LessEqual;
                Handles.color = Color.blue;
                Handles.ArrowHandleCap(-1, bone.position, Quaternion.LookRotation(Forward(bone)), size, EventType.Repaint);
                Handles.color = Color.green;
                Handles.ArrowHandleCap(-1, bone.position, Quaternion.LookRotation(Up(bone)), size, EventType.Repaint);
                Handles.color = Color.red;
                Handles.ArrowHandleCap(-1, bone.position, Quaternion.LookRotation(Right(bone)), size, EventType.Repaint);

                Handles.zTest = CompareFunction.Greater;
                Handles.color = Color.blue.WithAlpha(0.25f);
                Handles.ArrowHandleCap(-1, bone.position, Quaternion.LookRotation(Forward(bone)), size, EventType.Repaint);
                Handles.color = Color.green.WithAlpha(0.25f);
                Handles.ArrowHandleCap(-1, bone.position, Quaternion.LookRotation(Up(bone)), size, EventType.Repaint);
                Handles.color = Color.red.WithAlpha(0.25f);
                Handles.ArrowHandleCap(-1, bone.position, Quaternion.LookRotation(Right(bone)), size, EventType.Repaint);
            }
        }

        public void DrawGizmos(Transform bone, float size = 0.1f)
        {
            if (bone)
            {
                DebugExtension.DrawArrow(bone.position, size * Forward(bone), Color.blue);
                DebugExtension.DrawArrow(bone.position, size * Up(bone), Color.green);
                DebugExtension.DrawArrow(bone.position, size * Right(bone), Color.red);
            }
        }
#endif
    }
}
