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

namespace DXGfxLib.Serialization
{
    public class SkyDomeSurrogate : ISerializationSurrogate
    {
        /// <summary>
        /// This is called to serialize the Scene.
        /// </summary>
        /// <param name="obj">The scene object that need to be serialized.</param>
        /// <param name="info">where we have to write what we want to be serialized.</param>
        /// <param name="context"></param>
        public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context)
        {
            if (obj == null)
            {
                throw new NullReferenceException("No scene object to serialize, this is wrong!");
            }

            SkyDome dome = obj as SkyDome;

            if (dome == null)
            {
                throw new ArgumentException("The Object passed through obj argument is not a scene object, this is wrong!");
            }

            info.AddValue("FileName", dome.fileName, typeof(string));
        }

        /// <summary>
        /// This is called to deserialize a scene object.
        /// </summary>
        /// <param name="obj">The scene object we need to populate with values.</param>
        /// <param name="info">Serialized information we need to read and push to the scene object.</param>
        /// <param name="context"></param>
        /// <param name="selector"></param>
        /// <returns>null (ignored)</returns>
        public Object SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            if (obj == null)
            {
                throw new NullReferenceException("No scene object to serialize, this is wrong!");
            }

            SkyDome dome = obj as SkyDome;

            if (dome == null)
            {
                throw new ArgumentException("The Object passed through obj argument is not a terrain object, this is wrong!");
            }

            dome.fileName = (string)info.GetValue("FileName", typeof(string));

            return null;
        }
    }
}
