package com.game.business.util.other;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.util.Args;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

public class PayHttpUtil {

    private static final Logger _log = LoggerFactory.getLogger(PayHttpUtil.class);

    public static String getSign(Map<String, Object> map, String key) {
        StringBuffer sb = new StringBuffer();
        Set es = map.entrySet();
        Iterator it = es.iterator();
        String sign = "";

        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String v = (String)entry.getValue();
            if (StringUtils.isNotBlank(v)) {
                sb.append(v);
            }
        }

        sign = sb.toString();
        String signStr = sign + key;

        try {
            _log.info("待签名串：{}", new Object[]{signStr});
            sign = MD5Util.string2MD5(signStr);
            _log.info("签名结果:{}", new Object[]{sign});
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return sign;
    }

    /**
     * InputStream转换成String
     * 注意:流关闭需要自行处理
     * @param entity
     * @param encoding 编码
     * @return String
     * @throws Exception
     */
    public static String InputStreamTOString(HttpEntity entity, String encoding) throws IOException {
        Args.notNull(entity, "Entity");
        InputStream instream = entity.getContent();
        if (instream == null) {
            return null;
        }else{
            return new String(InputStreamTOByte(instream),encoding);
        }
    }

    /**
     * InputStream转换成Byte
     * 注意:流关闭需要自行处理
     * @param in
     * @return byte
     * @throws Exception
     */
    public static byte[] InputStreamTOByte(InputStream in) throws IOException {

        int BUFFER_SIZE = 4096;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[BUFFER_SIZE];
        int count = -1;

        while((count = in.read(data,0,BUFFER_SIZE)) != -1)
            outStream.write(data, 0, count);

        data = null;
        byte[] outByte = outStream.toByteArray();
        outStream.close();

        return outByte;
    }

    /** <一句话功能简述>
     * <功能详细描述>request转字符串
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String parseRequst(HttpServletRequest request){
        String body = "";
        try {
            ServletInputStream inputStream = request.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            while(true){
                String info = br.readLine();
                if(info == null){
                    break;
                }
                if(body == null || "".equals(body)){
                    body = info;
                }else{
                    body += info;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return body;
    }


    public static String getParamsFromFormDataByNames(HttpServletRequest request){
        Map<String, String> map =new HashMap<>();
        Enumeration<String> er = request.getParameterNames();
        while (er.hasMoreElements()) {
            String name = (String) er.nextElement();
            String value = request.getParameter(name);
            map.put(name, value);
        }
        return JSONObject.toJSONString(map);
    }

}



