package com.pj.rabbitmq;

import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * @Author Mechrevo
 * @Date 2023 08 25 12 30
 **/
@Component
public class MessageSender {

    private final RabbitTemplate levelOneConnectionFactory;
    private final RabbitTemplate levelTwoConnectionFactory;
    private final RabbitTemplate customsConnectionFactory;
    private final RabbitMQProperties rabbitMQProperties;

    @Autowired
    public MessageSender(@Qualifier("levelOneConnectionFactory") CachingConnectionFactory levelOneConnectionFactory,
                         @Qualifier("levelTwoConnectionFactory") CachingConnectionFactory levelTwoConnectionFactory,
                         @Qualifier("customsConnectionFactory") CachingConnectionFactory customsConnectionFactory,
                         RabbitMQProperties rabbitMQProperties) {
        this.levelOneConnectionFactory = new RabbitTemplate(levelOneConnectionFactory);
        this.levelTwoConnectionFactory = new RabbitTemplate(levelTwoConnectionFactory);
        this.customsConnectionFactory = new RabbitTemplate(customsConnectionFactory);
        this.rabbitMQProperties = rabbitMQProperties;
    }

    public void sendToLevelOneMQ(String message) {
        //levelTwoConnectionFactory.setMessageConverter(new Jackson2JsonMessageConverter());
        levelOneConnectionFactory.convertAndSend(rabbitMQProperties.getLevelOneSendQueue(), message);
        //levelOneConnectionFactory.convertAndSend(rabbitMQProperties.getLevelOneReceiveQueue(), (Object) message,messagePostProcessor);
    }
    public void sendToLevelTwoMQ(String message) {
        //levelTwoConnectionFactory.setMessageConverter(new Jackson2JsonMessageConverter());
        levelTwoConnectionFactory.convertAndSend(rabbitMQProperties.getLevelTwoSendQueue(), message);
    }
    public void sendToCustomsMQ(String message) {
        customsConnectionFactory.convertAndSend(rabbitMQProperties.getCustomsSendQueue(), message);
    }

    private final static MessagePostProcessor messagePostProcessor = message -> {
        message.getMessageProperties().setContentType("application/octet-stream");
        message.getMessageProperties().setContentEncoding("UTF-8");
        return message;
    };

}
