package vg.civcraft.mc.bettershards.bungee;

import java.util.List;
import java.util.Random;

import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;

import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import vg.civcraft.mc.mercury.ServiceManager;
import vg.civcraft.mc.mercury.config.MercuryConfigManager;
import vg.civcraft.mc.mercury.events.EventListener;
import vg.civcraft.mc.mercury.events.EventManager;

public class BungeeListener implements Listener, EventListener{

	private RedisBungeeAPI redisAPI = RedisBungee.getApi();
	private BetterShardsBungee plugin = BetterShardsBungee.getInstance();
	private List<String> servers;
	
	public BungeeListener() {
		servers = redisAPI.getAllServers();
		plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() {

			@Override
			public void run() {
				MercuryConfigManager.initialize();
				ServiceManager.getService(); // Initialize everything.
			}
			
		});
		EventManager.registerListener(this);
	}
	
	@EventHandler()
	public void playerJoinedBungeeNetwork(ServerConnectEvent event) {
		ProxiedPlayer p = event.getPlayer();
		if (redisAPI.getLastOnline(p.getUniqueId()) != -1)
			return;
		Random rand = new Random();
		synchronized(servers) {
			int random = rand.nextInt(servers.size()-1);
			String server = servers.get(random);
			ServerInfo sInfo = ProxyServer.getInstance().getServerInfo(server);
			p.connect(sInfo);
		}
	}

	@Override
	public void receiveMessage(String channel, String message) {
		if (!channel.equals("BetterShards"))
			return;
		String[] content = message.split(" ");
		if (!content[0].equals("removeServer"))
			return;
		synchronized (servers) {
			for (int x = 1; x < content.length; x++)
				servers.remove(content[x]);
		}
	}
}

@@ -1928,16 +1928,55 @@
 vers) %7B%0A
+%09%09%09servers = redisAPI.getAllServers();%0A
 %09%09%09for (

Fix servers not reallowing connection if exclusion removed