package com.fs.his.utils;

import com.alibaba.fastjson.JSONObject;
import com.fs.hisapi.domain.ApiResponse;
import com.fs.hisapi.util.MapUtil;
import com.fs.hisapi.util.SignUtil;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
public class HttpUtil {
    public static String endApi(String url , Map<String, String> param,Map<String, String> map){

        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
        // 设置请求参数

        if (param!=null){
            List<NameValuePair> params = new ArrayList<>();
            for (Map.Entry<String, String> entry : param.entrySet()) {
                String value = entry.getValue();
                params.add(new BasicNameValuePair(entry.getKey(), value));
            }
            // 将参数编码为URL格式
            String paramString = URLEncodedUtils.format(params, StandardCharsets.UTF_8);
            try {
                httpPost.setEntity(new StringEntity(paramString,"UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Gson gson = new Gson();
        String json = gson.toJson(map);
        StringEntity requestEntity = new StringEntity(json, "UTF-8");
        httpPost.setEntity(requestEntity);
        return  sendHttp(httpPost);
    }
    public static String sendHttp( HttpPost httpPost){
        String responseText=null;
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 发送请求，获取响应
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            // 获取响应实体
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity, "UTF-8");
            }
            // 关闭响应
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭HttpClient
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseText;
    }

    public static byte[] getWechatQrcodeByHttpURL(String url, Map<String, String> body) {
        HttpURLConnection httpURLConnection = null;
        try {
            httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
            httpURLConnection.setRequestMethod("POST");
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            printWriter.write(JSONObject.toJSONString(body));
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            try (InputStream inputStream = httpURLConnection.getInputStream();
                 ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                byte[] buffer = new byte[1024];
                int len = -1;
                while ((len = inputStream.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                return out.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }
        return null;
    }



}
