/*
 * This file is part of GoodTime-Industrial, licensed under MIT License (MIT).
 *
 * Copyright (c) 2015 GoodTime Studio <https://github.com/GoodTimeStudio>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mcgoodtime.gti.common.nei.handler;

import codechicken.nei.NEIServerUtils;
import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.TemplateRecipeHandler;
import com.mcgoodtime.gti.common.core.Gti;
import com.mcgoodtime.gti.common.recipes.IProcessable;
import com.mcgoodtime.gti.common.recipes.RecipePart;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.StatCollector;

import java.awt.*;
import java.util.Collections;
import java.util.List;

/**
 * Created by BestOwl on 2015.12.19.0019.
 *
 * @author BestOwl
 */
public abstract class RecipeHandlerBase extends TemplateRecipeHandler {

    public abstract IProcessable getRecipesList();

    public abstract String getRecipeNameForCrafting();

    public abstract String getRecipeID();

    @Override
    public abstract Class<? extends GuiContainer> getGuiClass();

    @Override
    public void loadCraftingRecipes(String outputId, Object... results) {
        System.out.println(outputId);
        if (outputId.equals(this.getRecipeID())) {
            for (RecipePart recipes : this.getRecipesList().getProcessRecipesList()) {
                this.arecipes.add(new CachedRecipePart(recipes.input, recipes.output));
            }
        } else {
            super.loadCraftingRecipes(outputId, results);
        }
    }

    @Override
    public void loadCraftingRecipes(ItemStack result) {
        for (RecipePart recipes : this.getRecipesList().getProcessRecipesList()) {
            if(NEIServerUtils.areStacksSameTypeCrafting(recipes.output, result)) {
                this.arecipes.add(new CachedRecipePart(recipes.input, recipes.output));
            }
        }
    }

    @Override
    public void loadUsageRecipes(ItemStack ingredient) {
        for (RecipePart recipes : this.getRecipesList().getProcessRecipesList()) {
            if (recipes.input.isItemEqual(ingredient)) {
                this.arecipes.add(new CachedRecipePart(recipes.input, recipes.output));
            }
        }
    }

    @Override
    public abstract String getOverlayIdentifier();

    @Override
    public void loadTransferRects() {
        this.transferRects.add(new RecipeTransferRect(new Rectangle(74, 23, 24, 18), this.getRecipeID()));
    }

    @Override
    public final String getRecipeName() {
        return StatCollector.translateToLocal(Gti.GUI_PREFIX + this.getRecipeNameForCrafting());
    }

    @Override
    public void drawExtras(int recipe) {
        this.drawProgressBar(51, 25, 176, 0, 14, 14, 48, 3);
        this.drawProgressBar(74, 23, 176, 14, 24, 16, 48, 0);
    }

    public class CachedRecipePart extends CachedRecipe {
        PositionedStack input;
        PositionedStack result;

        public CachedRecipePart(ItemStack input, ItemStack result) {
            super();
            this.input = new PositionedStack(input, 51, 5);
            this.result = new PositionedStack(result, 111, 24);
        }

        @Override
        public List<PositionedStack> getIngredients() {
            return this.getCycledIngredients(RecipeHandlerBase.this.cycleticks / 48, Collections.singletonList(this.input));
        }

        @Override
        public PositionedStack getResult() {
            return this.result;
        }

        @Override
        public PositionedStack getOtherStack() {
            return null;
        }
    }
}

@@ -3892,24 +3892,201 @@
  0);%0A    %7D%0A%0A
+    protected Position getInputPosition() %7B%0A        return new Position(51, 5);%0A    %7D%0A%0A    protected Position getOutputPosition() %7B%0A        return new Position(111, 24);%0A    %7D%0A%0A
     public c
@@ -4130,32 +4130,42 @@
 ecipe %7B%0A        
+protected 
 PositionedStack 
@@ -4179,16 +4179,26 @@
         
+protected 
 Position
@@ -4320,53 +4320,234 @@
     
-this.input = new PositionedStack(input, 51, 5
+Position position;%0A            position = RecipeHandlerBase.this.getInputPosition();%0A            this.input = new PositionedStack(input, position.x, position.y);%0A            position = RecipeHandlerBase.this.getOutputPosition(
 );%0A 
@@ -4599,23 +4599,38 @@
 result, 
-111, 24
+position.x, position.y
 );%0A     
@@ -5058,11 +5058,196 @@
  %7D%0A    %7D
+%0A%0A    public static class Position %7B%0A        public int x;%0A        public int y;%0A%0A        public Position(int x, int y) %7B%0A            this.x = x;%0A            this.y = y;%0A        %7D%0A    %7D
 %0A%7D%0A

Improve RecipeHandlerBase