package crazypants.enderio.nei;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentData;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.StatCollector;
import codechicken.lib.gui.GuiDraw;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.TemplateRecipeHandler;
import crazypants.enderio.machine.enchanter.EnchanterRecipe;
import crazypants.enderio.machine.enchanter.EnchanterRecipeManager;
import crazypants.enderio.machine.enchanter.GuiEnchanter;
import crazypants.enderio.machine.enchanter.TileEnchanter;
import crazypants.enderio.machine.recipe.RecipeInput;
public class EnchanterRecipeHandler extends TemplateRecipeHandler {
@Override
public String getRecipeName() {
return "Enchanter";
}
@Override
public String getGuiTexture() {
return "enderio:textures/gui/enchanter.png";
}
@Override
public Class<? extends GuiContainer> getGuiClass() {
return GuiEnchanter.class;
}
@Override
public String getOverlayIdentifier() {
return "EIOEnchanter";
}
@Override
public void loadTransferRects() {
transferRects.add(new TemplateRecipeHandler.RecipeTransferRect(new Rectangle(149, -3, 16, 16), "EIOEnchanter", new Object[0]));
}
@Override
public void loadCraftingRecipes(ItemStack result) {
if(result == null || result.getItem() != Items.enchanted_book) {
return;
}
Map<Number, Number> enchants = EnchantmentHelper.getEnchantments(result);
List<EnchanterRecipe> recipes = EnchanterRecipeManager.getInstance().getRecipes();
for (Number id : enchants.keySet()) {
if(id != null && id.intValue() >= 0 && id.intValue() < Enchantment.enchantmentsList.length) {
Enchantment ench = Enchantment.enchantmentsList[id.intValue()];
if(ench != null && ench.getName() != null) {
for (EnchanterRecipe recipe : recipes) {
if(recipe.isValid() && recipe.getEnchantment().getName().equals(ench.getName())) {
EnchantmentData enchantment = new EnchantmentData(recipe.getEnchantment(), 1);
ItemStack output = new ItemStack(Items.enchanted_book);
Items.enchanted_book.addEnchantment(output, enchantment);
EnchanterRecipeNEI rec = new EnchanterRecipeNEI(recipe, output, enchantment);
arecipes.add(rec);
}
}
}
}
}
}
@Override
public void loadCraftingRecipes(String outputId, Object... results) {
if(outputId.equals("EIOEnchanter") && getClass() == EnchanterRecipeHandler.class) {
List<EnchanterRecipe> recipes = EnchanterRecipeManager.getInstance().getRecipes();
for (EnchanterRecipe recipe : recipes) {
if(recipe.isValid()) {
EnchantmentData enchantment = new EnchantmentData(recipe.getEnchantment(), 1);
ItemStack output = new ItemStack(Items.enchanted_book);
Items.enchanted_book.addEnchantment(output, enchantment);
EnchanterRecipeNEI rec = new EnchanterRecipeNEI(recipe, output, enchantment);
arecipes.add(rec);
}
}
} else {
super.loadCraftingRecipes(outputId, results);
}
}
@Override
public void loadUsageRecipes(ItemStack ingredient) {
List<EnchanterRecipe> recipes = EnchanterRecipeManager.getInstance().getRecipes();
for (EnchanterRecipe recipe : recipes) {
if(recipe.isValid()) {
EnchantmentData enchantment = new EnchantmentData(recipe.getEnchantment(), 1);
ItemStack output = new ItemStack(Items.enchanted_book);
Items.enchanted_book.addEnchantment(output, enchantment);
EnchanterRecipeNEI rec = new EnchanterRecipeNEI(recipe, output, enchantment);
if(rec.contains(rec.input, ingredient)) {
rec.setIngredientPermutation(rec.input, ingredient);
arecipes.add(rec);
}
}
}
}
@Override
public void drawExtras(int recipeIndex) {
EnchanterRecipeNEI recipe = (EnchanterRecipeNEI) arecipes.get(recipeIndex);
GuiDraw.drawStringC(recipe.getEnchantName(), 83, 10, 0x808080, false);
int cost = TileEnchanter.getEnchantmentCost(recipe.recipe, 1);
if(cost > 0) {
String s = I18n.format("container.repair.cost", new Object[] { Integer.valueOf(cost) });
GuiDraw.drawStringC(s, 83, 46, 0x80FF20);
}
}
public List<ItemStack> getInputs(RecipeInput input) {
List<ItemStack> result = new ArrayList<ItemStack>();
result.add(input.getInput());
ItemStack[] equivs = input.getEquivelentInputs();
if(equivs != null && equivs.length > 0) {
result.addAll(Arrays.asList(equivs));
}
return result;
}
public class EnchanterRecipeNEI extends TemplateRecipeHandler.CachedRecipe {
private ArrayList<PositionedStack> input;
private PositionedStack output;
private EnchantmentData enchData;
private EnchanterRecipe recipe;
public String getEnchantName() {
return StatCollector.translateToLocal(enchData.enchantmentobj.getName());
}
@Override
public List<PositionedStack> getIngredients() {
return getCycledIngredients(cycleticks / 20, input);
}
@Override
public PositionedStack getResult() {
return output;
}
public EnchanterRecipeNEI(EnchanterRecipe recipe, ItemStack result, EnchantmentData enchData) {
this.recipe = recipe;
input = new ArrayList<PositionedStack>();
input.add(new PositionedStack(new ItemStack(Items.writable_book), 22, 24));
input.add(new PositionedStack(getInputs(recipe.getInput()), 71, 24));
output = new PositionedStack(result, 129, 24);
this.enchData = enchData;
}
}
}