package com.zhongshu.card.server.core.service.mqtt.mqttConfig.config;

import com.zhongshu.card.server.core.service.mqtt.mqttConfig.conf.MQConf;
import com.zhongshu.card.server.core.service.mqtt.mqttConfig.constant.MQConstant;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;


@Configuration
public class ConsumerConfiguration {

    private static final Logger log = LoggerFactory.getLogger(ConsumerConfiguration.class);
    @Value("${spring.artemis.broker-url}")
    private String host;

    @Value("${spring.artemis.user}")
    private String userName;

    @Value("${spring.artemis.password}")
    private String passWord;


    @Autowired
    private MQConf mqConf;

    /**
     * 配置预期量
     *
     * @return
     */
//    @Bean
//    public ActiveMQPrefetchPolicy activeMQPrefetchPolicy() {
//        ActiveMQPrefetchPolicy prefetchPolicy = new ActiveMQPrefetchPolicy();
//        prefetchPolicy.setTopicPrefetch(mqConf.getMessageThreadPoolCount() * 2);
//        prefetchPolicy.setDurableTopicPrefetch(mqConf.getMessageThreadPoolCount() * 2);
//        prefetchPolicy.setMaximumPendingMessageLimit(mqConf.getMessageThreadPoolCount() * 2);
//        return prefetchPolicy;
//    }
    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        log.info("init  connectionFactory");
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(host, userName, passWord);
//        activeMQConnectionFactory.setPrefetchPolicy(activeMQPrefetchPolicy());
//        //采用异步投递消息
//        activeMQConnectionFactory.setAlwaysSyncSend(true);
//        //优化自动确认
//        activeMQConnectionFactory.setOptimizeAcknowledge(true);
//
//        activeMQConnectionFactory.setMaxRetryInterval();
//        activeMQConnectionFactory.set
        return activeMQConnectionFactory;
    }


    @Bean
    public JmsTemplate jmsTemplate() throws JMSException {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(connectionFactory());
        //setting PuSubDomain to true configures JmsTemplate to work with topics instead of queues
        jmsTemplate.setPubSubDomain(true);
        return jmsTemplate;
    }

    @Bean(name = MQConstant.TopicListenerContainerFactory)
    public JmsListenerContainerFactory topicListenerContainerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }
}
