package com.gmail.nossr50.skills.repair;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.material.MaterialData;

import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.ItemUtils;

public class Repair {
    public static int    repairMasteryMaxBonusLevel = AdvancedConfig.getInstance().getRepairMasteryMaxLevel();
    public static double repairMasteryMaxBonus      = AdvancedConfig.getInstance().getRepairMasteryMaxBonus();

    public static int    superRepairMaxBonusLevel = AdvancedConfig.getInstance().getSuperRepairMaxLevel();
    public static double superRepairMaxChance     = AdvancedConfig.getInstance().getSuperRepairChanceMax();

    public static int salvageUnlockLevel = AdvancedConfig.getInstance().getSalvageUnlockLevel();

    public static Material salvageAnvilMaterial = Config.getInstance().getSalvageAnvilMaterial();
    public static Material repairAnvilMaterial  = Config.getInstance().getRepairAnvilMaterial();
    public static boolean  anvilMessagesEnabled = Config.getInstance().getRepairAnvilMessagesEnabled();

    /**
     * Checks if the item is salvageable.
     *
     * @param item Item to check
     *
     * @return true if the item is salvageable, false otherwise
     */
    public static boolean isSalvageable(ItemStack item) {
        if (Config.getInstance().getSalvageTools() && ItemUtils.isMinecraftTool(item)) {
            return true;
        }

        if (Config.getInstance().getSalvageArmor() && !ItemUtils.isChainmailArmor(item) && ItemUtils.isMinecraftArmor(item)) {
            return true;
        }

        return false;
    }

    public static String getAnvilMessage(Material type) {
        if (type == repairAnvilMaterial) {
            return LocaleLoader.getString("Repair.Listener.Anvil");
        }

        if (type == salvageAnvilMaterial) {
            return LocaleLoader.getString("Repair.Listener.Anvil2");
        }

        return "";
    }

    protected static Material getRepairAndSalvageItem(ItemStack inHand) {
        if (ItemUtils.isDiamondTool(inHand) || ItemUtils.isDiamondArmor(inHand)) {
            return Material.DIAMOND;
        }
        else if (ItemUtils.isGoldTool(inHand) || ItemUtils.isGoldArmor(inHand)) {
            return Material.GOLD_INGOT;
        }
        else if (ItemUtils.isIronTool(inHand) || ItemUtils.isIronArmor(inHand)) {
            return Material.IRON_INGOT;
        }
        else if (ItemUtils.isStoneTool(inHand)) {
            return Material.COBBLESTONE;
        }
        else if (ItemUtils.isWoodTool(inHand)) {
            return Material.WOOD;
        }
        else if (ItemUtils.isLeatherArmor(inHand)) {
            return Material.LEATHER;
        }
        else if (ItemUtils.isStringTool(inHand)) {
            return Material.STRING;
        }
        else {
            return null;
        }
    }

    public static int getRepairAndSalvageQuantities(ItemStack item) {
        return getRepairAndSalvageQuantities(item, getRepairAndSalvageItem(item), (byte) -1);
    }

    public static int getRepairAndSalvageQuantities(ItemStack item, Material repairMaterial, byte repairMetadata) {
        int quantity = 0;
        MaterialData repairData = repairMaterial != null ? new MaterialData(repairMaterial, repairMetadata) : null;
        Recipe recipe = mcMMO.p.getServer().getRecipesFor(item).get(0);

        if (recipe instanceof ShapelessRecipe) {
            for (ItemStack ingredient : ((ShapelessRecipe) recipe).getIngredientList()) {
                if (ingredient != null && (repairMaterial == null || ingredient.getType() == repairMaterial) && (repairMetadata == -1 || ingredient.getData().equals(repairData))) {
                    quantity += ingredient.getAmount();
                }
            }
        }
        else if (recipe instanceof ShapedRecipe) {
            for (ItemStack ingredient : ((ShapedRecipe) recipe).getIngredientMap().values()) {
                if (ingredient != null && (repairMaterial == null || ingredient.getType() == repairMaterial) && (repairMetadata == -1 || ingredient.getData().equals(repairData))) {
                    quantity += ingredient.getAmount();
                }
            }
        }

        return quantity;
    }
}

@@ -35,16 +35,40 @@
 epair;%0A%0A
+import java.util.List;%0A%0A
 import o
@@ -3589,21 +3589,28 @@
     
+List%3C
 Recipe
+%3E
  recipe
+s
  = m
@@ -3647,16 +3647,88 @@
 or(item)
+;%0A%0A        if (!recipes.isEmpty()) %7B%0A            Recipe recipe = recipes
 .get(0);
@@ -3725,24 +3725,28 @@
 es.get(0);%0A%0A
+    
         if (
@@ -3774,32 +3774,36 @@
 pelessRecipe) %7B%0A
+    
             for 
@@ -3872,24 +3872,28 @@
 ntList()) %7B%0A
+    
             
@@ -4073,32 +4073,36 @@
                 
+    
 quantity += ingr
@@ -4129,34 +4129,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -4165,34 +4165,40 @@
   %7D%0A        
-%7D%0A
+    %7D%0A  
         
 else if (rec
@@ -4177,32 +4177,34 @@
     %7D%0A          
+  
 else if (recipe 
@@ -4222,32 +4222,36 @@
 ShapedRecipe) %7B%0A
+    
             for 
@@ -4325,24 +4325,28 @@
 values()) %7B%0A
+    
             
@@ -4526,32 +4526,36 @@
                 
+    
 quantity += ingr
@@ -4566,32 +4566,54 @@
 nt.getAmount();%0A
+                    %7D%0A
                 

Allow repair of items without a recipe.