package com.minelittlepony.mixin;

import com.minelittlepony.ducks.IRenderItem;

import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.GlStateManager.DestFactor;
import net.minecraft.client.renderer.GlStateManager.SourceFactor;
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.item.ItemStack;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(RenderItem.class)
public abstract class MixinRenderItem implements IResourceManagerReloadListener, IRenderItem {

    private boolean transparency;

    @Override
    public void useTransparency(boolean transparency) {
        this.transparency = transparency;
    }

    @Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/renderer/block/model/IBakedModel;)V", at = @At("HEAD"))
    private void onRenderItem(ItemStack stack, IBakedModel model, CallbackInfo info) {
        if (transparency) {
            GlStateManager.tryBlendFuncSeparate(SourceFactor.CONSTANT_COLOR, DestFactor.ONE, SourceFactor.ONE, DestFactor.ZERO);
        }
    }

    @ModifyArg(method = "renderQuads(Lnet/minecraft/client/renderer/BufferBuilder;ILjava/util/List;Lnet/minecraft/item/Itemstack;)V",
            at = @At(value = "INVOKE",
                    target = "renderQuad(Lnet/minecraft/client/renderer/BufferBuilder;Lnet/minecraft/client/renderer/block/model/BakedQuad;I)V"),
            index = 2)
    private int modifyItemRenderTint(int color) {
        return transparency ? -1 : color;
    }

    @Inject(method = "renderEffect(Lnet/minecraft/client/renderer/block/model/IBakedQuad;)V", at = @At("HEAD"), cancellable = true)
    private void renderEffect(IBakedModel model, CallbackInfo info) {
        if (transparency) {
            info.cancel();
        }
    }
}

@@ -1504,17 +1504,16 @@
 Builder;
-I
 Ljava/ut
@@ -1520,16 +1520,17 @@
 il/List;
+I
 Lnet/min
@@ -1545,17 +1545,17 @@
 tem/Item
-s
+S
 tack;)V%22
@@ -1621,24 +1621,66 @@
   target = %22
+Lnet/minecraft/client/renderer/RenderItem;
 renderQuad(L
@@ -1989,20 +1989,21 @@
 l/IBaked
-Quad
+Model
 ;)V%22, at

Fix build errors.