#if Server
using System;
using System.Text;
using MessagePack;
using MessagePack.Formatters;
using MessagePack.Resolvers;
using Newtonsoft.Json;

namespace GameFix.Poker
{
    public class DeckSerializer
    {
        private Deck mDeck;
        private MessagePackSerializerOptions mOptions;
        private JsonSerializerSettings mMemSaveJsonSet;
        
        public DeckSerializer(Deck deck)
        {
            mDeck = deck;
            // MessagePack
            var resolver = CompositeResolver.Create(
                new IMessagePackFormatter[]
                {
                    new CardMessagePackFormatter(mDeck),
                },
                new IFormatterResolver[] 
                {
                    StandardResolver.Instance
                }
            );
            mOptions = MessagePackSerializerOptions.Standard
                .WithResolver(resolver);
                    // .WithCompression(MessagePackCompression.Lz4BlockArray);
            
            // Json
            mMemSaveJsonSet = new JsonSerializerSettings();
            mMemSaveJsonSet.DefaultValueHandling = DefaultValueHandling.Ignore;
            mMemSaveJsonSet.Converters = new JsonConverter[] { new CardIdJsonConverter(mDeck) };
        }

        public byte[] Serialize<T>(T value, bool isJson = false)
        {
            if (isJson)
            {
                string jsonData = JsonHelper.SerializeObject(value, mMemSaveJsonSet);
                return Encoding.UTF8.GetBytes(jsonData);
            }
            
            return MessagePackSerializer.Serialize(value, mOptions);
        }
        
        public T Deserialize<T>(byte[] data, bool isJson = false)
        {
            if (isJson)
            {
                string jsondata = Encoding.UTF8.GetString(data);
                return JsonHelper.DeserializeObject<T>(jsondata, mMemSaveJsonSet);
            }
            
            return MessagePackSerializer.Deserialize<T>(data, mOptions);
        }
    }
}

#else
namespace GameFix.Poker
{
    public class DeckSerializer
    {
        private Deck mDeck;
        public DeckSerializer(Deck deck)
        {
            mDeck = deck;
        }
    }
}
#endif