﻿using UnityEngine;

namespace pdinklag.CPW.Common
{
    /// <summary>
    /// Component for body parts approximated by a box.
    /// </summary>
    public class BodyPartBox : BodyPartVolumetric, IBodyPart
    {
        public Vector3 Size = new Vector3(0.1f, 0.1f, 0.1f);
        Vector3 IBodyPart.Size => Size;

        public override Vector3 Top { get { return transform.position + 0.5f * Size.y * transform.up; } }
        public override Vector3 Bottom { get { return transform.position - 0.5f * Size.y * transform.up; } }
        public override Vector3 Right { get { return transform.position + 0.5f * Size.x * transform.right; } }
        public override Vector3 Left { get { return transform.position - 0.5f * Size.x * transform.right; } }
        public override Vector3 Front { get { return transform.position + 0.5f * Size.z * transform.forward; } }
        public override Vector3 Back { get { return transform.position - 0.5f * Size.z * transform.forward; } }

        public override float Volume { get { return Size.x * Size.y * Size.z; } }

        public override Collider CreateCollider(GameObject go)
        {
            var c = go.AddComponent<BoxCollider>();
            c.size = Size;
            return c;
        }

        public override void CopyData(BodyPartComponent other)
        {
            base.CopyData(other);

            var box = other as BodyPartBox;
            if(box)
            {
                Size = box.Size;
            }
        }

#if UNITY_EDITOR
        private void OnDrawGizmos()
        {
            DebugExtension.DrawLocalCube(Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one), Size, GizmoColor);
        }
#endif
    }
}
