using InABox.Core;

namespace InABox.IPC
{
    public abstract class RPCCommandHandler<TSender, TParameters, TResult> : IRPCCommandHandler where TSender : class
    {
        public TSender Sender { get; }

        public RPCCommandHandler(TSender sender)
        {
            Sender = sender ?? throw new ArgumentNullException(nameof(sender));
        }

        public abstract TResult? Execute(TParameters? parameters);

        public String? Execute(String? parameters)
        {
            var props = Serialization.Deserialize<TParameters>(parameters); 
            var result = Execute(props);
            return Serialization.Serialize(result);
        }
    }
}