﻿using UnityEngine;

namespace pdinklag.CPW.Common
{
    public class BodyPartSphere : BodyPartVolumetric, IBodyPart
    {
        public float Radius = 0.1f;

        public Vector3 Size
        {
            get
            {
                var d = 2.0f * Radius;
                return new(d, d, d);
            }
        }

        public override Vector3 Top { get { return transform.position + Radius * transform.up; } }
        public override Vector3 Bottom { get { return transform.position - Radius * transform.up; } }
        public override Vector3 Right { get { return transform.position + Radius * transform.right; } }
        public override Vector3 Left { get { return transform.position - Radius * transform.right; } }
        public override Vector3 Front { get { return transform.position + Radius * transform.forward; } }
        public override Vector3 Back { get { return transform.position - Radius * transform.forward; } }

        public override float Volume { get { return MathExt.SphereVolumeCoeff * (Radius * Radius * Radius); } }

        public override Collider CreateCollider(GameObject go)
        {
            var c = go.AddComponent<SphereCollider>();
            c.radius = Radius;
            return c;
        }

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

            var sphere = other as BodyPartSphere;
            if (sphere)
            {
                Radius = sphere.Radius;
            }
        }

        private void OnDrawGizmos()
        {
            Gizmos.color = GizmoColor;
            Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
            Gizmos.DrawWireSphere(Vector3.zero, Radius);
        }
    }
}
