package com.qmth.paper.library.common.util;

import com.qmth.paper.library.common.enums.ExceptionResultEnum;
import com.qmth.paper.library.common.contant.SystemConstant;
import org.apache.commons.io.FileUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @Description: http util
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/12/11
 */
public class HttpUtil {
    private final static Logger log = LoggerFactory.getLogger(HttpUtil.class);

    /**
     * post json
     *
     * @param url
     * @param json
     * @param secret
     * @param timestamp
     * @return
     * @throws IOException
     */
    public static String postJson(String url, String json, String secret, Long timestamp) throws IOException {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(SystemConstant.CONNECT_TIME_OUT)// 连接主机服务超时时间
                .setConnectionRequestTimeout(SystemConstant.CONNECT_TIME_OUT)// 请求超时时间
                .setSocketTimeout(SystemConstant.SOCKET_CONNECT_TIME_OUT)// 数据读取超时时间
                .build();

        // 构建post请求
        HttpPost post = new HttpPost(url);
        post.setConfig(requestConfig);
        post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
        post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
        post.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
        post.setHeader("Accept", "application/json");

        String encoderJson = URLEncoder.encode(json, SystemConstant.CHARSET_NAME);
        StringEntity se = new StringEntity(encoderJson);
        se.setContentType("text/json");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);
        // 执行请求，获取响应
        return getRespString(post);
    }

    /**
     * post 请求
     *
     * @param url
     * @param params
     * @param secret
     * @param timestamp
     * @return
     */
    public static String post(String url, Map<String, Object> params, String secret, Long timestamp) throws IOException {
        // 构建post请求
        HttpPost post = new HttpPost(url);
        post.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
        post.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
        post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=utf-8");
        // 构建请求参数
        List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
        if (params != null) {
            for (String key : params.keySet()) {
                pairs.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
            }
        }
        HttpEntity entity = null;
        try {
            entity = new UrlEncodedFormEntity(pairs, SystemConstant.CHARSET_NAME);
        } catch (UnsupportedEncodingException e) {
            log.error(SystemConstant.LOG_ERROR, e);
        }
        post.setEntity(entity);
        // 执行请求，获取响应
        return getRespString(post);
    }

    /**
     * get 请求
     *
     * @param url
     * @param params
     * @param secret
     * @param timestamp
     * @return
     */
    public static String get(String url, Map<String, Object> params, String secret, Long timestamp) throws IOException {
        // 构建请求参数
        List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
        if (params != null) {
            for (String key : params.keySet()) {
                pairs.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
            }
        }
        String str = EntityUtils.toString(new UrlEncodedFormEntity(pairs, Consts.UTF_8));//转换为键值对
        HttpGet get = new HttpGet(url + "?" + str);
        if (Objects.nonNull(secret)) {
            get.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
        }
        if (Objects.nonNull(timestamp)) {
            get.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
        }
        // 执行请求，获取响应
        return getRespString(get);
    }

    /**
     * 获取响应信息
     *
     * @param request
     * @return
     */
    public static String getRespString(HttpUriRequest request) throws IOException {
        // 获取响应流
        InputStream in = null;
        ByteArrayOutputStream ou = null;
        try {
            in = getRespInputStream(request);
            ou = new ByteArrayOutputStream();
            IOUtils.copy(in, ou);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
        } finally {
            if (Objects.nonNull(in)) {
                in.close();
            }
            if (Objects.nonNull(ou)) {
                ou.close();
            }
        }
        return Objects.nonNull(ou) ? new String(ou.toByteArray(), StandardCharsets.UTF_8) : null;
    }

    /**
     * 获取输入流
     *
     * @param request
     * @return
     */
    public static InputStream getRespInputStream(HttpUriRequest request) {
        // 获取响应对象
        HttpResponse response = null;
        try {
            response = HttpClients.createDefault().execute(request);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
        }
        if (response == null) {
            return null;
        }
        // 获取Entity对象
        HttpEntity entity = response.getEntity();
        // 获取响应信息流
        InputStream in = null;
        if (entity != null) {
            try {
                in = entity.getContent();
            } catch (Exception e) {
                log.error(SystemConstant.LOG_ERROR, e);
            }
        }
        return in;
    }

    /**
     * 根据url下载文件，保存到filePath中
     *
     * @param url
     * @param filePath
     * @return
     */
    public static File httpDownload(String url, String filePath) throws IOException {
        InputStream is = null;
        File file = null;
        try {
            HttpClient client = HttpClients.createDefault();
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = client.execute(httpget);

            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            Optional.ofNullable(is).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("所在路径不存在"));
            file = new File(filePath);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            FileUtils.copyInputStreamToFile(is, file);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
        } finally {
            if (Objects.nonNull(is)) {
                is.close();
            }
        }
        return file;
    }
}