package com.bocloud.ims.booter.config;

import com.bocloud.common.utils.MapTools;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.Map;

/**
 * rabbitmq相关配置
 *
 * @author wangyu
 * @version 1.0
 * @since 2018年4月16日
 */
@Configuration
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitMQConfiguration {

	@Autowired
	private ConnectionFactory connectionFactory;
	private Map<String, Object> arguments = MapTools.simpleMap("x-ha-policy", "all");

	/**
	 * ==============================================================================================================
	 * 定义direct exchange
	 *
	 * @return
	 */
	@Bean
	public DirectExchange declareDirectExchange() {
		return new DirectExchange("bocloud.direct.exchange", true, true, arguments);
	}
	
	/**
	 * 用于cop主机统计
	 * @return
	 */
	@Bean
	public Queue declareForCopTaskHostQueue() {
		return new Queue("bocloud.taskhost.operate", true, false, false, arguments);
	}

	@Bean
	public Binding bindingForCopTaskHostQueue() {
		return BindingBuilder.bind(declareForCopTaskHostQueue()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 堡垒机部门同步消息回调队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareFortressCallbackQueueDept() {
		return new Queue("fortress.callback.queue.dept", true, false, false, arguments);
	}

	@Bean
	public Binding bindingFortressCallbackQueueDept() {
		return BindingBuilder.bind(declareFortressCallbackQueueDept()).to(declareDirectExchange()).withQueueName();
	}

	/** @Author: lxy
	 * @Description: 堡垒机接收创建vm回复信息
	 * @Date 2021/5/13 17:33
	 * @Param
	 * @return org.springframework.amqp.core.Queue
	 * @throws:
	 **/
	@Bean
	public Queue declareSyncShtermCallbackQueue() {
		return new Queue("sync.shterm.callback.queue", true, false, false, arguments);
	}

	@Bean
	public Binding bindingSyncShtermCallbackQueue() {
		return BindingBuilder.bind(declareSyncShtermCallbackQueue()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 堡垒机项目同步消息回调队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareFortressCallbackQueueProject() {
		return new Queue("fortress.callback.queue.project", true, false, false, arguments);
	}

	@Bean
	public Binding bindingFortressCallbackQueueProject() {
		return BindingBuilder.bind(declareFortressCallbackQueueProject()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 堡垒机用户同步消息回调队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareFortressCallbackQueueUser() {
		return new Queue("fortress.callback.queue.user", true, false, false, arguments);
	}

	@Bean
	public Binding bindingFortressCallbackQueueUser() {
		return BindingBuilder.bind(declareFortressCallbackQueueUser()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 堡垒机部门、资源组、用户同步队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareFortressSyncQueue() {
		return new Queue("fortress.sync.queue", true, false, false, arguments);
	}

	@Bean
	public Binding bindingFortressSyncQueue() {
		return BindingBuilder.bind(declareFortressSyncQueue()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 声明资源订购队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareCreationQueue() {
		return new Queue("iaas.resource.creation.queue", true, false, false, arguments);
	}

	/**
	 * 声明资源变更队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareModificationQueue() {
		return new Queue("iaas.resource.modification.queue", true, false, false, arguments);
	}

	/**
	 * 声明资源退订队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareDeletionQueue() {
		return new Queue("iaas.resource.deletion.queue", true, false, false, arguments);
	}

	/**
	 * 声明资源延期队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareExtensionQueue() {
		return new Queue("iaas.resource.extension.queue", true, false, false, arguments);
	}

	/**
	 * 绑定资源订购队列
	 *
	 * @return
	 */
	@Bean
	public Binding bindingCreationQueue() {
		return BindingBuilder.bind(declareCreationQueue()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 绑定资源变更队列
	 *
	 * @return
	 */
	@Bean
	public Binding bindingModificationQueue() {
		return BindingBuilder.bind(declareModificationQueue()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 绑定资源退订队列
	 *
	 * @return
	 */
	@Bean
	public Binding bindingDeletionQueue() {
		return BindingBuilder.bind(declareDeletionQueue()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 绑定资源延期队列
	 *
	 * @return
	 */
	@Bean
	public Binding bindingExtensionQueue() {
		return BindingBuilder.bind(declareExtensionQueue()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 绑定云平台统计队列
	 *
	 * @return
	 */
	@Bean
	public Binding bindingStatisticsQueue() {
		return BindingBuilder.bind(declareStatisticsQueue()).to(declareDirectExchange()).withQueueName();
	}

	/**
	 * 声明资源编排节点请求队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareRosQueue() {
		return new Queue("bocloud.ros.queue", true, false, false, arguments);
	}

	/**
	 * 声明云平台统计队列
	 *
	 * @return
	 */
	@Bean
	public Queue declareStatisticsQueue() {
		return new Queue("vendor.statistics.queue", true, false, false, arguments);
	}

	@Bean
	public MessageConverter messageConverter() {
		return new Jackson2JsonMessageConverter();
	}

	@Bean
	@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
	public RabbitTemplate amqpTemplate() {
		RabbitTemplate amqpTemplate = new RabbitTemplate(connectionFactory);
		amqpTemplate.setMessageConverter(messageConverter());
		amqpTemplate.setExchange(declareDirectExchange().getName());
		return amqpTemplate;
	}

	@Bean
	@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
	public RabbitAdmin rabbitAdmin() {
		RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
		return rabbitAdmin;
	}

	@Bean
	@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
	public AmqpAdmin amqpAdmin() {
		AmqpAdmin amqpAdmin = new RabbitAdmin(connectionFactory);
		return amqpAdmin;
	}
}
