package de.st_ddt.crazyutil.paramitrisable;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldType;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import de.st_ddt.crazyplugin.exceptions.CrazyCommandNoSuchException;
import de.st_ddt.crazyplugin.exceptions.CrazyException;

public class WorldParamitrisable extends TypedParamitrisable<World>
{

	public WorldParamitrisable(final World defaultValue)
	{
		super(defaultValue);
	}

	public WorldParamitrisable(final Player player)
	{
		this(player.getWorld());
	}

	public WorldParamitrisable(final CommandSender sender)
	{
		this(sender instanceof Player ? ((Player) sender).getWorld() : null);
	}

	@Override
	public void setParameter(final String parameter) throws CrazyException
	{
		value = Bukkit.getWorld(parameter);
		if (value == null)
			throw new CrazyCommandNoSuchException("World", parameter, tab(parameter));
	}

	@Override
	public List<String> tab(final String parameter)
	{
		return tabHelp(parameter);
	}

	public static List<String> tabHelp(String parameter)
	{
		parameter = parameter.toLowerCase();
		final List<String> res = getWorldNames();
		final Iterator<String> it = res.iterator();
		while (it.hasNext())
			if (!it.next().toLowerCase().startsWith(parameter))
				it.remove();
		return res;
	}

	public static List<String> getWorldNames()
	{
		return getWorldNames(Bukkit.getWorlds());
	}

	public static List<String> getWorldNames(final World... worlds)
	{
		final List<String> res = new LinkedList<String>();
		for (final World world : Bukkit.getWorlds())
			res.add(world.getName());
		return res;
	}

	public static List<String> getWorldNames(final Collection<World> worlds)
	{
		final List<String> res = new LinkedList<String>();
		for (final World world : Bukkit.getWorlds())
			res.add(world.getName());
		return res;
	}

	public static List<World> getWorldsByEnvironment(final Environment... environments)
	{
		final List<World> res = new LinkedList<World>();
		final List<World> worlds = Bukkit.getWorlds();
		for (final Environment environment : environments)
			for (final World world : worlds)
				if (world.getEnvironment() == environment)
					res.add(world);
		return res;
	}

	public static List<World> getWorldsByEnvironment(final Collection<Environment> environments)
	{
		final List<World> res = new LinkedList<World>();
		final List<World> worlds = Bukkit.getWorlds();
		for (final Environment environment : environments)
			for (final World world : worlds)
				if (world.getEnvironment() == environment)
					res.add(world);
		return res;
	}

	public static List<World> getWorldsByType(final WorldType... types)
	{
		final List<World> res = new LinkedList<World>();
		final List<World> worlds = Bukkit.getWorlds();
		for (final WorldType type : types)
			for (final World world : worlds)
				if (world.getWorldType() == type)
					res.add(world);
		return res;
	}

	public static List<World> getWorldsByType(final Collection<WorldType> types)
	{
		final List<World> res = new LinkedList<World>();
		final List<World> worlds = Bukkit.getWorlds();
		for (final WorldType type : types)
			for (final World world : worlds)
				if (world.getWorldType() == type)
					res.add(world);
		return res;
	}
}

@@ -57,26 +57,25 @@
 va.util.
-Collection
+ArrayList
 ;%0D%0Aimpor
@@ -86,24 +86,26 @@
 va.util.
-Iterator
+Collection
 ;%0D%0Aimpor
@@ -1327,26 +1327,29 @@
 ist%3CString%3E 
-re
+world
 s = getWorld
@@ -1370,16 +1370,12 @@
 nal 
-Iterator
+List
 %3CStr
@@ -1383,128 +1383,212 @@
 ng%3E 
-it = res.iterator();%0D%0A%09%09while (it.hasNext())%0D%0A%09%09%09if (!it.next().toLowerCase().startsWith(parameter))%0D%0A%09%09%09%09it.remove();%0D%0A
+res = new ArrayList%3CString%3E(worlds.size());%0D%0A%09%09for (final String world : worlds)%0D%0A%09%09%09if (world.toLowerCase().startsWith(parameter))%0D%0A%09%09%09%09res.add(world);%0D%0A%09%09if (res.isEmpty())%0D%0A%09%09%09return worlds;%0D%0A%09%09else%0D%0A%09
 %09%09re

return a list of possible worlds, if the list would be empty otherwise