package com.elmakers.mine.bukkit.spell.builtin;

import com.elmakers.mine.bukkit.api.maps.MapController;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.spell.TargetingSpell;
import com.elmakers.mine.bukkit.utility.ConfigurationUtils;
import com.elmakers.mine.bukkit.utility.Target;
import com.elmakers.mine.bukkit.api.maps.URLMap;

public class CameraSpell extends TargetingSpell
{
	@SuppressWarnings("deprecation")
	@Override
	public SpellResult onCast(ConfigurationSection parameters) 
	{
		ItemStack newMapItem = null;
		Integer priority = ConfigurationUtils.getInteger(parameters, "priority", null);
		
		// Check for special case id
		if (parameters.contains("id"))
		{
			newMapItem = new ItemStack(Material.MAP, 1, (short)parameters.getInt("id", 0));
			String mapName = parameters.getString("name", "Image");
			ItemMeta meta = newMapItem.getItemMeta();
			// TODO: How to handle names with spaces in them?
			meta.setDisplayName(mapName);
			newMapItem.setItemMeta(meta);
		}

        MapController maps = controller.getMaps();
		
		// Check for special case url
		if (newMapItem == null) {
			String url = parameters.getString("url");
			if (url != null) {
				int x = parameters.getInt("x", 0);
				int y = parameters.getInt("y", 0);
				int width = parameters.getInt("width", 0);
				int height = parameters.getInt("height", 0);
				String mapName = parameters.getString("name", "Photo");
				newMapItem = maps.getURLItem(getWorld().getName(), url, mapName, x, y, width, height, priority);
			}
		}
		
		if (newMapItem == null) {
			Target target = getTarget();
			String playerName = parameters.getString("name");
			String metaName = null;
			if (playerName == null) 
			{
				if (target != null)
				{
					if (target.hasEntity()) {
						Entity targetEntity = target.getEntity();
						if (targetEntity instanceof Player) {
							playerName = ((Player)targetEntity).getName();
						} else {
							playerName = getMobSkin(targetEntity.getType());
							if (playerName != null) {
								metaName = targetEntity.getType().getName();
							}
						}
					} else {
						Block targetBlock = target.getBlock();
						if (targetBlock == null) {
							return SpellResult.NO_TARGET;
						}
						playerName = getBlockSkin(targetBlock.getType());
						if (playerName != null) {
							metaName = target.getBlock().getType().name();
						}
					}
				}

				if (playerName == null)
				{
					Player player = mage.getPlayer();
					if (player == null) {
						return SpellResult.NO_TARGET;
					}
					playerName = player.getName();
				}
			}
			if (parameters.contains("reload")) {
				maps.forceReloadPlayerPortrait(getWorld().getName(), playerName);
			}
			newMapItem = maps.getPlayerPortrait(getWorld().getName(), playerName, priority, metaName);
		}
		if (newMapItem == null) {
			return SpellResult.FAIL;
		}
		getWorld().dropItemNaturally(getLocation(), newMapItem);
		
		return SpellResult.CAST;
	}
}

@@ -2969,16 +2969,83 @@
 );%0A%09%09%09%7D%0A
+            metaName = (metaName == null) ? playerName : metaName;%0A
 %09%09%09newMa
@@ -3118,16 +3118,30 @@
 riority,
+ %22Photo of %22 +
  metaNam

Add "Photo of" prefix to Camera photos