package com.sun.identity.cot;

import com.sun.identity.shared.debug.Debug;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

public class COTUtils {
   public static final String RESOURCE_BUNDLE_NAME = "libCOT";
   public static final Debug DEBUG = Debug.getInstance("libCOT");
   public static final String SAML2_DELIM = "|saml2";

   public static String getFirstEntry(Map<String, Set<String>> attrMap, String key) {
      String retValue = null;
      if (attrMap != null && !attrMap.isEmpty()) {
         Set<String> valueSet = attrMap.get(key);
         if (valueSet != null && !valueSet.isEmpty()) {
            retValue = valueSet.iterator().next();
         }
      }

      return retValue;
   }

   public static boolean isValidProtocolType(String protocolType) {
      boolean isValid = "saml2".equalsIgnoreCase(protocolType);
      if (!isValid) {
         String[] data = new String[]{protocolType};
         LogUtil.error(Level.INFO, "INVALID_COT_TYPE", data);
      }

      return isValid;
   }

   static Map<String, Set<String>> trustedProviderSetToProtocolMap(Set<String> providerSet) {
      if (providerSet != null && !providerSet.isEmpty()) {
         Map<String, Set<String>> map = new HashMap<>();
         Set<String> saml2Set = new HashSet<>();

         for (String val : providerSet) {
            if (DEBUG.messageEnabled()) {
               DEBUG.message("COTUtils.setToProtocolMap: check " + val);
            }

            if (val.endsWith("|saml2")) {
               saml2Set.add(val.substring(0, val.length() - "|saml2".length()));
            }
         }

         map.put("saml2", saml2Set);
         if (DEBUG.messageEnabled()) {
            DEBUG.message("COTUtils.setToProtocolMap: return " + map);
         }

         return map;
      } else {
         return Collections.emptyMap();
      }
   }
}
