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

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

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

public class CommandAfk extends AbstractCommandExecutor {
	
	public CommandAfk() {
		super("afk");
	}


	
	
	
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
	{
		boolean execute_self = false;
		boolean execute_other = false;
		Player other_player = null;
		
		if(sender instanceof Player)
		{
			if (args.length > 0)
			{
				if (((Player)sender).hasPermission("pandacraft.afk.other"))
				{
					if (plugin.getServer().getPlayer(args[0]) != null)
					{
						other_player = plugin.getServer().getPlayer(args[0]);
						execute_other = true;
					}
					else
						sender.sendMessage("§cLe joueur n'existe pas.");
				}
				else
				{
					sender.sendMessage("§cVous n'avez pas la permission d'effectuer cette action sur un autre joueur.");
				}
			}
			else
				execute_self = true;
	    }
		else
		{
			if (args.length > 0)
			{
				if (plugin.getServer().getPlayer(args[0]) != null)
				{
					other_player = plugin.getServer().getPlayer(args[0]);
					execute_other = true;
				}
				else
					sender.sendMessage("§cLe joueur n'existe pas.");
			}
			else
				sender.sendMessage("§cVeuillez indiquer un joueur.");
			
		}
		
		
		if (execute_self)
		{
			Player p = (Player)sender;
			OnlinePlayer ap = OnlinePlayerManager.get(p);
			if (ap == null)
				plugin.getLogger().severe("Cant find AfkPlayer with name "+p.getName()+" when performing command /afk");
			else
				ap.performAfkCommand();
			return true;
		}
		else if (execute_other && other_player != null)
		{
			OnlinePlayer ap = OnlinePlayerManager.get(other_player);
			if (ap == null)
				plugin.getLogger().severe("Cant find AfkPlayer with name "+other_player.getName()+" when performing command /afk <player>");
			ap.adminToggleAfk();
			return true;
		}
		
		
		
		
		
		return false;
	}
	
	
}
