package com.drops.utils;

import cn.hutool.http.HttpGlobalConfig;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.drops.ui.MainController;
import sun.applet.Main;

import java.awt.*;
import java.net.Proxy;

/**
 * @ClassName: HTTPUtils
 * @Description: TODO
 * @Author: Summer
 * @Date: 2021/7/28 9:39
 * @Version: v1.0.0
 * @Description:
 **/
public class HTTPUtils {
    private static final int Timeout = 500000;
    public String DefalutEncoding = "UTF-8";
    private String method = "get";


    public HTTPUtils(){
        // 设置全局timeout
        HttpGlobalConfig.setTimeout(Timeout);
    }
    public HTTPUtils(int Timeout){
        // 设置全局timeout

        HttpGlobalConfig.setTimeout(Timeout);
    }

    public static HttpResponse deleteRequest(String target, String s) {
        String url = URLUtil.normalizeURL(target);
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = null;
        if (proxy == null){
            result = HttpRequest.delete(url).execute();
        }else {
            result = HttpRequest.delete(url).setProxy(proxy).execute();
        }

        return result;
    }



    public void setTimeout(int Timeout){
        HttpGlobalConfig.setTimeout(Timeout);
    }

    /**
     * @Description: 对Spring Boot 版本的Get请求处理
     * @param target 目标
     *
     * @return:
     */

    public static HttpResponse getRequest(String target){
        String url = URLUtil.normalizeURL(target);
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = null;
        if (proxy == null){
            result = HttpRequest.get(url).execute();
        }else {
            result = HttpRequest.get(url).setProxy(proxy).execute();
        }

        return result;
    }

    public static HttpResponse getRequest(String target, String point){
        String url = URLUtil.normalizeURL(target) + point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = null;
        System.out.println(url);
        if (proxy == null){
            result = HttpRequest.get(url).execute();
        }else {
            result = HttpRequest.get(url).setProxy(proxy).execute();
        }

        return result;
    }
    /**
     * @Description: 对Spring Boot 版本的Get请求处理并设置代理
     * @param target 目标
     *
     * @return:
     */
    public static HttpResponse getRequestProxy(String target){
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        String url = URLUtil.normalizeURL(target);
        HttpResponse result = HttpRequest.get(target)
                .setProxy(proxy).execute();
        return result;
    }

    /**
     * @Description: 对Spring Boot V1 版本的Post请求处理
     * @param target 目标
     * @param Point 端点
     *
     * @return:
     */
    public static HttpResponse postRequestV1(String target, String Point){
        String url = URLUtil.normalizeURL(target) + Point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = null;
        if (proxy != null){
            result = HttpRequest.post(url)
                    .header("Content-Type","application/x-www-form-urlencoded")
                    .setProxy(proxy).execute();
        }else {
            result = HttpRequest.post(url)
                    .header("Content-Type","application/x-www-form-urlencoded")
                    .execute();
        }

        return result;
    }
    public static HttpResponse postRequestV1(String target, String Point, String boby){
        String url = URLUtil.normalizeURL(target) + Point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = null;
        if (proxy != null){
            result = HttpRequest.post(url)
                    .header("Content-Type","application/x-www-form-urlencoded")
                    .body(boby)
                    .setProxy(proxy).execute();
        }else {
            result = HttpRequest.post(url)
                    .body(boby)
                    .header("Content-Type","application/x-www-form-urlencoded")
                    .execute();
        }

        return result;
    }

    public static HttpResponse postRequestV1Proxy(String target, String Point){
        String url = URLUtil.normalizeURL(target) + Point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = HttpRequest.post(url)
                .header("Content-Type","application/x-www-form-urlencoded")
                .setProxy(proxy).execute();
        return result;


    }

    public static HttpResponse H2PostRequest(String target, String boby){

        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");

        HttpResponse result;
        if (proxy != null){
            result = HttpRequest.post(target)
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .header("Referer", target)
                    .body(boby)
                    .setProxy(proxy).execute();
        }else {
            result = HttpRequest.post(target)
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .header("Referer", target)
                    .body(boby)
                    .execute();
        }


        return result;
    }
    public static HttpResponse postRequestjson(String target, String point ,String json){

        String url = URLUtil.normalizeURL(target) + point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");

        HttpResponse result;
        if (proxy != null){
            result = HttpRequest.post(url)
                    .header("Content-Type", "application/json")
                    .header("Referer", target)
                    .body(json)
                    .setProxy(proxy).execute();
        }else {
            result = HttpRequest.post(url)
                    .header("Content-Type", "application/json")
                    .header("Referer", target)
                    .body(json)
                    .execute();
        }

        return result;
    }

    public static HttpResponse postRequestV1Proxy(String target, String Point, String boby){
        String url = URLUtil.normalizeURL(target) + Point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = HttpRequest.post(url)
                .header("Content-Type","application/x-www-form-urlencoded")
                .body(boby)
                .setProxy(proxy).execute();
        return result;


    }

    /**
     * @Description: 对Spring Boot V2 版本的Post请求处理
     * @param target 目标
     * @param Point 端点
     *
     * @return:
     */
    public static HttpResponse postRequestV2(String target, String Point){
        String url = URLUtil.normalizeURL(target) + Point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = null;
        if (proxy != null){
            result = HttpRequest.post(url)
                    .header("Content-Type","application/json")
                    .header("Cache-Control", "max-age=0")
                    .setProxy(proxy).execute();
        }else {
            result = HttpRequest.post(url)
                    .header("Cache-Control", "max-age=0")
                    .header("Content-Type","application/json")
                    .execute();
        }

        return result;

    }
    public static HttpResponse postRequestV2(String target, String Point, String boby){
        String url = URLUtil.normalizeURL(target) + Point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");
        HttpResponse result = null;
        if (proxy != null){
            result = HttpRequest.post(url)
                    .header("Content-Type","application/json")
                    .header("Cache-Control", "max-age=0")
                    .body(boby)
                    .setProxy(proxy).execute();
        }else {
            result = HttpRequest.post(url)
                    .header("Cache-Control", "max-age=0")
                    .header("Content-Type","application/json")
                    .body(boby)
                    .execute();
        }

        return result;

    }

    public static HttpResponse postRequestV2Proxy(String target, String Point){
        String url = URLUtil.normalizeURL(target) + Point;
        Proxy proxy = (Proxy) MainController.currentProxy.get("proxy");

        HttpResponse result = HttpRequest.post(url)
                .header("Content-Type","application/json")
                .setProxy(proxy).execute();
        return result;
    }

    public static void main(String[] args) {
        String url = "https://www.baidu.com/";

        HTTPUtils httpUtils = new HTTPUtils(6000);
        System.out.println(getRequest(url).getStatus());
    }











}
