package com.yonge.cooleshow.auth.core.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import com.yonge.cooleshow.auth.api.entity.LoginEntity;
import com.yonge.cooleshow.auth.config.token.PhoneAuthenticationToken;
import com.yonge.cooleshow.common.security.SecurityConstants;

public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

	private static final String SPRING_SECURITY_RESTFUL_PHONE_KEY = "phone";
	private static final String SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY = "smsCode";
	private static final String clientIdParameter = "clientId";
	private static final String IS_SURPORT_REGISTER = "isSurportRegister";

	private static final String DEVICE_NUM = "deviceNum";

	private static final String SPRING_SECURITY_RESTFUL_LOGIN_URL = "/smsLogin";
	private boolean postOnly = true;

	public PhoneLoginAuthenticationFilter() {
		super(new AntPathRequestMatcher(SPRING_SECURITY_RESTFUL_LOGIN_URL, "POST"));
	}

	@Override
	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
		if (postOnly && !request.getMethod().equals("POST")) {
			throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
		}

		AbstractAuthenticationToken authRequest;
		// 手机验证码登陆
		String principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_PHONE_KEY);
		String credentials = obtainParameter(request, SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY);
		
		boolean isRegister = StringUtils.equals("true", request.getParameter(IS_SURPORT_REGISTER));

		String clientId = request.getParameter(clientIdParameter).toUpperCase();

		if ("EDUCATION".equals(clientId)) {
			clientId = "SYSTEM";
		}
		
		String deviceNum = request.getParameter(DEVICE_NUM);

		principal = principal.trim();

		LoginEntity loginEntity = new LoginEntity();
		loginEntity.setClientId(clientId);
		loginEntity.setPhone(principal);
		loginEntity.setSmsCode(credentials);
		loginEntity.setIsSurportRegister(isRegister);
		loginEntity.setDeviceNum(deviceNum);

		authRequest = new PhoneAuthenticationToken(SecurityConstants.PHONE_PRINCIPAL_PREFIX + principal, loginEntity);

		// Allow subclasses to set the "details" property
		setDetails(request, authRequest);
		return this.getAuthenticationManager().authenticate(authRequest);
	}

	@Override
	protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult)
			throws IOException, ServletException {
		super.successfulAuthentication(request, response, chain, authResult);
		// chain.doFilter(request, response);
	}

	private void setDetails(HttpServletRequest request, AbstractAuthenticationToken authRequest) {
		authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
	}

	private String obtainParameter(HttpServletRequest request, String parameter) {
		String result = request.getParameter(parameter);
		return result == null ? "" : result;
	}

}
