using System;
using Jitter.Collision.Shapes;
using Protogame;
using Protoinject;

namespace SuperBallBrawlers
{
    public class Physical3DSphereComponent : PhysicalBaseRigidBodyComponent
    {
        private float _radius;

        public Physical3DSphereComponent(INode node, IPhysicsEngine physicsEngine) : base(node, physicsEngine)
        {
            _radius = 1f;
            UpdateRigidBodyShape();

            Enabled = true;
        }

        public float Radius
        {
            get { return _radius; }
            set { _radius = value; UpdateRigidBodyShape(); }
        }

        protected override Shape GetShape(ITransform localTransform)
        {
            if (_radius <= 0)
            {
                throw new InvalidOperationException("Invalid radius.");
            }

            return new SphereShape(_radius);
        }

        public override void DeclareSynchronisedProperties(ISynchronisationApi synchronisationApi)
        {
            base.DeclareSynchronisedProperties(synchronisationApi);

            synchronisationApi.Synchronise("radius", 60, Radius, x => Radius = x, null);
        }
    }
}
