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

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager;
import net.mc_pandacraft.java.plugin.pandacraftutils.config.elements.MultiCommandConfigEntry;
import net.mc_pandacraft.java.plugin.pandacraftutils.config.elements.MultiCommandConfigEntry.MucoSubCommand;
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;
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;
import org.bukkit.scheduler.BukkitScheduler;

public class CommandMuco extends AbstractCommandExecutor {
	
	public CommandMuco() {
		super("muco");
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label,
			String[] args) {
		
		if (args.length == 0) {
			sendMucoLists(sender);
			return true;
		}
		
		
		
		String list_name = args[0].toLowerCase();
		
		MultiCommandConfigEntry list = ConfigManager.getInstance().multiCommandConfig.getEntry(list_name);
		
		if (list == null) {
			sender.sendMessage(ChatColor.RED+"La sous commande '"+list_name+"' n'existe pas");
			return true;
		}
		
		if ((sender instanceof Player) && !OnlinePlayerManager.get((Player)sender).hasPermission("pandacraft.muco."+list_name)) {
			sender.sendMessage(ChatColor.RED+"Vous n'avez pas la permission pour la sous commande '"+list_name+"'");
			return true;
		}
		
		
		// gestion des paramètres $1 $2 ...
		List<String> params = new ArrayList<String>();
		for (int i=1; i<args.length; i++) {
			params.add(args[i]);
		}
		
		
		
		
		
		for (MucoSubCommand line : list.getSubCommands()) {
			executeListedCommand(sender, line, params);
		}
		
		
		return true;
	}
	
	
	
	
	
	
	
	
	
	
	private void sendMucoLists(CommandSender sender) {
		
		List<String> list_names = getMucoLists((sender instanceof Player)?OnlinePlayerManager.get((Player) sender):null);

		sender.sendMessage(ChatColor.GOLD+"----- Liste des commandes /muco -----");
		
		for (String name : list_names) {
			sender.sendMessage(" - "+ChatColor.GRAY+name);
		}
		
		sender.sendMessage(ChatColor.GOLD+"-----------------------------------");
	}
	
	
	private List<String> getMucoLists(OnlinePlayer permittedPlayer) {
		Set<String> list_names = ConfigManager.getInstance().multiCommandConfig.getList();
		
		List<String> retList = new ArrayList<String>();
		
		for (String name : list_names) {
			if (permittedPlayer == null || permittedPlayer.hasPermission("pandacraft.muco."+name))
				retList.add(name);
		}
		
		return retList;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private void executeListedCommand(CommandSender sender, MucoSubCommand subCommand, List<String> params) {
		
		
		String cmd = subCommand.command;
		
		
		for (int i=0; i<params.size(); i++) {
			cmd = cmd.replaceAll("\\$"+(i+1), params.get(i));
		}
		
		sender = subCommand.console ? plugin.getServer().getConsoleSender() : sender;
		
		BukkitScheduler scheduler = plugin.getServer().getScheduler();
		
		Runnable executor = new Runnable() {
			CommandSender sender; String cmd;
			public Runnable init(CommandSender s, String c) {
				sender = s; cmd = c;
				return this;
			}
			
			@Override
			public void run() {
				plugin.getServer().dispatchCommand(sender, cmd);
			}
		}.init(sender, cmd);
		
		
		if (subCommand.async)
			scheduler.runTaskLaterAsynchronously(plugin, executor, subCommand.tick);
		else
			scheduler.runTaskLater(plugin, executor, subCommand.tick);
		
	}
	
	
	
	
	
	
	
	

}
