package org.wcs.plugin.webHttpClient.model;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.Map;

@Setter
@Getter
public class HttpRequest {

    /**
     * 请求id
     */
    private String id;

    /**
     * 是否自动重试
     */
    private boolean autoRetry;

    /**
     * 请求地址
     */
    private String url;

    /**
     * 访问地址的key
     */
    private String addressKey;

    /**
     * 请求方式
     */
    private String method;

    /**
     * 请求体
     */
    private String body;

    /**
     * 请求头
     */
    private Map<String, String> headers;

    /**
     * 请求参数
     */
    private Map<String, String> params;

    /**
     * 编码格式
     */
    private String charset;

    /**
     * 请求内容类型
     */
    private String contentType;

    /**
     * 超时时间
     */
    private int timeout;

    /**
     * 重试次数
     */
    private int retryCount;

    /**
     * 下次重试时间
     */
    private LocalDateTime nextRetryTime;

    /**
     * 构建开始
     * @return 返回一个实例
     */
    public static HttpRequest buildStart() {
        return new HttpRequest();
    }


    /**
     * 设置 GET 请求方式
     * @return HttpRequest
     */
    public HttpRequest get() {
        this.method = "GET";
        return this;
    }

    /**
     * 设置 POST 请求方式
     * @return HttpRequest
     */
    public HttpRequest post() {
        this.method = "POST";
        return this;
    }

    /**
     * 设置 PUT 请求方式
     * @return HttpRequest
     */
    public HttpRequest put() {
        this.method = "PUT";
        return this;
    }

    /**
     * 设置 DELETE 请求方式
     * @return HttpRequest
     */
    public HttpRequest delete() {
        this.method = "DELETE";
        return this;
    }

    /**
     * 设置请求ID
     * @param id 请求ID
     * @return HttpRequest
     */
    public HttpRequest setId(String id) {
        this.id = id;
        return this;
    }

    /**
     * 设置自动重试
     * @param autoRetry 是否自动重试
     * @return HttpRequest
     */
    public HttpRequest autoRetry(boolean autoRetry) {
        this.autoRetry = autoRetry;
        return this;
    }

    /**
     * 默认开启自动重试
     * @return HttpRequest
     */
    public HttpRequest autoRetry() {
        this.autoRetry = true;
        return this;
    }

    /**
     * 设置请求地址
     * @param url 请求地址
     * @return HttpRequest
     */
    public HttpRequest setUrl(String url) {
        this.url = url;
        this.addressKey = null;
        return this;
    }

    /**
     * 设置请求 key ，前提必须设置key才行，否则请使用 url
     * @param addressKey 请求key
     * @return HttpRequest
     */
    public HttpRequest setAddressKey(String addressKey) {
        this.addressKey = addressKey;
        this.url = null;
        return this;
    }

    /**
     * 设置请求 body
     * @param body 请求 body
     * @return HttpRequest
     */
    public HttpRequest setBody(String body) {
        this.body = body;
        return this;
    }

    /**
     * 添加请求头
     * @param headers 请求头
     * @return HttpRequest
     */
    public HttpRequest setHeaders(Map<String, String> headers) {
        this.headers = headers;
        return this;
    }

    /**
     * 添加请求参数
     * @param params 请求参数
     * @return HttpRequest
     */
    public HttpRequest setParams(Map<String, String> params) {
        this.params = params;
        return this;
    }

    /**
     * 设置编码
     * @param charset 编码
     * @return HttpRequest
     */
    public HttpRequest setCharset(String charset) {
        this.charset = charset;
        return this;
    }

    /**
     * 设置超时时间
     * @param timeout 超时时间
     * @return HttpRequest
     */
    public HttpRequest setTimeout(int timeout) {
        this.timeout = timeout;
        return this;
    }

    /**
     * 设置Content-Type
     * @param contentType Content-Type
     * @return HttpRequest
     */
    public HttpRequest setContentType(String contentType) {
        this.contentType = contentType;
        return this;
    }

    /**
     * 构建HttpRequest
     * @return HttpRequest
     */
    public HttpRequest buildEnd() {
        if(this.method == null) {
            this.method = "GET";
        }
        if(this.contentType == null) {
            this.contentType = "application/json";
        }
        if(this.charset == null) {
            this.charset = "UTF-8";
        }
        if(this.timeout <= 0) {
            this.timeout = 15000;
        }
        return this;
    }

    @Override
    public String toString() {
        return "HttpRequest{" +
                "url='" + url + '\'' +
                ", addressKey='" + addressKey + '\'' +
                ", method='" + method + '\'' +
                ", body='" + body + '\'' +
                ", headers=" + headers +
                ", params=" + params +
                ", charset='" + charset + '\'' +
                ", contentType='" + contentType + '\'' +
                ", timeout=" + timeout +
                '}';
    }

}
