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

namespace SuperBallBrawlers
{
    public class Physical3DCylinderComponent : PhysicalBaseRigidBodyComponent
    {
        private float _radius;
        private float _height;

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

            Enabled = true;
        }

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

        public float Height
        {
            get { return _height; }
            set { _height = value; UpdateRigidBodyShape(); }
        }

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

            return new CylinderShape(_height, _radius);
        }

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

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