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

import java.util.List;

import net.mc_pandacraft.java.plugin.pandacraftutils.modules.AutoMessagesManager.AutoMessage;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager;

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

public class CommandAutomessager extends AbstractCommandExecutor {
	
	public CommandAutomessager() {
		super("automessager");
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String alias,
			String[] args) {
		
		List<AutoMessage> messages = plugin.autoMessagesManager.getMessagesFromConfig();
		
		if (args.length == 0 || !(sender instanceof Player)) {
			sender.sendMessage(ChatColor.GOLD+"Il y a "+messages.size()+" messages (de 0 à "+(messages.size()-1)+")");
			return true;
		}
		
		int id;
		try {
			id = Integer.parseInt(args[0]);
		} catch (NumberFormatException e) {
			sender.sendMessage("§cLa valeur indiquée doit être un chiffre");
			return false;
		}
		
		if (id < 0 || id >= messages.size()) {
			sender.sendMessage("§cLe message numéro "+id+" n'existe pas");
			return false;
		}
		
		OnlinePlayerManager
				.get((Player)sender)
				.sendMessageFromServer(messages.get(id).getMessage());
		
		
		
		return true;
	}

}
