package com.namelessmc.java_api;

import com.github.mizosoft.methanol.Methanol;
import com.google.gson.GsonBuilder;
import com.namelessmc.java_api.logger.ApiLogger;
import com.namelessmc.java_api.logger.PrintStreamLogger;
import com.namelessmc.java_api.logger.Slf4jLogger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.ProxySelector;
import java.net.URL;
import java.time.Duration;
import java.util.Objects;

public class NamelessApiBuilder {

	private final @NonNull URL apiUrl;
	private final @NonNull String apiKey;

	private Duration timeout = Duration.ofSeconds(10);
	private int responseSizeLimit = 32*1024*1024;
	private String userAgent = "Nameless-Java-API";
	private @Nullable ApiLogger debugLogger = null;
	private @Nullable ProxySelector proxy = null;
	private @Nullable Authenticator authenticator = null;

	private boolean pettyJsonRequests = false;

	NamelessApiBuilder(final @NonNull URL apiUrl,
					   final @NonNull String apiKey) {
		try {
			this.apiUrl = apiUrl.toString().endsWith("/") ? apiUrl : new URL(apiUrl + "/");
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
		this.apiKey = apiKey;
	}

	public NamelessApiBuilder userAgent(final String userAgent) {
		this.userAgent = userAgent;
		return this;
	}

	public NamelessApiBuilder stdErrDebugLogger() {
		this.debugLogger = PrintStreamLogger.DEFAULT_INSTANCE;
		return this;
	}

	public NamelessApiBuilder slf4jDebugLogger() {
		this.debugLogger = Slf4jLogger.DEFAULT_INSTANCE;
		return this;
	}

	public NamelessApiBuilder customDebugLogger(final @Nullable ApiLogger debugLogger) {
		this.debugLogger = debugLogger;
		return this;
	}

	public NamelessApiBuilder timeout(final Duration timeout) {
		this.timeout = Objects.requireNonNull(timeout);
		return this;
	}

	public NamelessApiBuilder withProxy(final @Nullable ProxySelector proxy) {
		this.proxy = proxy;
		return this;
	}

	public NamelessApiBuilder authenticator(final @Nullable Authenticator authenticator) {
		this.authenticator = authenticator;
		return this;
	}

	public @NonNull NamelessApiBuilder pettyJsonRequests() {
		this.pettyJsonRequests = true;
		return this;
	}

	public @NonNull NamelessApiBuilder responseSizeLimit(int responseSizeLimitBytes) {
		this.responseSizeLimit = responseSizeLimitBytes;
		return this;
	}

	public @NonNull NamelessAPI build() {
		final Methanol.Builder methanolBuilder = Methanol.newBuilder()
				.defaultHeader("Authorization", "Bearer " + this.apiKey)
				.userAgent(this.userAgent)
				.autoAcceptEncoding(true)
				.readTimeout(this.timeout)
				.requestTimeout(this.timeout)
				.connectTimeout(this.timeout)
				.headersTimeout(this.timeout);
		if (this.proxy != null) {
			methanolBuilder.proxy(this.proxy);
		}
		if (this.authenticator != null) {
			methanolBuilder.authenticator(this.authenticator);
		}

		GsonBuilder gsonBuilder = new GsonBuilder()
				.disableHtmlEscaping();

		if (this.pettyJsonRequests) {
			gsonBuilder.setPrettyPrinting();
		}

		return new NamelessAPI(
				new RequestHandler(
						this.apiUrl,
						methanolBuilder.build(),
						gsonBuilder.create(),
						this.debugLogger,
						this.responseSizeLimit
				),
				this.apiUrl,
				this.apiKey
		);
	}

}

@@ -508,16 +508,49 @@
 et.URL;%0A
+import java.net.http.HttpClient;%0A
 import j
@@ -1004,16 +1004,74 @@
  = null;
+%0A%09private HttpClient.@Nullable Version httpVersion = null;
 %0A%0A%09priva
@@ -2281,33 +2281,24 @@
 %09%7D%0A%0A%09public 
-@NonNull 
 NamelessApiB
@@ -2382,33 +2382,24 @@
 %09%7D%0A%0A%09public 
-@NonNull 
 NamelessApiB
@@ -2531,24 +2531,158 @@
 %09public 
-@NonNull
+NamelessApiBuilder httpversion(final HttpClient. @Nullable Version httpVersion) %7B%0A%09%09this.httpVersion = httpVersion;%0A%09%09return this;%0A%09%7D%0A%0A%09public
  Nameles
@@ -3176,24 +3176,108 @@
 icator);%0A%09%09%7D
+%0A%09%09if (this.httpVersion != null) %7B%0A%09%09%09methanolBuilder.version(this.httpVersion);%0A%09%09%7D
 %0A%0A%09%09GsonBuil

Allow forcing http version