package com.sun.identity.cot;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class CircleOfTrustDescriptor {
   private String circleOfTrustType;
   private Map<String, Set<String>> trustedProviderMap;

   CircleOfTrustDescriptor(Map<String, Set<String>> attrMap) throws COTException {
      this.setCircleOfTrustType(COTUtils.getFirstEntry(attrMap, "sun-fm-cot-type"));
      this.setTrustedProviders(attrMap.get("sun-fm-trusted-providers"));
   }

   public CircleOfTrustDescriptor() {
   }

   private void setCircleOfTrustType(String type) throws COTException {
      if (type != null && !COTUtils.isValidProtocolType(type)) {
         String[] data = new String[]{type};
         throw new COTException("invalidProtooclType", data);
      }

      COTUtils.DEBUG.message("Circle of Trust type is : " + type);
      this.circleOfTrustType = type;
   }

   private void setTrustedProviders(Set<String> circleOfTrustProvider) {
      this.trustedProviderMap = Collections.unmodifiableMap(COTUtils.trustedProviderSetToProtocolMap(circleOfTrustProvider));
   }

   public Set<String> getTrustedProviders(String protocol) {
      if (this.trustedProviderMap != null && protocol != null) {
         Set<String> tmp = this.trustedProviderMap.get(protocol);
         if (tmp == null) {
            return Collections.emptySet();
         } else {
            return this.circleOfTrustType != null && !this.circleOfTrustType.equalsIgnoreCase(protocol) ? Collections.emptySet() : new HashSet<>(tmp);
         }
      } else {
         return Collections.emptySet();
      }
   }
}
