using UnityEngine;

namespace pdinklag.CPW.Common
{
    /// <summary>
    /// A climbable ledge.
    /// </summary>
    public class Ledge : MonoBehaviour
    {
        [Tooltip("The collider specifying the ledge's dimensions; expected to be aligned with the object's rotation.")]
        public BoxCollider Collider;

        [Tooltip("Allow wrestlers to be rolled onto this ledge.")]
        public bool RollTarget = true;

#if UNITY_EDITOR
        private static readonly GUIStyle ErrorMessageStyle = new GUIStyle();

        private void OnDrawGizmos()
        {
            var c = Collider ? Collider : GetComponentInChildren<BoxCollider>();
            if(!c) return;

            var xform = transform;

            var rot = xform.rotation.eulerAngles;
            var xzAxisRotation = (rot.x != 0 || rot.z != 0);
            var color = xzAxisRotation ? Color.red : Color.green;

            var pos = xform.TransformPoint(c.center);

            var facing = xform.forward;
            var right = xform.right;
            var up = xform.up;

            var width = c.size.x;
            var height = c.size.y;
            var depth = 2.0f * c.size.z;

            var bottom = pos - 0.5f * height * up;
            var top = pos + 0.5f * height * up;

            DebugExtension.DrawArrow(bottom, 0.5f * depth * facing, color);
            DebugExtension.DrawArrow(top, -0.5f * depth * facing, color);
            Debug.DrawLine(top, bottom, color);
            Debug.DrawLine(top - 0.5f * width * right, top + 0.5f * width * right, color);
            Debug.DrawLine(bottom - 0.5f * width * right, bottom + 0.5f * width * right, color);

            if (xzAxisRotation)
            {
                ErrorMessageStyle.normal.textColor = Color.red;
                UnityEditor.Handles.Label(xform.position, "X and Z axis rotation is not supported!", ErrorMessageStyle);
            }
        }
#endif
    }
}
