/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.cloud.stream.binder.rocketmq.properties;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.stream.binder.ExtendedBindingProperties;

/**
 * @author Timur Valiev
 * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
 */
@ConfigurationProperties("spring.cloud.stream.rocketmq")
public class RocketMQExtendedBindingProperties implements
		ExtendedBindingProperties<RocketMQConsumerProperties, RocketMQProducerProperties> {

	private Map<String, RocketMQBindingProperties> bindings = new HashMap<>();

	public Map<String, RocketMQBindingProperties> getBindings() {
		return this.bindings;
	}

	public void setBindings(Map<String, RocketMQBindingProperties> bindings) {
		this.bindings = bindings;
	}

	@Override
	public synchronized RocketMQConsumerProperties getExtendedConsumerProperties(
			String channelName) {
		if (bindings.containsKey(channelName)) {
			if (bindings.get(channelName).getConsumer() != null) {
				return bindings.get(channelName).getConsumer();
			}
			else {
				RocketMQConsumerProperties properties = new RocketMQConsumerProperties();
				this.bindings.get(channelName).setConsumer(properties);
				return properties;
			}
		}
		else {
			RocketMQConsumerProperties properties = new RocketMQConsumerProperties();
			RocketMQBindingProperties rbp = new RocketMQBindingProperties();
			rbp.setConsumer(properties);
			bindings.put(channelName, rbp);
			return properties;
		}
	}

	@Override
	public synchronized RocketMQProducerProperties getExtendedProducerProperties(
			String channelName) {
		if (bindings.containsKey(channelName)) {
			if (bindings.get(channelName).getProducer() != null) {
				return bindings.get(channelName).getProducer();
			}
			else {
				RocketMQProducerProperties properties = new RocketMQProducerProperties();
				this.bindings.get(channelName).setProducer(properties);
				return properties;
			}
		}
		else {
			RocketMQProducerProperties properties = new RocketMQProducerProperties();
			RocketMQBindingProperties rbp = new RocketMQBindingProperties();
			rbp.setProducer(properties);
			bindings.put(channelName, rbp);
			return properties;
		}
	}
}
