using System;
using InABox.Core;

namespace InABox.Rpc
{
    public class RpcSaveItem : ISerializeBinary
    {
        public Guid Key { get; set; }
        public Type Type { get; set; }
        public Entity? Entity { get; set; }

        public RpcSaveItem()
        {
            Key = Guid.NewGuid();
        }
        
        public void SerializeBinary(CoreBinaryWriter writer)
        {
            writer.Write(Key);
            writer.Write(Type.EntityName());
            writer.WriteBinaryValue(Type, Entity);
        }

        public void DeserializeBinary(CoreBinaryReader reader)
        {
            Key = reader.ReadGuid();
            Type = CoreUtils.GetEntity(reader.ReadString());
            Entity = reader.ReadBinaryValue(Type) as Entity;
        }
    }
}