package com.elmakers.mine.bukkit.plugins.magic.spells;

import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import com.elmakers.mine.bukkit.plugins.magic.Spell;
import com.elmakers.mine.bukkit.plugins.magic.SpellResult;
import com.elmakers.mine.bukkit.utilities.Target;
import com.elmakers.mine.bukkit.utilities.URLMap;
import com.elmakers.mine.bukkit.utilities.borrowed.ConfigurationNode;

public class CameraSpell extends Spell
{
	@SuppressWarnings("deprecation")
	@Override
	public SpellResult onCast(ConfigurationNode parameters) 
	{
		ItemStack newMapItem = null;
		Integer priority = parameters.getInteger("priority", null);
		
		// Check for special case id
		if (parameters.containsKey("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);
		}
		
		// 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 = URLMap.getURLItem(url, x, y, width, height, mapName, priority);
			}
		}
		
		if (newMapItem == null) {
			this.targetEntity(LivingEntity.class);
			Target target = getTarget();
			String playerName = parameters.getString("name");
			String metaName = null;
			if (playerName == null) 
			{
				Player player = getPlayer();
				if (target != null)
				{
					if (target.isEntity()) {
						if (target instanceof Player) {
							playerName = ((Player)target.getEntity()).getName();
						} else {
							playerName = getMobSkin(target.getEntity().getType());
							if (playerName != null) {
								metaName = target.getEntity().getType().getName();
							}
						}
					} else {
						playerName = getBlockSkin(target.getBlock().getType());
						if (playerName != null) {
							metaName = target.getBlock().getType().name();
						}
					}
				}
				
				if (playerName == null && player != null)
				{
					castMessage("Selfie!");
					playerName = player.getName();
				} else {
					castMessage("CLICK!");
				}
			}
			if (parameters.containsKey("reload")) {
				URLMap.forceReloadPlayerPortrait(playerName);
			}
			newMapItem = URLMap.getPlayerPortrait(playerName, priority, metaName);
		}
		if (newMapItem == null) {
			sendMessage("Failed to load photo");
			return SpellResult.FAILURE;
		}
		getPlayer().getWorld().dropItemNaturally(getPlayer().getLocation(), newMapItem);
		getPlayer().getWorld().playEffect(getPlayer().getEyeLocation(), Effect.SMOKE, 0);
		
		return SpellResult.SUCCESS;
	}
}

@@ -103,16 +103,80 @@
 terial;%0A
+import org.bukkit.block.Block;%0Aimport org.bukkit.entity.Entity;%0A
 import o
@@ -1990,24 +1990,72 @@
 Entity()) %7B%0A
+%09%09%09%09%09%09Entity targetEntity = target.getEntity();%0A
 %09%09%09%09%09%09if (ta
@@ -2058,16 +2058,22 @@
  (target
+Entity
  instanc
@@ -2121,28 +2121,22 @@
 r)target
-.get
 Entity
-()
 ).getNam
@@ -2188,36 +2188,30 @@
 bSkin(target
-.get
 Entity
-()
 .getType());
@@ -2269,28 +2269,22 @@
 = target
-.get
 Entity
-()
 .getType
@@ -2324,24 +2324,147 @@
 %09%09%09%7D else %7B%0A
+%09%09%09%09%09%09Block targetBlock = target.getBlock();%0A%09%09%09%09%09%09if (targetBlock == null) %7B%0A%09%09%09%09%09%09%09return SpellResult.NO_TARGET;%0A%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09player
@@ -2485,35 +2485,29 @@
 kSkin(target
-.get
 Block
-()
 .getType());
@@ -2649,33 +2649,85 @@
 null
- && 
+)%0A%09%09%09%09%7B%0A%09%09%09%09%09if (
 player 
-!
+=
 = null)
-%0A%09%09%09%09%7B
+ %7B%0A%09%09%09%09%09%09return SpellResult.NO_TARGET;%0A%09%09%09%09%09%7D
 %0A%09%09%09

Fix the camera spell not working on players anymore.