package com.elmakers.mine.bukkit.magicworlds.spawn.builtin;

import com.elmakers.mine.bukkit.magicworlds.MagicWorldsController;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Horse.Variant;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Ocelot.Type;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Skeleton.SkeletonType;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.Plugin;

public class ReplaceRule extends EquipmentRule {
    protected EntityType 		replaceWith;
    protected String			entitySubType;
    protected boolean			docile;

    @Override
    public boolean load(String key, ConfigurationSection parameters, MagicWorldsController controller)
    {
    	if (!super.load(key, parameters, controller)) return false;
    	String entityName = parameters.getString("replace_type");
    	entitySubType = parameters.getString("replace_sub_type");

    	replaceWith = parseEntityType(entityName);
		if (replaceWith == null) {
			this.controller.getLogger().warning(" Invalid entity type: " + entityName);
			return false;
		}
    	
		String subTypeDescription = entitySubType == null || entitySubType.length() == 0 ? "" : ":" + entitySubType;
    	controller.getLogger().info(" Replacing: " + targetEntityType.name() + " at y > " + minY
				+ " with " + replaceWith.name() + subTypeDescription + " with a " + (percentChance * 100) + "% chance");
    	return true;
    }
    
    public EntityType getReplaceWith()
    {
        return replaceWith;
    }
    
    @Override
    public LivingEntity onProcess(Plugin plugin, LivingEntity entity) {
        Entity result = entity.getWorld().spawnEntity(entity.getLocation(), replaceWith);
        result.setMetadata("docile", new FixedMetadataValue(plugin, true));
        
        if (entitySubType != null && entitySubType.length() > 0) {
   			try {
	           	switch (replaceWith) {
	        	case HORSE:
	        		if (result instanceof Horse) {
	        			Horse horse = (Horse)result;
	        			try {
	        				horse.setVariant(Variant.valueOf(entitySubType.toUpperCase()));
	        			} catch (Throwable ex) {
	        				ex.printStackTrace();
	        			}
	        		}
	        		break;
		        case SKELETON:
		    		if (result instanceof Skeleton) {
		    			Skeleton skeleton = (Skeleton)result;
		    			skeleton.setSkeletonType(SkeletonType.valueOf(entitySubType.toUpperCase()));
		    		}
		    		break;
		        case OCELOT:
		    		if (result instanceof Ocelot) {
		    			Ocelot ocelot = (Ocelot)result;
		    			ocelot.setCatType(Type.valueOf(entitySubType.toUpperCase()));
		    		}
		    		break;
		    	default:
		    	}
			} catch (Throwable ex) {
			}
        }
		if (!(result instanceof LivingEntity)) {
			return null;
		}
		LivingEntity li = (LivingEntity)result;
		setEquipment(li);
        return li;
    }
}

@@ -514,55 +514,8 @@
 pe;%0A
-import org.bukkit.metadata.FixedMetadataValue;%0A
 impo
@@ -543,16 +543,16 @@
 Plugin;%0A
+
 %0Apublic 
@@ -702,16 +702,72 @@
 %09docile;
+%0A%09protected double %09%09%09health;%0A%09protected String %09%09%09name;
 %0A%0A    @O
@@ -1073,16 +1073,101 @@
 _type%22);
+%0A%09%09name = parameters.getString(%22name%22);%0A%09%09health = parameters.getDouble(%22health%22, 0);
 %0A%0A    %09r
@@ -1950,93 +1950,8 @@
 h);%0A
-        result.setMetadata(%22docile%22, new FixedMetadataValue(plugin, true));%0A        %0A
     
@@ -2976,16 +2976,16 @@
 result;%0A
-
 %09%09setEqu
@@ -2995,16 +2995,200 @@
 ent(li);
+%0A%0A%09%09if (health %3E 0) %7B%0A%09%09%09li.setMaxHealth(health);%0A%09%09%09li.setHealth(health);%0A%09%09%7D%0A%09%09if (name != null && !name.isEmpty()) %7B%0A%09%09%09li.setCustomName(name);%0A%09%09%09li.setCustomNameVisible(true);%0A%09%09%7D
 %0A       

Add health and custom name options for replaced mobs