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

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.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.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 get(String url, Map<String, Object> params, String token) {
        // post请求
        HttpClient httpClient;
        HttpGet httpGet;
        HttpResponse response;
        String responseContent;

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

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

            // set params
            if(params != null) {
                BasicHttpParams httpParams = new BasicHttpParams();
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    httpParams.setParameter(entry.getKey(), entry.getValue());
                }
                httpGet.setParams(httpParams);
            }

            // 发送请求
            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();
        }
        return null;
    }
}
