
package com.jeelowcode.framework.utils.component.crypto;

import com.jeelowcode.framework.utils.annotation.ApiDecryptAes;
import com.jeelowcode.framework.utils.tool.AesUtil;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.NonNull;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

/**
 * 参数加密
 */
@ControllerAdvice
public class ApiDecryptRequestBodyAdvice  implements RequestBodyAdvice {
    @Override
    public boolean supports(MethodParameter methodParameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
        return methodParameter.hasMethodAnnotation(ApiDecryptAes.class);
    }

    @Override
    public Object handleEmptyBody(Object body, @NonNull HttpInputMessage inputMessage, @NonNull MethodParameter parameter,
                                  @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    @NonNull
    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, @NonNull MethodParameter parameter,
                                           @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) throws IOException {

        ApiDecryptAes apiDecryptAes = parameter.getMethodAnnotation(ApiDecryptAes.class);
        if (apiDecryptAes == null) {
            return inputMessage;
        }
        //key
        String aesKey = AesUtil.secretKey;

        // 判断 body 是否为空
        InputStream messageBody = inputMessage.getBody();
        if (messageBody.available() <= 0) {
            return inputMessage;
        }
        //加密字符
        String bodyStr = StreamUtils.copyToString(messageBody, Charset.defaultCharset());
        bodyStr=bodyStr.replaceAll("\"","");

        //解密字符
        byte[] decrypt = AesUtil.decryptFormBase64(bodyStr, aesKey);

        InputStream inputStream = new ByteArrayInputStream(decrypt);

        return new HttpInputMessage() {
            @Override
            public InputStream getBody() throws IOException {
                return inputStream;  // 再将输入流返回
            }
            @Override
            public HttpHeaders getHeaders() {
                return inputMessage.getHeaders();
            }
        };

    }




    @NonNull
    @Override
    public Object afterBodyRead(@NonNull Object body, @NonNull HttpInputMessage inputMessage, @NonNull MethodParameter parameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

}
