package com.bonus.common.houqin.mq;

import com.bonus.common.houqin.mq.rabbit.RabbitPropertiesExt;
import com.bonus.common.houqin.mq.rocket.RocketProperties;
import com.google.common.collect.Sets;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Set;

@ConfigurationProperties(
   prefix = "spring.mq"
)
public class MQProperties {
   public static final String PREFIX = "spring.mq";
   public static final String TRACE_KEY = "X-Trace-Id";
   private MQType type;
   private Set<Topic> topics;
   private Set<Topic> topicsMore;
   private String namespace;
   private boolean subscriptionMissingCheck;
   private RabbitPropertiesExt rabbitmq;
   private RocketProperties rocketmq;

   public void checkTopicDefinition(String topic) {
      if (!this.topics.stream().anyMatch((t) -> {
         return t.getName().equals(topic);
      }) && !this.topicsMore.stream().anyMatch((t) -> {
         return t.getName().equals(topic);
      })) {
         throw new IllegalArgumentException("Undefined topic: `" + topic + "`");
      }
   }

   public Set<Topic> getAllTopics() {
      Set<Topic> allTopics = Sets.newHashSet(this.topics);
      allTopics.addAll(this.topicsMore);
      return allTopics;
   }

   public MQProperties() {
      this.type = MQType.RABBITMQ;
      this.topics = Sets.newHashSet();
      this.topicsMore = Sets.newHashSet();
      this.namespace = "__default__";
      this.rabbitmq = new RabbitPropertiesExt();
      this.rocketmq = new RocketProperties();
   }

   public MQType getType() {
      return this.type;
   }

   public Set<Topic> getTopics() {
      return this.topics;
   }

   public Set<Topic> getTopicsMore() {
      return this.topicsMore;
   }

   public String getNamespace() {
      return this.namespace;
   }

   public boolean isSubscriptionMissingCheck() {
      return this.subscriptionMissingCheck;
   }

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

   public RocketProperties getRocketmq() {
      return this.rocketmq;
   }

   public void setType(final MQType type) {
      this.type = type;
   }

   public void setTopics(final Set<Topic> topics) {
      this.topics = topics;
   }

   public void setTopicsMore(final Set<Topic> topicsMore) {
      this.topicsMore = topicsMore;
   }

   public void setNamespace(final String namespace) {
      this.namespace = namespace;
   }

   public void setSubscriptionMissingCheck(final boolean subscriptionMissingCheck) {
      this.subscriptionMissingCheck = subscriptionMissingCheck;
   }

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

   public void setRocketmq(final RocketProperties rocketmq) {
      this.rocketmq = rocketmq;
   }

   public static enum MQType {
      RABBITMQ,
      ROCKETMQ,
      NONE;

      // $FF: synthetic method
      private static MQType[] $values() {
         return new MQType[]{RABBITMQ, ROCKETMQ, NONE};
      }
   }

   public static class Topic {
      private String name;
      private int queueNum = 4;

      public String getName() {
         return this.name;
      }

      public int getQueueNum() {
         return this.queueNum;
      }

      public void setName(final String name) {
         this.name = name;
      }

      public void setQueueNum(final int queueNum) {
         this.queueNum = queueNum;
      }

   }
}
