package ink.wgink.module.wechat.officialaccount.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.module.wechat.officialaccount.service.IOfficialAccountCheckService;
import ink.wgink.properties.wechat.official.account.OfficialAccountProperties;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: OfficialAccountBindServiceImpl
 * @Description: 公众号绑定
 * @Author: wanggeng
 * @Date: 2021/4/28 3:20 下午
 * @Version: 1.0
 */
@Service
public class OfficialAccountCheckServiceImpl extends DefaultBaseService implements IOfficialAccountCheckService {

    @Autowired
    private OfficialAccountProperties officialAccountProperties;

    @Override
    public void checkSignature(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String signatureParameter = request.getParameter("signature");
        String timestampParameter = request.getParameter("timestamp");
        String nonceParameter = request.getParameter("nonce");
        String echostrParameter = request.getParameter("echostr");
        boolean isWechatConfig = !org.springframework.util.StringUtils.isEmpty(signatureParameter)
                && !org.springframework.util.StringUtils.isEmpty(timestampParameter)
                && !org.springframework.util.StringUtils.isEmpty(nonceParameter)
                && !StringUtils.isEmpty(echostrParameter);
        if (!isWechatConfig) {
            throw new ParamsException("参数缺失");
        }
        if (!isWechatOfficialAccountConfigRequest(signatureParameter, timestampParameter, nonceParameter, echostrParameter)) {
            throw new ParamsException("非公众号账号配置请求");
        }
        PrintWriter writer = response.getWriter();
        writer.write(echostrParameter);
        writer.flush();
        writer.close();
    }

    /**
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    private boolean isWechatOfficialAccountConfigRequest(String signature, String timestamp, String nonce, String echostr) {
        StringBuilder newSignature = new StringBuilder();
        Stream.of(officialAccountProperties.getConfigToken(), timestamp, nonce).sorted()
                .collect(Collectors.toList())
                .forEach(str -> {
                    newSignature.append(str);
                });
        if (!StringUtils.equals(signature, DigestUtils.sha1Hex(newSignature.toString()))) {
            return false;
        }
        return true;
    }

}
