package net.mc_pandacraft.java.plugin.pandacraftutils.modules;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.bukkit.entity.Player;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerOptions;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.WrappedGameProfile;

import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;
import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager;

public class PacketOutServerInfoListener {
	private PandacraftUtils plugin = PandacraftUtils.getInstance();
	
	
	public PacketOutServerInfoListener() {
		
		ProtocolLibrary
			.getProtocolManager()
			.addPacketListener(
					new PacketAdapter(
							plugin,
							ListenerPriority.NORMAL,
							Arrays.asList(new PacketType[] { PacketType.Status.Server.OUT_SERVER_INFO }),
							new ListenerOptions[] { ListenerOptions.ASYNC })
					{
						
						@Override
						public void onPacketSending(PacketEvent event)
						{
							Collection<OnlinePlayer> pl_list = OnlinePlayerManager.getAllNotVanished();
							
							int count_player = 0;
							

							List<Player> plAdmin = new ArrayList<Player>();
							List<Player> plStaff = new ArrayList<Player>();
							List<Player> plPlayerUltimate = new ArrayList<Player>();
							List<Player> plPlayerPremium = new ArrayList<Player>();
							List<Player> plPlayer = new ArrayList<Player>();
							
							for (OnlinePlayer op : pl_list)
							{
								
								if (op.isInGroup("admins"))
									plAdmin.add(op.getPlayer());
								else if (op.isInStaff())
									plStaff.add(op.getPlayer());
								else if (op.isInGroup("ultimate"))
									plPlayerUltimate.add(op.getPlayer());
								else if (op.isInGroup("premium"))
									plPlayerPremium.add(op.getPlayer());
								else
									plPlayer.add(op.getPlayer());
								
								count_player++;
								
							}
							
							List<WrappedGameProfile> list = new ArrayList<WrappedGameProfile>();
							
							if (!plAdmin.isEmpty() || !plStaff.isEmpty())
							{
								list.add(new WrappedGameProfile("", "§cStaff connecté :"));
								for (Player p : plAdmin)
								{
									list.add(new WrappedGameProfile("", "§7 - "+p.getDisplayName()));
								}
								for (Player p : plStaff)
								{
									list.add(new WrappedGameProfile("", "§7 - "+p.getDisplayName()));
								}
							}
							if (!plPlayer.isEmpty() || !plPlayerPremium.isEmpty() || !plPlayerUltimate.isEmpty())
							{
								list.add(new WrappedGameProfile("", "§6Joueurs connectés :"));

								for (Player p : plPlayerUltimate)
								{
									list.add(new WrappedGameProfile("", "§7 - "+p.getDisplayName()));
								}
								for (Player p : plPlayerPremium)
								{
									list.add(new WrappedGameProfile("", "§7 - "+p.getDisplayName()));
								}
								for (Player p : plPlayer)
								{
									list.add(new WrappedGameProfile("", "§7 - "+p.getDisplayName()));
								}
							}
							
							
							event.getPacket().getServerPings().read(0).setPlayers(list);
							event.getPacket().getServerPings().read(0).setPlayersOnline(count_player);
							
							
							
							event.getPacket().getServerPings().read(0).setMotD(ConfigManager.getInstance().defaultConfig.pingMOTD);
						}
					}
				);
		
	}
}
