package com.bonus.common.houqin.mq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.rocketmq.spring.support.RocketMQMessageConverter;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.messaging.converter.*;
import org.springframework.util.ClassUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class MQMessageConverter extends RocketMQMessageConverter implements MessageConverter {
   private static final boolean JACKSON_PRESENT;
   private static final boolean FASTJSON_PRESENT;
   private static final ObjectMapper objectMapper = new ObjectMapper();
   private final CompositeMessageConverter messageConverter;

   public MQMessageConverter() {
      List<org.springframework.messaging.converter.MessageConverter> messageConverters = new ArrayList();
      ByteArrayMessageConverter byteArrayMessageConverter = new ByteArrayMessageConverter();
      byteArrayMessageConverter.setContentTypeResolver((ContentTypeResolver)null);
      messageConverters.add(byteArrayMessageConverter);
      messageConverters.add(new StringMessageConverter());
      if (JACKSON_PRESENT) {
         MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
         converter.setObjectMapper(objectMapper);
         messageConverters.add(converter);
      }

      if (FASTJSON_PRESENT) {
         try {
            messageConverters.add((org.springframework.messaging.converter.MessageConverter)ClassUtils.forName("com.alibaba.fastjson.support.spring.messaging.MappingFastJsonMessageConverter", ClassUtils.getDefaultClassLoader()).newInstance());
         } catch (IllegalAccessException | InstantiationException | ClassNotFoundException var4) {
         }
      }

      this.messageConverter = new CompositeMessageConverter(messageConverters);
   }

   @Override
   public org.springframework.messaging.converter.MessageConverter getMessageConverter() {
      return this.messageConverter;
   }

   @Override
   public Message toMessage(Object payload, MessageProperties messageProperties) throws MessageConversionException {
      try {
         if (messageProperties.getMessageId() == null) {
            messageProperties.setMessageId(UUID.randomUUID().toString());
         }

         byte[] bytes = new byte[0];
         if (payload instanceof byte[]) {
            messageProperties.setContentType("application/octet-stream");
         } else if (payload instanceof String) {
            bytes = ((String)payload).getBytes(StandardCharsets.UTF_8);
            messageProperties.setContentType("text/plain");
         } else {
            bytes = objectMapper.writeValueAsBytes(payload);
            messageProperties.setContentType("application/json");
         }

         return new Message(bytes, messageProperties);
      } catch (Throwable var4) {
         try {
            throw var4;
         } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
         }
      }
   }

   @Override
   public Object fromMessage(Message message) throws MessageConversionException {
      throw new UnsupportedOperationException();
   }

   public static ObjectMapper getObjectMapper() {
      return objectMapper;
   }

   static {
      objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
      objectMapper.registerModule(new JavaTimeModule());
      ClassLoader classLoader = MQMessageConverter.class.getClassLoader();
      JACKSON_PRESENT = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) && ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
      FASTJSON_PRESENT = ClassUtils.isPresent("com.alibaba.fastjson.JSON", classLoader) && ClassUtils.isPresent("com.alibaba.fastjson.support.config.FastJsonConfig", classLoader);
   }
}
