package com.foreverwin.mesnac.common.config;


import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;

@Configuration
@ConditionalOnProperty(prefix = "spring.activemq", value = {"enabled"}, matchIfMissing = true)
public class ActiveMQConfig {

    //☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
    @Primary
    @Bean(name = "defaultConnectionFactory")
    public ActiveMQConnectionFactory defaultConnectionFactory(
            @Value("${spring.activemq.brokerUrl}") String brokerUrl,
            @Value("${spring.activemq.user}") String username,
            @Value("${spring.activemq.password}") String password) {

            return this.createConnectionFactory(brokerUrl, username, password);
    }

    @Bean(name = "mesConnectionFactory")
    public ActiveMQConnectionFactory mesConnectionFactory(
            @Value("${activemq.brokerUrl}") String brokerUrl,
            @Value("${activemq.user}") String username,
            @Value("${activemq.password}") String password) {

        return this.createConnectionFactory(brokerUrl, username, password);
    }


    private ActiveMQConnectionFactory createConnectionFactory (String brokerUrl, String username, String password) {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(brokerUrl);
        factory.setUserName(username);
        factory.setPassword(password);
        return factory;
    }

    //**************************************************************************************************************
    @Primary
    @Bean(name = "defaultJmsTemplate")
    public JmsMessagingTemplate defaultJmsTemplate(
            @Qualifier("defaultConnectionFactory") ActiveMQConnectionFactory connectionFactory) {

        JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory);
        return  template;
    }

    @Bean(name = "mesJmsTemplate")
    public JmsMessagingTemplate mesJmsTemplate(
            @Qualifier("mesConnectionFactory") ActiveMQConnectionFactory connectionFactory) {

        JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory);
        return  template;
    }


    //☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
    @Primary
    @Bean(name = "defaultFactory")
    public JmsListenerContainerFactory defaultFactory(
            @Qualifier("defaultConnectionFactory") ActiveMQConnectionFactory connectionFactory) {

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

    @Bean(name = "mesFactory")
    public JmsListenerContainerFactory mesFactory(
            @Qualifier("mesConnectionFactory") ActiveMQConnectionFactory connectionFactory) {

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }
}
