package net.simpvp.Ignore;

import java.util.UUID;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 * Takes over the /tell command.
 * Respects people ignoring other players, and also lets players /tell server
 */
public class TellCommand implements CommandExecutor {

	public boolean onCommand(
			CommandSender sender,
			Command cmd,
			String label,
			String[] args) {

		Player player = null;
		if (sender instanceof Player) {
			player = (Player) sender;
		}

		if (args.length < 2) {
			send_msg(player, "Usage: /tell <player> <private message ...>",
					ChatColor.RED);
			return true;
		}

		/* Only used as command argument */
		@SuppressWarnings("deprecation")
		Player target = Ignore.instance.getServer()
				.getPlayerExact(args[0]);

		String msg = args[1];
		for (int i = 2; i < args.length; i++) {
			msg += " " + args[i];
		}

		sendPM(player, target, msg, args[0].equalsIgnoreCase("server"));

		return true;
	}

	/**
	 * Have player send msg to target
	 */
	public static void sendPM(
			Player player,
			Player target,
			String msg,
			boolean is_target_server) {

		/* String of the player, in case it's null and it should
		 * be 'Server' */
		String splayer;
		UUID uplayer = null;
		if (player == null) {
			splayer = "Server";
		} else {
			splayer = player.getName();
			uplayer = player.getUniqueId();
		}

		if (target == null && !is_target_server) {
			/* Yes this is confusing, but the message is
			 * imitating default (Notchian) behavior */
			send_msg(player,
					"[" + splayer
					+ ": That player cannot be found]",
					ChatColor.GRAY);
			return;
		}

		String starget;
		UUID utarget = null;
		if (target == null) {
			starget = "Server";
		} else {
			starget = target.getName();
			utarget = target.getUniqueId();
		}

		/* Don't let people figure out if a vanished admin is
		 * online or not */
		if (player == null || target == null || player.canSee(target)) {
			send_msg(player, "-->" + starget + ": " + msg,
					ChatColor.GRAY);
			PMCommands.mPlayerList.put(uplayer, utarget);
		} else {
			send_msg(player,
					"[" + splayer
					+ ": That player cannot be found]",
					ChatColor.GRAY);
		}

		/* Only actually send the message to the recipient if they're
		 * not ignoring the sender */
		if (player == null || !Storage.getIsIgnoring(target, player)) {
			send_msg(target, "<--" + splayer + ": " + msg,
					ChatColor.GRAY);
			PMCommands.rPlayerList.put(utarget, uplayer);
		}

		return;
	}

	/**
	 * Common function for sending a message to somebody
	 */
	public static void send_msg(
			Player player,
			String message,
			ChatColor chatcolor) {

		if (player == null) {
			Ignore.instance.getLogger().info(message);
		} else {
			player.sendMessage(chatcolor + message);
		}
	}

}


@@ -1523,185 +1523,51 @@
 %0A%09%09%09
-/* Yes this is confusing, but the message is%0A%09%09%09 * imitating default (Notchian) behavior */%0A%09%09%09send_msg(player,%0A%09%09%09%09%09%22%5B%22 + splayer%0A%09%09%09%09%09+ %22: That player cannot be found%5D%22,%0A%09%09%09%09%09
+send_msg(player, %22No such player online.%22, 
 Chat
@@ -1568,28 +1568,27 @@
 , ChatColor.
-GRAY
+RED
 );%0A%09%09%09return
@@ -2064,74 +2064,35 @@
 yer,
-%0A%09%09%09%09%09%22%5B%22 + splayer%0A%09%09%09%09%09+ %22: That player cannot be found%5D%22,%0A%09%09%09%09%09
+ %22No such player online.%22, 
 Chat
@@ -2093,28 +2093,27 @@
 , ChatColor.
-GRAY
+RED
 );%0A%09%09%7D%0A%0A%09%09/*

Change the offline player message