package me.philippheuer.twitch4j;

import lombok.*;
import lombok.experimental.Wither;
import me.philippheuer.twitch4j.auth.CredentialManager;
import me.philippheuer.twitch4j.auth.model.OAuthCredential;
import me.philippheuer.twitch4j.streamlabs.StreamlabsClient;
import org.springframework.util.Assert;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

/**
 * Builder to get a TwitchClient Instance by provided various options, to provide the user with a lot of customizable options.
 * @author Damian Staszewski [https://github.com/stachu540]
 * @version %I%, %G%
 * @since 1.0
 */
@Getter
@Wither
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class TwitchClientBuilder {

	/**
	 * Client ID
	 */
	private String clientId;

	/**
	 * Client Secret
	 */
	private String clientSecret;

	/**
	 * IRC Credential
	 */
	private String credential;

	/**
	 * Auto Saving Configuration
	 */
	private boolean autoSaveConfiguration = false;

	/**
	 * Configuration Directory
	 */
	private File configurationDirectory;

	/**
	 * StreamLabs Client
	 * TODO: Remove
	 */
	private StreamlabsClient streamlabsClient = null;

	/**
	 * List of listeners
	 */
	private final Set<Object> listeners = new HashSet<>();

	/**
	 * Initializing builder
	 * @return Client Builder
	 */
	public static TwitchClientBuilder init() {
		return new TwitchClientBuilder();
	}

	/**
	 * Initialize
	 * @return {@link TwitchClient} initialized class
	 */
	public TwitchClient build() {
		// Reqired Parameters
		Assert.notNull(clientId, "You need to provide a client id!");
		Assert.notNull(clientSecret, "You need to provide a client secret!");

		final TwitchClient client = new TwitchClient(clientId, clientSecret);
		client.getCredentialManager().provideTwitchClient(client);
		client.getCredentialManager().setSaveCredentials(autoSaveConfiguration);
		if (configurationDirectory != null) {
			if (!configurationDirectory.exists()) {
				configurationDirectory.mkdirs();
			}

			client.setConfigurationDirectory(configurationDirectory);
			client.getCredentialManager().initializeConfiguration();
			client.getCommandHandler().initializeConfiguration();
		}

		if (credential != null) {
			client.getCredentialManager().addTwitchCredential(CredentialManager.CREDENTIAL_IRC,
					new OAuthCredential((credential.toLowerCase().startsWith("oauth:")) ? credential.substring(6) : credential));
		}

		// Streamlabs
		if(streamlabsClient != null) {
			client.setStreamLabsClient(streamlabsClient);
			client.getCredentialManager().provideStreamlabsClient(client.getStreamLabsClient());
		}

		if (listeners.size() > 0) {
			listeners.forEach(client.getDispatcher()::registerListener);
		}

		return client;
	}

	public TwitchClientBuilder withListener(Object listener) {
		listeners.add(listener);
		return this;
	}

	/**
	 * Initialize and connect to twitch
	 * @return {@link TwitchClient} initialized class
	 */
	public TwitchClient connect() {
		Assert.notNull(credential, "You need provide a OAuth Credentials for the Bot. Use: https://twitchapps.com/tmi/ to generate a oauth key.");
		TwitchClient client = build();
		client.connect();
		return client;
	}
}

@@ -200,69 +200,8 @@
 al;%0A
-import me.philippheuer.twitch4j.streamlabs.StreamlabsClient;%0A
 impo
@@ -258,16 +258,43 @@
 o.File;%0A
+import java.util.Calendar;%0A
 import j
@@ -879,24 +879,110 @@
 redential;%0A%0A
+%09/**%0A%09 * IRC Credential - Refresh Token%0A%09 */%0A%09private String credentialRefreshToken;%0A%0A
 %09/**%0A%09 * Aut
@@ -1139,109 +1139,8 @@
 y;%0A%0A
-%09/**%0A%09 * StreamLabs Client%0A%09 * TODO: Remove%0A%09 */%0A%09private StreamlabsClient streamlabsClient = null;%0A%0A
 %09/**
@@ -2190,97 +2190,42 @@
 %0A%09%09%09
-client.getCredentialManager().addTwitchCredential(CredentialManager.CREDENTIAL_IRC,%0A%09%09%09%09%09
+OAuthCredential oAuthCredential = 
 new 
@@ -2331,53 +2331,66 @@
 ial)
-);%0A%09%09%7D
+;
 %0A%0A%09%09
+%09
 // 
-Streamlabs%0A%09%09if(streamlabsCli
+got a refresh token?%0A%09%09%09if(credentialRefreshTok
 en
-t
  != 
@@ -2404,139 +2404,237 @@
 %0A%09%09%09
-client.setStreamLabsClient(streamlabsClient);%0A%09%09%09client.getCredentialManager().provideStreamlabsClient(client.getStreamLabsClient()
+%09oAuthCredential.setRefreshToken(credentialRefreshToken);%0A%09%09%09%09oAuthCredential.setTokenExpiresAt(Calendar.getInstance());%0A%09%09%09%7D%0A%0A%09%09%09client.getCredentialManager().addTwitchCredential(CredentialManager.CREDENTIAL_IRC, oAuthCredential
 );%0A%09

Revert "Merge remote track from origin/develop to upstream/develop"