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

namespace DXGfxLib.Serialization
{
    public class SceneSurrogate : 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 to serialize, this is wrong!");
            }

            OutDoorScene scene = obj as OutDoorScene;

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

            info.AddValue("Name", scene.name, typeof(string));
            info.AddValue("SceneContent", scene.sceneContent, typeof(Hashtable));

        }

        /// <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 to serialize, this is wrong!");
            }

            OutDoorScene scene = obj as OutDoorScene;

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

            scene.name = (string)info.GetValue("Name", typeof(string));
            scene.sceneContent = (Hashtable)info.GetValue("SceneContent", typeof(Hashtable));

            return null;
        }

    }
}
