using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Physics;
using Microsoft.Xna.Framework;

namespace Innovation
{
    // A box shaped physics object
    public class BoxObject : PhysicsObject
    {
        // The length of the sides of the box
        public Vector3 SideLengths
        {
            get { return ((Box)CollisionSkin.GetPrimitiveLocal(0)).SideLengths; }
            set { SetupSkin(value); }
        }

        // Constructors

        public BoxObject() : base()
        {
            InitializeBody();
            SetupSkin(Vector3.One);
        }

        public BoxObject(Vector3 SideLengths) : base()
        {
            InitializeBody();
            SetupSkin(SideLengths);
        }

        public BoxObject(Vector3 SideLengths, Vector3 Position, Vector3 Rotation) : base()
        {
            InitializeBody();
            SetupSkin(SideLengths);
            this.Position = Position;
            this.EulerRotation = Rotation;
        }

        // Sets up the object with the specified parameters
        void SetupSkin(Vector3 SideLengths)
        {
            // Update the collision skin
            CollisionSkin.RemoveAllPrimitives();
            CollisionSkin.AddPrimitive(
                new Box(-0.5f * SideLengths, Body.Orientation, SideLengths),
                (int)MaterialTable.MaterialID.UserDefined,
                PhysicsMaterial);
        }

        public override void SaveSerializationData(SerializationData Data)
        {
            base.SaveSerializationData(Data);
            Data.AddData("BoxObject.SideLengths", SideLengths);
        }

        public override void LoadFromSerializationData(SerializationData Data)
        {
            base.LoadFromSerializationData(Data);
            this.SideLengths = Data.GetData<Vector3>("BoxObject.SideLengths");
        }
    }
}