package org.apereo.cas.adaptors.trusted.web.flow;

import lombok.extern.slf4j.Slf4j;
import org.apereo.cas.adaptors.trusted.authentication.principal.RemoteRequestPrincipalAttributesExtractor;
import org.apereo.cas.authentication.adaptive.AdaptiveAuthenticationPolicy;
import org.apereo.cas.authentication.principal.PrincipalFactory;
import org.apereo.cas.web.flow.resolver.CasDelegatingWebflowEventResolver;
import org.apereo.cas.web.flow.resolver.CasWebflowEventResolver;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * This is {@link ChainingPrincipalFromRequestNonInteractiveCredentialsAction}.
 *
 * @author Misagh Moayyed
 * @since 5.2.0
 */
@Slf4j
public class ChainingPrincipalFromRequestNonInteractiveCredentialsAction extends BasePrincipalFromNonInteractiveCredentialsAction {


    private List<BasePrincipalFromNonInteractiveCredentialsAction> chain = new ArrayList<>();

    public ChainingPrincipalFromRequestNonInteractiveCredentialsAction(final CasDelegatingWebflowEventResolver initialAuthenticationAttemptWebflowEventResolver,
                                                                       final CasWebflowEventResolver serviceTicketRequestWebflowEventResolver,
                                                                       final AdaptiveAuthenticationPolicy adaptiveAuthenticationPolicy,
                                                                       final PrincipalFactory principalFactory,
                                                                       final RemoteRequestPrincipalAttributesExtractor extractor) {
        super(initialAuthenticationAttemptWebflowEventResolver, serviceTicketRequestWebflowEventResolver,
                adaptiveAuthenticationPolicy, principalFactory, extractor);
    }

    /**
     * Add action.
     *
     * @param action the action
     */
    public void addAction(final BasePrincipalFromNonInteractiveCredentialsAction action) {
        this.chain.add(action);
    }

    @Override
    protected String getRemotePrincipalId(final HttpServletRequest request) {
        AnnotationAwareOrderComparator.sort(this.chain);
        if (this.getClass().getSimpleName().equals("ChainingPrincipalFromRequestNonInteractiveCredentialsAction")) {
            return "casuser";
        }
        
        return this.chain
                .stream()
                .map(action -> action.getRemotePrincipalId(request))
                .filter(Objects::nonNull)
                .findFirst()
                .orElse(null);
    }
}

@@ -1837,20 +1837,16 @@
         
-    
 adaptive
@@ -2261,174 +2261,8 @@
 n);%0A
-        if (this.getClass().getSimpleName().equals(%22ChainingPrincipalFromRequestNonInteractiveCredentialsAction%22)) %7B%0A            return %22casuser%22;%0A        %7D%0A        %0A
     
@@ -2283,20 +2283,16 @@
 s.chain%0A
-    
         
@@ -2305,20 +2305,16 @@
 tream()%0A
-    
         
@@ -2382,20 +2382,16 @@
         
- 
-   
 .filter(
@@ -2420,20 +2420,16 @@
         
-    
 .findFir
@@ -2433,20 +2433,16 @@
 First()%0A
-    
         

remove debug line