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

import net.mc_pandacraft.java.plugin.pandacraftutils.plugin_interface.EssentialsInterface;

import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.entity.Player;

public class CommandMe extends AbstractCommandExecutor {
	
	public CommandMe() {
		super("me");
	}

	
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		if (args.length == 0) return false;
		
		// nom qui doit être affiché
		String name;
		if (sender instanceof Player)
		{
			// pas de /me pour les mute
			if (EssentialsInterface.isPlayerMuted((Player)sender)) return true;
			
			name = ((Player)sender).getDisplayName()+ChatColor.RESET+" "+ChatColor.ITALIC;
		}
		else if (sender instanceof ConsoleCommandSender
				|| sender instanceof RemoteConsoleCommandSender)
			name = ChatColor.ITALIC+"Le serveur ";
		else
			name = ChatColor.ITALIC+"@ ";
		
		// message à afficher
		String message;
		if (!(sender instanceof Player) || ((Player)sender).hasPermission("pandacraft.me.color")) {
			message = ChatColor.translateAlternateColorCodes('&', StringUtils.join(args, ' '));
		}
		else {
			message = StringUtils.join(args, ' ');
		}
		
		plugin.getServer().broadcastMessage(name + message);
		
		return true;
	}

}
