package net.mc_pandacraft.java.plugin.pandacraftutils.simple_commands._command_alias;

import java.util.List;
import java.util.Map;

import net.mc_pandacraft.java.plugin.pandacraftutils.ConfigManager;
import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;

public class CommandAliasManager implements Listener {
	private PandacraftUtils plugin;
	
	
	public CommandAliasManager(PandacraftUtils pl) {
		plugin = pl;
		plugin.getServer().getPluginManager().registerEvents(this, plugin);
		
		
		
	}

	@EventHandler(priority=EventPriority.HIGHEST)
	public void onPlayerCommandPreprocess (PlayerCommandPreprocessEvent event)
	{
		Player p = event.getPlayer();
		
		List<Map<String, String>> aliases = ConfigManager.getInstance().CommandAlias_alias;
		for (Map<String, String> alias : aliases) {
			if (alias == null)
				continue;
			String perm = alias.get("permissions");
			String initialCmd = alias.get("initialCommand");
			String aliasCmd = alias.get("replaceWith");
			if (perm == null || initialCmd == null || aliasCmd == null)
				continue;
			
			if (!p.hasPermission(perm))
				continue;

			String mess = event.getMessage();
			
			if(mess.toLowerCase().startsWith(initialCmd))
				mess = mess.replaceFirst(initialCmd, aliasCmd);
			else
				continue;
			
			event.setMessage(mess);
			return;
		}
		
	}
	
	
	
}
