package me.sirenninja.ultimatetroll.Commands;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;

import me.sirenninja.ultimatetroll.UltimateTrollMain;
import me.sirenninja.ultimatetroll.Utils.GlobalUtils;

public class Summon implements CommandExecutor{
	UltimateTrollMain plugin;
	 
	public Summon(UltimateTrollMain instance) {
		plugin = instance;
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
		if(!(sender instanceof Player))
			return true;
		
		Player player = (Player) sender;
		if(cmd.getName().equalsIgnoreCase("pranksummon")){
			if(!(player.hasPermission("ultroll.prank.summon"))){
				GlobalUtils.playerMessage(player, plugin.getConfig().getString("global.noPerms"));
				return true;
			}

			if(args.length > 0){
				if(plugin.getServer().getPlayer(args[0]) != null){
					Player target = plugin.getServer().getPlayer(args[0]);
					
					if(plugin.getConfig().getBoolean("global.dontTrollBypassedUsers")){
						if(target.hasPermission("ultroll.bypass")){
							GlobalUtils.playerMessage(player, plugin.getConfig().getString("global.userCantBeTrolled"));
							return true;
						} 
					}
					
					if(!(args.length >= 2)){
						GlobalUtils.playerMessage(player, "&4Use: /psummon <player> <mobname> <amount>");
						return true;
					}
					
					World world = target.getWorld();
	                Location TargetLocation = target.getLocation();
	                
	                if(!(isEntity(args[1].toUpperCase()))){
	                	GlobalUtils.playerMessage(player, "&4" + args[1] + " is not a valid entity.");
	                	return true;
	                }
	                
	                if(args.length == 3){
	                	if(!(args[2].matches("\\d+"))){
							GlobalUtils.playerMessage(player, "&4&l" + args[2] + " &4is not a number.");
							return true;
						}
	                	
	                	for(int i = 1; i <= Integer.parseInt(args[2]); i++){
	                		world.spawnEntity(TargetLocation, EntityType.valueOf(args[1].toUpperCase()));
	                	}
	                	
	                	String msg = plugin.getConfig().getString("summon.msg")
	                			.replaceAll("%amount%", args[2])
	                			.replaceAll("%user%", target.getName());
	                	
	                	GlobalUtils.playerMessage(player, msg);
	                }else{
	                	world.spawnEntity(TargetLocation, EntityType.valueOf(args[1].toUpperCase()));
	                }
	                
				}else{
					GlobalUtils.playerMessage(player, "&4This user does not exist.");
				}
			}else{
				GlobalUtils.playerMessage(player, "&4Specify a user.");
			}
		}
		return true;
	}
	
	private static boolean isEntity(String mob){
		for (EntityType e : EntityType.values()){
            if(e.toString().equalsIgnoreCase(mob)){
                return true;
            }
        }
		return false;
	}
}
