package com.elmakers.mine.bukkit.entity;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Horse;
import org.bukkit.inventory.ItemStack;

import com.elmakers.mine.bukkit.api.magic.MageController;

public class EntityHorseData extends EntityExtraData {
    public Horse.Color color;
    public Horse.Style style;
    public ItemStack saddle;
    public ItemStack armor;
    public Integer domestication;
    public Integer maxDomestication;
    public Double jumpStrength;
    public Boolean tamed;

    public EntityHorseData() {

    }

    public EntityHorseData(ConfigurationSection parameters, MageController controller) {
        Logger log = controller.getLogger();
        if (parameters.contains("horse_color")) {
            try {
                String colorString = parameters.getString("horse_color");
                color = Horse.Color.valueOf(colorString.toUpperCase());
            } catch (Exception ex) {
                log.log(Level.WARNING, "Invalid horse_color: " + parameters.getString("horse_color"), ex);
            }
        }

        if (parameters.contains("horse_style")) {
            try {
                String styleString = parameters.getString("horse_style");
                style = Horse.Style.valueOf(styleString.toUpperCase());
            } catch (Exception ex) {
                log.log(Level.WARNING, "Invalid horse_style: " + parameters.getString("horse_style"), ex);
            }
        }

        if (parameters.contains("horse_jump_strength")) {
            jumpStrength = parameters.getDouble("horse_jump_strength");
        }

        if (parameters.contains("tamed")) {
            tamed = parameters.getBoolean("tamed");
        }
    }

    public EntityHorseData(Horse horse) {
        color = horse.getColor();
        style = horse.getStyle();
        saddle = horse.getInventory().getSaddle();
        armor = horse.getInventory().getArmor();
        domestication = horse.getDomestication();
        maxDomestication = horse.getMaxDomestication();
        jumpStrength = horse.getJumpStrength();
        tamed = horse.isTamed();
    }

    @Override
    public void apply(Entity entity) {
        if (!(entity instanceof Horse)) return;
        Horse horse = (Horse)entity;

        horse.getInventory().setSaddle(saddle);
        horse.getInventory().setArmor(armor);
        if (color != null) {
            horse.setColor(color);
        }
        if (style != null) {
            horse.setStyle(style);
        }
        if (domestication != null) {
            horse.setDomestication(domestication);
        }
        if (maxDomestication != null) {
            horse.setMaxDomestication(maxDomestication);
        }
        if (jumpStrength != null) {
            horse.setJumpStrength(jumpStrength);
        }
        if (tamed != null) {
            horse.setTamed(tamed);
        }
    }

    @Override
    public EntityExtraData clone() {
        EntityHorseData copy = new EntityHorseData();
        copy.saddle = saddle == null ? null : saddle.clone();
        copy.armor = armor == null ? null : armor.clone();
        copy.color = color;
        copy.domestication = domestication;
        copy.style = style;
        copy.maxDomestication = maxDomestication;
        copy.jumpStrength = jumpStrength;
        copy.tamed = tamed;
        return copy;
    }

    @Override
    public void removed(Entity entity) {
    }
}

@@ -101,16 +101,51 @@
 ogger;%0A%0A
+import javax.annotation.Nullable;%0A%0A
 import o
@@ -295,16 +295,67 @@
 Stack;%0A%0A
+import com.elmakers.mine.bukkit.api.item.ItemData;%0A
 import c
@@ -535,21 +535,20 @@
 lic Item
-Stack
+Data
  saddle;
@@ -563,21 +563,20 @@
 lic Item
-Stack
+Data
  armor;%0A
@@ -1882,32 +1882,185 @@
 med%22);%0A        %7D
+%0A%0A        saddle = controller.getOrCreateItem(parameters.getString(%22saddle%22));%0A        armor = controller.getOrCreateItem(parameters.getString(%22armor%22));
 %0A    %7D%0A%0A    publ
@@ -2174,24 +2174,32 @@
    saddle = 
+getItem(
 horse.getInv
@@ -2214,24 +2214,25 @@
 .getSaddle()
+)
 ;%0A        ar
@@ -2233,24 +2233,32 @@
     armor = 
+getItem(
 horse.getInv
@@ -2272,24 +2272,25 @@
 ).getArmor()
+)
 ;%0A        do
@@ -2465,32 +2465,187 @@
 Tamed();%0A    %7D%0A%0A
+    @Nullable%0A    private ItemData getItem(ItemStack item) %7B%0A        return item == null ? null : new com.elmakers.mine.bukkit.item.ItemData(item);%0A    %7D%0A%0A
     @Override%0A  
@@ -2812,16 +2812,56 @@
 e(saddle
+ == null ? null : saddle.getItemStack(1)
 );%0A     
@@ -2898,16 +2898,55 @@
 or(armor
+ == null ? null : armor.getItemStack(1)
 );%0A     
@@ -3606,99 +3606,36 @@
 ddle
- == null ? null : saddle.clone();%0A        copy.armor = armor == null ? null : armor.clone()
+;%0A        copy.armor = armor
 ;%0A  

Add "saddle" and "armor" mob options for horses