using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

using Motion6D;

namespace InterfaceOpenGL
{
    [Serializable()]
    public class ShapeGL : ISerializable, IVisible
    {

        #region Fields
        private string filename = "";
        private OpenGL_Library.ShapeGL shape = new OpenGL_Library.ShapeGL();

        #endregion


        #region Ctor

        internal ShapeGL()
        {
        }

        protected ShapeGL(SerializationInfo info, StreamingContext context)
        {
            filename = info.GetValue("FileName", typeof(string)) as string;
            init();
        }

        #endregion

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("FileName", filename, typeof(string));
        }

        #endregion

        #region Specific Members

        internal string Filename
        {
            get
            {
                return filename;
            }
            set
            {
                filename = value;
                init();
            }
        }
        #endregion

        #region Specific Members

        internal OpenGL_Library.ShapeGL Shape
        {
            get
            {
                return shape;
            }
        }

        private void init()
        {
            string fn = AppDomain.CurrentDomain.BaseDirectory + 
                Path.DirectorySeparatorChar + "Shapes" + Path.DirectorySeparatorChar + filename;
            if (!File.Exists(fn))
            {
                return;
            }
            Stream stream = File.OpenRead(fn);
            BinaryReader reader = new BinaryReader(stream);
            int n = reader.ReadInt32();
            double[] x = new double[12 * n];
            for (int i = 0; i < x.Length; i++)
            {
                x[i] = reader.ReadDouble();
            }
            shape.Set(n, x);
        }
        #endregion

    }
}
