using System;
using System.Linq;
using InABox.Core;

namespace InABox.Rpc
{
    public class RpcSaveParameters : IRpcCommandParameters
    {
        public Type Type { get; set; }
        public Entity[] Items { get; set; }
        public string AuditNote { get; set; }
        
        public bool ExcludeCustomProperties { get; set; }

        public string CommandName => "Save";

        public RpcSaveParameters()
        {
            Items = Array.Empty<Entity>();
            AuditNote = "";
        }
        
        public void SerializeBinary(CoreBinaryWriter writer)
        {
            writer.Write(Type.EntityName());
            writer.WriteObjects(Type, Items, (p) => ExcludeCustomProperties ? p is StandardProperty : true);
            writer.Write(AuditNote);
        }
        
        public void DeserializeBinary(CoreBinaryReader reader)
        {
            var type = reader.ReadString();
            Type = CoreUtils.GetEntity(type);
            Items = reader.ReadObjects<Entity>(Type).ToArray();
            AuditNote = reader.ReadString();
        }

        public string FullDescription() => $"Save{Type.Name}({Items.Length}) Data=[{string.Join<Entity>(", ", Items)}]";

        public string ShortDescription() => $"Save{Type.Name}({Items.Length})";
    }
}