#if Server
using MessagePack;
using MessagePack.Formatters;

namespace GameFix.Poker
{
    public class CardMessagePackFormatter : IMessagePackFormatter<Card>
    {
        ICardIdResolver mResolver;

        public CardMessagePackFormatter(ICardIdResolver resolver)
        {
            mResolver = resolver;
        }
        
        public void Serialize(ref MessagePackWriter writer, Card value, MessagePackSerializerOptions options)
        {
            if (value == null)
            {
                writer.WriteNil();
                return;
            }
            writer.WriteInt32(value.Id);
        }

        public Card Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            if (reader.TryReadNil())
            {
                return null;
            }
            
            return mResolver.GetById(reader.ReadInt32());

        }
    }
}
#endif