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

namespace Innovation
{
    // A flat plane physics object
    public class PlaneObject : PhysicsObject
    {
        public float Distance
        {
            get { return ((JigLibX.Geometry.Plane)CollisionSkin.GetPrimitiveLocal(0)).D; }
            set { SetupSkin(value, Normal); }
        }

        public Vector3 Normal
        {
            get { return ((JigLibX.Geometry.Plane)CollisionSkin.GetPrimitiveLocal(0)).Normal; }
            set { SetupSkin(Distance, value); }
        }

        // Constructors

        public PlaneObject() : base()
        {
            InitializeBody();
            Body.Immovable = true;

            SetupSkin(0, Vector3.Up);
        }

        public PlaneObject(float Distance)
        {
            InitializeBody();
            Body.Immovable = true;

            SetupSkin(Distance, Vector3.Up);
        }

        public PlaneObject(Vector3 Normal, float Distance)
        {
            InitializeBody();
            Body.Immovable = true;

            SetupSkin(Distance, Normal);
        }

        // Setup everything
        void SetupSkin(float Distance, Vector3 Normal)
        {
            CollisionSkin.AddPrimitive(
                new JigLibX.Geometry.Plane(Normal, Distance),
                (int)MaterialTable.MaterialID.UserDefined,
                PhysicsMaterial);
        }

        public override void LoadFromSerializationData(SerializationData Data)
        {
            base.LoadFromSerializationData(Data);

            this.Distance = Data.GetData<float>("PlaneObject.Distance");
            this.Normal = Data.GetData<Vector3>("PlaneObject.Normal");
        }

        public override void SaveSerializationData(SerializationData Data)
        {
            base.SaveSerializationData(Data);

            Data.AddData("PlaneObject.Distance", Distance);
            Data.AddData("PlaneObject.Normal", Normal);
        }
    }
}
