package com.sun.identity.saml2.plugins;

import com.airlock.iam.opensso.jaxb.entityconfig.BaseConfigType;
import com.sun.identity.plugin.datastore.DataStoreProvider;
import com.sun.identity.saml2.common.SAML2SDKUtils;
import com.sun.identity.saml2.common.SAML2Utils;
import com.sun.identity.saml2.meta.SAML2MetaException;
import com.sun.identity.saml2.meta.SAML2MetaManager;
import com.sun.identity.saml2.meta.SAML2MetaUtils;
import java.util.List;
import java.util.Map;

public abstract class DefaultAccountMapper {
   protected static DataStoreProvider dsProvider;
   private static SAML2MetaManager metaManager;
   protected static final String IDP = "IDPRole";
   protected static final String SP = "SPRole";
   protected String role;

   public DefaultAccountMapper() {
      SAML2SDKUtils.DEBUG.message("DefaultAccountMapper.constructor: ");
   }

   protected String getAttribute(String realm, String entityID, String attributeName) {
      if (realm != null && entityID != null && attributeName != null) {
         try {
            BaseConfigType config;
            if (this.role.equals("IDPRole")) {
               config = metaManager.getIDPSSOConfig(realm, entityID);
            } else {
               config = metaManager.getSPSSOConfig(realm, entityID);
            }

            Map<String, List<String>> attributes = SAML2MetaUtils.getAttributes(config);
            if (attributes.isEmpty()) {
               if (SAML2SDKUtils.DEBUG.messageEnabled()) {
                  SAML2SDKUtils.DEBUG
                     .message(
                        "DefaultAccountMapper.getAttribute: attribute configuration is not defined for Entity "
                           + entityID
                           + " realm ="
                           + realm
                           + " role="
                           + this.role
                     );
               }

               return null;
            } else {
               List<String> list = attributes.get(attributeName);
               if (list != null && !list.isEmpty()) {
                  return list.getFirst();
               }

               if (SAML2SDKUtils.DEBUG.messageEnabled()) {
                  SAML2SDKUtils.DEBUG.message("DefaultSPAccountMapper.getAttribute: " + attributeName + " is not configured.");
               }

               return null;
            }
         } catch (SAML2MetaException sme) {
            if (SAML2SDKUtils.DEBUG.warningEnabled()) {
               SAML2SDKUtils.DEBUG.warning("DefaultSPAccountMapper.getAttribute: Meta Exception", sme);
            }

            return null;
         }
      } else {
         if (SAML2SDKUtils.DEBUG.messageEnabled()) {
            SAML2SDKUtils.DEBUG.message("DefaultAccountMapper.getAttribute: null input parameters.");
         }

         return null;
      }
   }

   static {
      try {
         dsProvider = SAML2Utils.getDataStoreProvider();
         metaManager = new SAML2MetaManager();
      } catch (Exception se) {
         SAML2SDKUtils.DEBUG.error("DefaultAccountMapper.static intialization failed", se);
      }
   }
}
