package cn.edu.ecnu.stu.bookstore.util;

import cn.edu.ecnu.stu.bookstore.component.Result;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class RequestUtil {

    public static Result post(String url, Map<String, Object> bodyMap, String token) {
        // post请求
        HttpClient httpClient;
        HttpPost httpPost;
        HttpResponse response;
        String responseContent;
        String body = bodyMap == null ? null : JSON.toJSONString(bodyMap);
        try {
            // 创建 httpClient
            httpClient = HttpClients.createDefault();

            httpPost = new HttpPost(url);
            httpPost.addHeader("Accept", "*/*");
            httpPost.addHeader("Content-Type", "application/json;charset=utf8");
            if(token != null)
                httpPost.addHeader("token", token);

            // set entity
            if(body != null)
                httpPost.setEntity(new StringEntity(body, StandardCharsets.UTF_8));

            // 发送请求
            response = httpClient.execute(httpPost);

            // 得到响应
            HttpEntity httpEntity = response.getEntity();
            responseContent = EntityUtils.toString(httpEntity, "UTF-8");

            // 释放资源
            EntityUtils.consume(httpEntity);

            return JSON.parseObject(responseContent, Result.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Result post(String url, String body, String token) {
        // post请求
        HttpClient httpClient;
        HttpPost httpPost;
        HttpResponse response;
        String responseContent;
        try {
            // 创建 httpClient
            httpClient = HttpClients.createDefault();

            httpPost = new HttpPost(url);
            httpPost.addHeader("Accept", "*/*");
            httpPost.addHeader("Content-Type", "application/json;charset=utf8");
            if(token != null)
                httpPost.addHeader("token", token);

            // set entity
            if(body != null)
                httpPost.setEntity(new StringEntity(body, StandardCharsets.UTF_8));

            // 发送请求
            response = httpClient.execute(httpPost);

            // 得到响应
            HttpEntity httpEntity = response.getEntity();
            responseContent = EntityUtils.toString(httpEntity, "UTF-8");

            // 释放资源
            EntityUtils.consume(httpEntity);

            return JSON.parseObject(responseContent, Result.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Result get(String url, Map<String, String> params, String token) {
        // post请求
        HttpClient httpClient;
        HttpGet httpGet;
        HttpResponse response;
        String responseContent;

        URIBuilder uriBuilder = null;

        try {
            // 创建 httpClient
            httpClient = HttpClients.createDefault();

            // set params
            if(params != null) {
                uriBuilder = new URIBuilder(url);
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    uriBuilder.setParameter(entry.getKey(), entry.getValue());
                }
            }
            if(uriBuilder == null)
                httpGet = new HttpGet(url);
            else
                httpGet = new HttpGet(uriBuilder.build());
            httpGet.addHeader("Accept", "*/*");
            httpGet.addHeader("Content-Type", "application/json;charset=utf8");
            if(token != null)
                httpGet.addHeader("token", token);

            // 发送请求
            response = httpClient.execute(httpGet);

            // 得到响应
            HttpEntity httpEntity = response.getEntity();
            responseContent = EntityUtils.toString(httpEntity, "UTF-8");

            // 释放资源
            EntityUtils.consume(httpEntity);

            return JSON.parseObject(responseContent, Result.class);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
