package com.ym.mec.auth.core.filter;

import com.ym.mec.auth.api.entity.LoginEntity;
import com.ym.mec.auth.config.token.PhoneAuthenticationToken;
import com.ym.mec.common.security.SecurityConstants;
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 javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
	private boolean postOnly = true;
	public PhoneLoginAuthenticationFilter() {
		super(new AntPathRequestMatcher("/smsLogin", "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());
		}
		//拼装请求参数
		LoginEntity loginEntity = getLoginEntity(request);
		AbstractAuthenticationToken authRequest = new PhoneAuthenticationToken(SecurityConstants.PHONE_PRINCIPAL_PREFIX + loginEntity.getPhone(), loginEntity);
		// Allow subclasses to set the "details" property
		authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
		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 String obtainParameter(HttpServletRequest request, String parameter) {
		String result = request.getParameter(parameter);
		return result == null ? "" : result;
	}

	private LoginEntity getLoginEntity(HttpServletRequest request){
		// 手机验证码登陆
		String principal = obtainParameter(request, "phone");
		String credentials = obtainParameter(request, "smsCode");
		// 是否是租户
		String isLessee = obtainParameter(request, "isLessee");
		String tenantId = obtainParameter(request, "tenantId");
		String organId = obtainParameter(request, "organId");
		boolean isRegister = StringUtils.equals("1", isLessee) || StringUtils.equals("true", isLessee);

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

		principal = principal.trim();
		LoginEntity loginEntity = new LoginEntity();
		loginEntity.setClientId(clientId);
		loginEntity.setPhone(principal);
		loginEntity.setSmsCode(credentials);
		loginEntity.setIsRegister(isRegister);
		loginEntity.setDeviceNum(deviceNum);
		loginEntity.setOrganId(organId);
		if(StringUtils.isNotEmpty(tenantId)){
			loginEntity.setTenantId(Integer.parseInt(tenantId));
		}
		return loginEntity;
	}

}
