using InABox.Core;

namespace InABox.IPC
{
    public class RPCSaveCommand : IRPCCommand<RPCSaveParameters, RPCSaveResponse> { }
    
    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;
        }
    }
    
    public class RPCSaveParameters : ISerializeBinary
    {
        public RPCSaveItem[] Items { get; set; }

        public RPCSaveParameters()
        {
            Items = new RPCSaveItem[] { };
        }
        
        public void SerializeBinary(CoreBinaryWriter writer)
        {
            writer.WriteBinaryValue(typeof(RPCSaveItem[]), Items);
        }

        public void DeserializeBinary(CoreBinaryReader reader)
        {
            Items = reader.ReadBinaryValue(typeof(RPCSaveItem[])) as RPCSaveItem[] ?? new RPCSaveItem[] { };
            ;
        }
    }
    
    public class RPCSaveResponse
    {
    }
}