package com.cpop.core.gateway.miniProgram;

import com.cpop.common.utils.StringUtils;
import com.cpop.core.base.enums.UserType;
import com.fasterxml.jackson.databind.ObjectMapper;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author DB
 * @Description: 小程序认证过滤器
 * @create 2023-08-23 21:03
 */
public class MiniProgramAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    public MiniProgramAuthenticationFilter() {
        super(new AntPathRequestMatcher("/miniLogin", "POST"));
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        if (!"POST".equals(request.getMethod())) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }
        ObjectMapper mapper = new ObjectMapper();
        InputStream inputStream;
        Map authenticationBean;
        try {
            inputStream = request.getInputStream();
            authenticationBean = mapper.readValue(inputStream, Map.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String principal = (String) authenticationBean.get("phone");
        if (StringUtils.isBlank(principal)){
            throw new AuthenticationServiceException("登录手机号为空");
        }
        String openId = (String) authenticationBean.get("openId");
        if (StringUtils.isBlank(openId)){
            throw new AuthenticationServiceException("登录openId为空");
        }
        String appId = (String) authenticationBean.get("appId");
        if (StringUtils.isBlank(appId)){
            throw new AuthenticationServiceException("登录appId为空");
        }
        String brandId = (String) authenticationBean.get("brandId");
        if (StringUtils.isBlank(brandId)){
            throw new AuthenticationServiceException("登录brandId为空");
        }
        String nickName = (String) authenticationBean.get("nickName");
        String avatar = (String) authenticationBean.get("avatar");
        String sourceType = (String) authenticationBean.get("sourceType");
        if (StringUtils.isBlank(sourceType)){
            throw new AuthenticationServiceException("登录sourceType为空");
        }
        UserType userType = UserType.valueOf(authenticationBean.get("userType").toString());//传递信息
        HashMap<String, Object> credentials = new HashMap<>(2);
        credentials.put("appId", appId);
        credentials.put("openId", openId);
        credentials.put("userType", userType);
        credentials.put("brandId", brandId);
        credentials.put("nickName", nickName);
        credentials.put("avatar", avatar);
        credentials.put("sourceType", sourceType);
        principal = principal.trim();
        MiniProgramAuthenticationToken authRequest = new MiniProgramAuthenticationToken(principal, credentials);
        this.setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }

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

}
