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

import java.util.ArrayList;
import java.util.Arrays;
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.plugin_interface.EssentialsInterface;

public class PacketOutServerInfoListener {
	private PandacraftUtils plugin;
	
	
	public PacketOutServerInfoListener(PandacraftUtils pl) {
		plugin = pl;
		
		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)
						{
							Player[] pl_list = PacketOutServerInfoListener.this.plugin.getServer().getOnlinePlayers();
							
							int count_player = 0;
							

							List<Player> plAdmin = new ArrayList<Player>();
							List<Player> plModo = new ArrayList<Player>();
							List<Player> plPlayerUltimate = new ArrayList<Player>();
							List<Player> plPlayerPremium = new ArrayList<Player>();
							List<Player> plPlayer = new ArrayList<Player>();
							
							for (Player p : pl_list)
							{
								if (p != null && p.isOnline())
								{
									// on passe si le joueur est vanish
									if(EssentialsInterface.isPlayerVanished(p)) continue;
									
									if (p.hasPermission("pandacraft.grade.admins"))
										plAdmin.add(p);
									else if (p.hasPermission("pandacraft.grade.modos"))
										plModo.add(p);
									else if (p.hasPermission("pandacraft.grade.ultimate"))
										plPlayerUltimate.add(p);
									else if (p.hasPermission("pandacraft.grade.premium"))
										plPlayerPremium.add(p);
									else
										plPlayer.add(p);
									
									count_player++;
								}
								
							}
							
							List<WrappedGameProfile> list = new ArrayList<WrappedGameProfile>();
							
							if (!plAdmin.isEmpty() || !plModo.isEmpty())
							{
								list.add(new WrappedGameProfile("", "§cStaff connecté :"));
								for (Player p : plAdmin)
								{
									list.add(new WrappedGameProfile("", " - "+p.getDisplayName()));
								}
								for (Player p : plModo)
								{
									list.add(new WrappedGameProfile("", " - "+p.getDisplayName()));
								}
							}
							if (!plPlayer.isEmpty() || !plPlayerPremium.isEmpty() || !plPlayerUltimate.isEmpty())
							{
								list.add(new WrappedGameProfile("", "§6Joueurs connectés :"));

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