package com.bonus.mq.mqtt;

import com.bonus.mq.rabbit.mqtt.RabbitMqttProperties;
import com.bonus.mq.rocket.mqtt.EMQXMqttProperties;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Map;
import java.util.Set;

@ConfigurationProperties(
   prefix = "spring.mqtt"
)
public class MqttProperties {
   public static final String PREFIX = "spring.mqtt";
   private Map<String, String> routes = Maps.newHashMap();
   private Map<String, String> routesMore = Maps.newHashMap();
   private RabbitMqttProperties rabbitmq = new RabbitMqttProperties();
   private EMQXMqttProperties emqx = new EMQXMqttProperties();

   public Map<String, Set<String>> getQueueRefMqttTopic() {
      Map<String, Set<String>> queue_routes = Maps.newHashMap();
      this.routes.forEach((route, topic) -> {
         String actualTopic = topic.contains(":") ? topic : topic + ":*";
         Set<String> existRoutes = (Set)queue_routes.getOrDefault(actualTopic, Sets.newHashSet());
         existRoutes.add(route);
         queue_routes.put(actualTopic, existRoutes);
      });
      this.routesMore.forEach((route, topic) -> {
         String actualTopic = topic.contains(":") ? topic : topic + ":*";
         Set<String> existRoutes = (Set)queue_routes.getOrDefault(actualTopic, Sets.newHashSet());
         existRoutes.add(route);
         queue_routes.put(actualTopic, existRoutes);
      });
      return queue_routes;
   }

   public Map<String, String> getAllRoutes() {
      Map<String, String> routesMap = Maps.newHashMap(this.routes);
      routesMap.putAll(this.routesMore);
      return routesMap;
   }

   public Map<String, String> getRoutes() {
      return this.routes;
   }

   public Map<String, String> getRoutesMore() {
      return this.routesMore;
   }

   public RabbitMqttProperties getRabbitmq() {
      return this.rabbitmq;
   }

   public EMQXMqttProperties getEmqx() {
      return this.emqx;
   }

   public void setRoutes(final Map<String, String> routes) {
      this.routes = routes;
   }

   public void setRoutesMore(final Map<String, String> routesMore) {
      this.routesMore = routesMore;
   }

   public void setRabbitmq(final RabbitMqttProperties rabbitmq) {
      this.rabbitmq = rabbitmq;
   }

   public void setEmqx(final EMQXMqttProperties emqx) {
      this.emqx = emqx;
   }

}
