package com.sptg.common.core.util;

import com.sptg.common.core.constant.CommonConstant;
import com.sptg.common.core.constant.HttpConstant;
import com.sptg.common.core.constant.SymbolConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * title: WebUtils
 * package: com.sptg.common.core.util
 * description: Web工具类
 *                  
 *                
 */
@Slf4j
public class WebUtils {

//    /**
//     * 获取 ServletRequestAttributes
//     * @return ServletRequestAttributes
//     */
//    public static ServletRequestAttributes getRequestAttributes(){
//        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
//        return null == attributes ? null : (ServletRequestAttributes) attributes;
//    }

    /**
     * 获取 HttpServletRequest
     * @return HttpServletRequest
     */
    public static HttpServletRequest getHttpServletRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取 HttpServletResponse
     * @return HttpServletResponse
     */
    public static HttpServletResponse getHttpServletResponse(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    /**
     * 获取Ip
     * @return Ip
     */
    public static String getIp(){
        HttpServletRequest request = WebUtils.getHttpServletRequest();
        //  获取远程地址
        String ip = request.getRemoteAddr();
        for (String ipHeader : HttpConstant.IP_HEADER_ARRAY) {
            String value = request.getHeader(ipHeader);
            if (!StringUtils.isEmpty(value) && !value.equalsIgnoreCase(HttpConstant.UNKNOWN)){
                ip = value;
                break;
            }
        }
        return StringUtils.isEmpty(ip) ? null : ip.split(SymbolConstant.COMMA)[0];
    }

    /**
     * 功能描述: 获取请求体数据
     * @param request 请求信息
     * @return String
     *                  
     *                     
     */
    public static String getRequestBody(HttpServletRequest request)throws IOException{
        byte[] buffer = getRequestBodys(request);
        String encoding = request.getCharacterEncoding();
        if (null == encoding){
            encoding = CommonConstant.UTF_8;
        }
        return new String(buffer, encoding);
    }

    /**
     * 功能描述: 获取请求体二进制数据
     * @param request 请求信息
     * @return byte[]
     *                  
     *                     
     */
    private static byte[] getRequestBodys(HttpServletRequest request)throws IOException {
        int contentLength = request.getContentLength();
        byte[] buffer = new byte[contentLength];
        ServletInputStream inputStream = request.getInputStream();
        for (int i = 0; i < contentLength;){
            int readLen = inputStream.read(buffer, i, contentLength - i);
            if (readLen == -1){
                break;
            }
            i += readLen;
        }
        return buffer;
    }

    /**
     * 功能描述: 写入数据到输出流
     * @param response 相应体
     * @param data 数据
     * @param contentType 数据类型
     *                  
     *                     
     */
    public static void writeResponse(HttpServletResponse response, String data, String contentType){
        PrintWriter printWriter = null;
        response.setContentLength(-1);
        response.setCharacterEncoding(CommonConstant.UTF_8);
        response.setContentType(contentType);
        try{
            printWriter = response.getWriter();
            printWriter.write(data);
            printWriter.flush();
        }catch (IOException e){
            log.error(e.getMessage(),e);
        }
        if (null != printWriter){
            printWriter.close();
        }
    }


}
