package edu.conncoll.cas.interrupts.web.flow;

import java.util.Map;
import org.apache.commons.lang3.tuple.Pair;
import org.apereo.cas.authentication.Credential;
import org.apereo.cas.authentication.principal.Principal;
import org.apereo.cas.ticket.registry.TicketRegistrySupport;
import org.apereo.cas.web.support.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.webflow.execution.RequestContext;
import edu.conncoll.cas.jdbc.JDBCCamel;

/*
 * Figure out which of these needs to be included for init
import org.apereo.cas.authentication.UsernamePasswordCredential;
import org.pac4j.core.credentials.UsernamePasswordCredentials;
import org.apereo.cas.ticket.registry.TicketRegistry;
import org.apereo.cas.ticket.TicketGrantingTicket;
 */

public abstract class AbstractConncollInterruptsRepository implements ConncollInterruptsRepository {
    private static final long serialVersionUID = 1883808902502739L;

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractConncollInterruptsRepository.class);

    /**
     * Single-valued attribute in LDAP that describes whether the policy
     * has been accepted. Its value must match either TRUE/FALSE.
     */
    protected String intAttributeName;
    
    protected JDBCCamel jdbcCamel;

    /**
     * Ticket registry support.
     */
    protected TicketRegistrySupport ticketRegistrySupport;

    public AbstractConncollInterruptsRepository(final TicketRegistrySupport ticketRegistrySupport) {
        this.ticketRegistrySupport = ticketRegistrySupport;
    }

    @Override
    public Pair<String, Principal> verify(final RequestContext requestContext, final Credential credential) {
        final Principal principal = WebUtils.getPrincipalFromRequestContext(requestContext, this.ticketRegistrySupport);
        final Map<String, Object> attributes = principal.getAttributes();
        LOGGER.debug("Principal attributes found for [{}] are [{}]", principal.getId(), attributes);
        
        if (attributes != null && attributes.containsKey(this.intAttributeName)) {
            final Object value = attributes.get(this.intAttributeName);
            LOGGER.debug("Evaluating attribute value [{}] found for [{}]", value, this.intAttributeName);
            if (value.toString().equalsIgnoreCase("true")) {
                return Pair.of("true", principal);
            }
        }

        return Pair.of("false", principal);
    }
    
    @Override
    public String checkInt(RequestContext requestContext, Credential credential, Boolean loggedIn){
     	return "noInterrupt";
    }
    
    @Override
    public String setFlag(RequestContext requestContext, Credential credential){
    	String interrupt = requestContext.getFlowScope().get("interrupt",String.class);
    	LOGGER.debug("setFlag updating Flag to " + interrupt);
    	requestContext.getFlowScope().put("Flag", interrupt);
    	return "success";
    }
    
    public void setIntAttributeName(final String intAttributeName) {
        this.intAttributeName = intAttributeName;
        LOGGER.debug("Interrupt attribute set to [{}]",intAttributeName);
    }

    public void setTicketRegistrySupport(final TicketRegistrySupport ticketRegistrySupport) {
        this.ticketRegistrySupport = ticketRegistrySupport;
    }
    
    public void setJDBCCamel (final JDBCCamel jdbcCamel){
    	this.jdbcCamel = jdbcCamel;
    }
    
    public JDBCCamel getJDBCCamel (){
    	return this.jdbcCamel;
    }
}
