using InABox.Core;

namespace InABox.IPC
{
    [Serializable]
    public class RPCMessage : ISerializeBinary
    {
        public Guid ID { get; set; }
        public String Command { get; set; }
        public string Payload { get; set; }
        public RPCError Error { get; set; }

        public RPCMessage()
        {
            ID = Guid.NewGuid();
            Command = "";
            Payload = "";
            Error = RPCError.NONE;
        }

        public RPCMessage(Guid id, String command, String payload, RPCError error = RPCError.NONE)
        {
            ID = id;
            Command = command;
            Payload = payload;
            Error = error;
        }

        public void SerializeBinary(CoreBinaryWriter writer)
        {
            writer.Write(ID);
            writer.Write(Command);
            writer.Write(Payload);
            writer.Write(Error.ToString());
        }

        public void DeserializeBinary(CoreBinaryReader reader)
        {
            ID = reader.ReadGuid();
            Command = reader.ReadString();
            Payload = reader.ReadString();
            if (Enum.TryParse<RPCError>(reader.ReadString(), out var error))
                Error = error;
        }
    }
}