package com.rjconsultores.ventaboletos.rest;

//import java.lang.reflect.Method;
//import java.text.SimpleDateFormat;
//import java.util.Arrays;
//import java.util.Calendar;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
//import javax.annotation.security.PermitAll;
//import javax.annotation.security.RolesAllowed;
//import javax.ws.rs.WebApplicationException;
//import javax.ws.rs.ext.Provider;
//
//import org.apache.commons.codec.binary.Base64;
//import org.jboss.resteasy.annotations.interception.ServerInterceptor;
//import org.jboss.resteasy.core.Headers;
//import org.jboss.resteasy.core.ResourceMethod;
//import org.jboss.resteasy.core.ServerResponse;
//import org.jboss.resteasy.spi.Failure;
//import org.jboss.resteasy.spi.HttpRequest;
//import org.jboss.resteasy.spi.interception.PreProcessInterceptor;
//import org.mindrot.jbcrypt.BCrypt;

//@ServerInterceptor
//@Provider
// FIXME: Ha uma diferenca das versoes 2.x para 3.x do resteasy onde a interface PreProcessInterceptor foi removida. Com isso não e possivel utilizar esta abordagem para o JBoss 6, EAP 7 e Wildfly 16
// OBS: Esta implementacao não estava ativa. Quando a mesma for implementada, a seguranca devera ser feita para casa requisicao REST
public class SecurityInterceptor /*implements PreProcessInterceptor*/ {
	
	/*
    private static final String AUTHORIZATION_PROPERTY = "AuthorizationSecurity";
    private static final String AUTHENTICATION_SCHEME = "Basic";
    private static final String ROLE_CHECK = "EMBARCADA_DISABLE";

    private static final ServerResponse ACCESS_DENIED = new ServerResponse("Access denied for this resource", 401, new Headers<Object>());
    private static final ServerResponse SERVER_ERROR = new ServerResponse("Internal Server Error", 500, new Headers<Object>());
    private static final ServerResponse ACCESS_FORBIDDEN = new ServerResponse("Access forbidden for this resource", 403, new Headers<Object>());
    
    private static final SimpleDateFormat sdf = new SimpleDateFormat("ddyyyyMM");
    
	@Override
	public ServerResponse preProcess(HttpRequest request, ResourceMethod methodInvoked) throws Failure, WebApplicationException {
		Method method = methodInvoked.getMethod();

		if (method.isAnnotationPresent(PermitAll.class)) {
            return null;
        }
		
		if (method.isAnnotationPresent(RolesAllowed.class)) {
			RolesAllowed rolesAnnotation = method.getAnnotation(RolesAllowed.class);
			Set<String> rolesSet = new HashSet<String>(Arrays.asList(rolesAnnotation.value()));
			 
			if (!rolesSet.contains(ROLE_CHECK)) {
				return null;
			}
		} else {
			return null;
		}
		
		final List<String> authorization = request.getHttpHeaders().getRequestHeader(AUTHORIZATION_PROPERTY);
        
        if(authorization == null || authorization.isEmpty()) {
            return ACCESS_DENIED;
        }

        final String encodedUserPassword = authorization.get(0).replaceFirst(AUTHENTICATION_SCHEME + " ", "");
        
        String infoUserPassword = null;
        
        try {
        	infoUserPassword = new String(Base64.decodeBase64(encodedUserPassword.getBytes()));
        } catch (Exception e) {
            return SERVER_ERROR;
        }        
        
        if (!isAllow(infoUserPassword)) {
        	return ACCESS_FORBIDDEN;
        }
        
		return null;
	}
	
	public boolean isAllow(final String infoUserPassword) {
        Calendar cal = Calendar.getInstance();

        String pwd1 = String.format("RJ#EMB@%s#Consultores", sdf.format(cal.getTime()));
        
        cal.add(Calendar.DAY_OF_MONTH, -1);

        String pwd2 = String.format("RJ#EMB@%s#Consultores", sdf.format(cal.getTime()));
        
        final String[] basic = infoUserPassword.split(":");
        
        final String password = basic[1];
		
        if (BCrypt.checkpw(pwd1, password) || BCrypt.checkpw(pwd2, password)) {
        	return true;
        }
        
        return false;
	}*/
}
