package net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors;

import java.util.Collection;

import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.Response;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager;

public class RequestExecutorPlayerList extends AbstractRequestExecutor {
	
	public RequestExecutorPlayerList() {
		super("player_list");
	}

	@Override
	protected Response run(String data) {
		
		boolean ope = data.trim().equalsIgnoreCase("op");
		
		Collection<OnlinePlayer> players = ope ?
				OnlinePlayerManager.getAll() : 
					OnlinePlayerManager.getAllNotVanished();
		
		String nul = "\u0000", nl = "\n";
		
		StringBuilder returnData = new StringBuilder();
		
		// d'abord, le nombre de connecté / le nombre de slot
		returnData.append(players.size()+"/"+plugin.getServer().getMaxPlayers());
		
		for (OnlinePlayer op : players) {

			returnData.append(nl+op.getPlayer().getName());
			
			returnData.append(nul+op.getPlayer().getDisplayName());
			
			returnData.append(nul+(op.isAfk()?"0":"1"));
			

			returnData.append(nul+op.getPlayer().getGameMode().toString());
			returnData.append(nul+(int)op.getPlayer().getHealth());
			returnData.append(nul+(int)op.getPlayer().getFoodLevel());
			
			
			returnData.append(nul+(op.isVanished()?"0":"1"));
			
		}
		
		
		return new Response(true, returnData.toString());
	}

}
