package com.namelessmc.plugin.spigot;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;

import com.namelessmc.java_api.NamelessException;

public class WhitelistRegistered implements Runnable {

	public WhitelistRegistered() {
		// Return if this feature is disabled
		if (!Config.MAIN.getConfig().getBoolean("auto-whitelist-registered.enabled", false)) {
			return;
		}

		this.run();
	}

	@Override
	public void run() {
		final boolean hideInactive = Config.MAIN.getConfig().getBoolean("auto-whitelist-registered.exclude-inactive");
		final boolean hideBanned = Config.MAIN.getConfig().getBoolean("auto-whitelist-registered.exclude-banned");
		final boolean log = Config.MAIN.getConfig().getBoolean("auto-whitelist-registered.log");

		final Logger logger = NamelessPlugin.getInstance().getLogger();

		Bukkit.getScheduler().runTaskAsynchronously(NamelessPlugin.getInstance(), () -> {
			final Set<UUID> uuids;
			try {
				uuids = NamelessPlugin.getInstance().api.getRegisteredUsers(hideInactive, hideBanned).keySet();
			} catch (final NamelessException e) {
				logger.warning(
						"An error occured while getting a list of registered users from the website for the auto-whitelist-registered feature.");
				e.printStackTrace();
				return;
			}
			
			// Use Set for O(1) contains()
			final Set<String> excludes = new HashSet<>();
			excludes.addAll(Config.MAIN.getConfig().getStringList("auto-whitelist-registered.log"));
			uuids.removeIf(u -> excludes.contains(u.toString()));

			Bukkit.getScheduler().runTask(NamelessPlugin.getInstance(), () -> {
				for (final OfflinePlayer whitelistedPlayer : Bukkit.getWhitelistedPlayers()) {
					if (!uuids.contains(whitelistedPlayer.getUniqueId())) {
						// The player is whitelisted, but no(t) (longer) registered.
						whitelistedPlayer.setWhitelisted(false);
						uuids.remove(whitelistedPlayer.getUniqueId());
						if (log) {
							logger.info("Removed " + whitelistedPlayer.getName() + " from the whitelist.");
						}
					}
				}

				// All remaining UUIDs in the set are from players that are not on the whitelist yet
				for (final UUID uuid : uuids) {
					final OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
					player.setWhitelisted(true);
					if (log) {
						logger.info("Added " + player.getName() + " to the whitelist.");
					}
				}

				// Run again after wait time
				final long pollInterval = Config.MAIN.getConfig().getInt("auto-whitelist-registered.poll-interval") * 20;
				Bukkit.getScheduler().runTaskLater(NamelessPlugin.getInstance(), this, pollInterval);
			});
		});
	}

}

@@ -1544,65 +1544,8 @@
 %22));
-%0A%09%09%09uuids.removeIf(u -%3E excludes.contains(u.toString()));
 %0A%0A%09%09
@@ -1642,28 +1642,17 @@
 ePlayer 
-whitelistedP
+p
 layer : 
@@ -1661,27 +1661,23 @@
 kkit.get
-Whitelisted
+Offline
 Players(
@@ -1690,88 +1690,256 @@
 %09%09%09%09
-if (!uuids.contains(whitelistedPlayer.getUniqueId())) %7B%0A%09%09%09%09%09%09// The 
+final UUID uuid = player.getUniqueId();%0A%09%09%09%09%09final String name = player.getName() == null ? uuid.toString() : player.getName();%0A%09%09%09%09%09if (excludes.contains(uuid.toString())) %7B%0A%09%09%09%09%09%09continue;%0A%09%09%09%09%09%7D%0A%09%09%09%09%09%0A%09%09%09%09%09if (uuids.contains(uuid) && !
 player
- is w
+.isW
 hite
@@ -1948,59 +1948,106 @@
 sted
-, but no(t) (longer) registered.%0A%09%09%09%09%09%09whitelistedP
+()) %7B%0A%09%09%09%09%09%09if (log) %7B%0A%09%09%09%09%09%09%09logger.info(%22Added %22 + name + %22 to the whitelist.%22);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09p
 laye
@@ -2063,20 +2063,19 @@
 elisted(
-fals
+tru
 e);%0A%09%09%09%09
@@ -2079,55 +2079,69 @@
 %09%09%09%09
-%09uuids.remove(whitelistedPlayer.getUniqueI
+%7D else if (!uuids.contains(uuid) && player.isWhiteliste
 d())
-;
+ %7B
 %0A%09%09%09
@@ -2190,35 +2190,12 @@
 %22 + 
-whitelistedPlayer.getN
+n
 ame
-()
  + %22
@@ -2222,24 +2222,60 @@
 %22);%0A%09%09%09%09%09%09%7D%0A
+%09%09%09%09%09%09player.setWhitelisted(false);%0A
 %09%09%09%09%09%7D%0A%09%09%09%09%7D

implement it properly