package crazypants.enderio.base.material.glass;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;

import org.apache.commons.lang3.tuple.Pair;

import com.enderio.core.common.fluid.SmartTank;

import crazypants.enderio.base.render.ICacheKey;
import crazypants.enderio.base.render.IRenderMapper;
import crazypants.enderio.base.render.property.EnumMergingBlockRenderMode;
import crazypants.enderio.base.render.util.ItemQuadCollector;
import crazypants.enderio.base.render.util.TankRenderHelper;
import crazypants.enderio.base.render.util.HalfBakedQuad.HalfBakedList;
import crazypants.enderio.util.NbtValue;
import info.loenwind.autosave.helpers.SmartTankItemAccess;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import static crazypants.enderio.base.render.property.EnumMergingBlockRenderMode.RENDER;

public class FusedQuartzItemRenderMapper implements IRenderMapper.IItemRenderMapper.IItemStateMapper {

  public static final @Nonnull FusedQuartzItemRenderMapper instance = new FusedQuartzItemRenderMapper();

  protected FusedQuartzItemRenderMapper() {
  }

  @Override
  @SideOnly(Side.CLIENT)
  public List<Pair<IBlockState, ItemStack>> mapItemRender(@Nonnull Block block, @Nonnull ItemStack stack, @Nonnull ItemQuadCollector itemQuadCollector) {

    if (NbtValue.FAKE.hasTag(stack)) {
      // this is for the TOP overlay, kind of a hack putting it here, but the alternative would be adding a new item just for this...
      SmartTank tank = SmartTankItemAccess.getTank(stack);
      HalfBakedList buffer = TankRenderHelper.mkTank(tank, 0, 0, 16, true);
      if (buffer != null) {
        List<BakedQuad> quads = new ArrayList<BakedQuad>();
        buffer.bake(quads);
        itemQuadCollector.addQuads(null, quads);
      }
      return null;
    }

    List<Pair<IBlockState, ItemStack>> states = new ArrayList<Pair<IBlockState, ItemStack>>();
    IBlockState defaultState = block.getDefaultState();
    FusedQuartzType bankType = FusedQuartzType.getTypeFromMeta(stack.getItemDamage());
    defaultState = defaultState.withProperty(FusedQuartzType.KIND, bankType);

    states.add(Pair.of(defaultState.withProperty(RENDER, EnumMergingBlockRenderMode.sides), (ItemStack) null));

    for (EnumFacing facing : EnumFacing.Plane.HORIZONTAL) {
      states.add(Pair.of(defaultState.withProperty(RENDER, EnumMergingBlockRenderMode.get(facing, EnumFacing.UP)), (ItemStack) null));
      states.add(Pair.of(defaultState.withProperty(RENDER, EnumMergingBlockRenderMode.get(facing, EnumFacing.DOWN)), (ItemStack) null));
      states.add(Pair.of(defaultState.withProperty(RENDER, EnumMergingBlockRenderMode.get(facing, facing.rotateYCCW())), (ItemStack) null));
      states.add(Pair.of(defaultState.withProperty(RENDER, EnumMergingBlockRenderMode.get(facing, facing.rotateYCCW(), EnumFacing.UP)), (ItemStack) null));
      states.add(Pair.of(defaultState.withProperty(RENDER, EnumMergingBlockRenderMode.get(facing, facing.rotateYCCW(), EnumFacing.DOWN)), (ItemStack) null));
    }
    return states;
  }

  @Override
  @SideOnly(Side.CLIENT)
  public @Nonnull ICacheKey getCacheKey(@Nonnull Block block, @Nonnull ItemStack stack, @Nonnull ICacheKey cacheKey) {
    return cacheKey;
  }

}

@@ -222,16 +222,69 @@
 tTank;%0A%0A
+import crazypants.enderio.base.fluid.ItemTankHelper;%0A
 import c
@@ -373,24 +373,95 @@
 nderMapper;%0A
+import crazypants.enderio.base.render.IRenderMapper.IItemRenderMapper;%0A
 import crazy
@@ -519,24 +519,96 @@
 RenderMode;%0A
+import crazypants.enderio.base.render.util.HalfBakedQuad.HalfBakedList;%0A
 import crazy
@@ -748,152 +748,21 @@
 rio.
-base.render.util.HalfBakedQuad.HalfBakedList;%0Aimport crazypants.enderio.util.NbtValue;%0Aimport info.loenwind.autosave.helpers.SmartTankItemAccess
+util.NbtValue
 ;%0Aim
@@ -1214,16 +1214,57 @@
 plements
+ IItemRenderMapper.IDynamicOverlayMapper,
  IRender
@@ -1698,450 +1698,8 @@
 ) %7B%0A
-      // this is for the TOP overlay, kind of a hack putting it here, but the alternative would be adding a new item just for this...%0A      SmartTank tank = SmartTankItemAccess.getTank(stack);%0A      HalfBakedList buffer = TankRenderHelper.mkTank(tank, 0, 0, 16, true);%0A      if (buffer != null) %7B%0A        List%3CBakedQuad%3E quads = new ArrayList%3CBakedQuad%3E();%0A        buffer.bake(quads);%0A        itemQuadCollector.addQuads(null, quads);%0A      %7D%0A
     
@@ -2963,24 +2963,742 @@
 tates;%0A  %7D%0A%0A
+  @Override%0A  @SideOnly(Side.CLIENT)%0A  public ItemQuadCollector mapItemDynamicOverlayRender(@Nonnull Block block, @Nonnull ItemStack stack) %7B%0A    if (NbtValue.FAKE.hasTag(stack)) %7B%0A      // this is for the TOP overlay, kind of a hack putting it here, but the alternative would be adding a new item just for this...%0A      SmartTank tank = ItemTankHelper.getTank(stack);%0A      HalfBakedList buffer = TankRenderHelper.mkTank(tank, 0, 0, 16, true);%0A      if (buffer != null) %7B%0A        ItemQuadCollector result = new ItemQuadCollector();%0A        List%3CBakedQuad%3E quads = new ArrayList%3CBakedQuad%3E();%0A        buffer.bake(quads);%0A        result.addQuads(null, quads);%0A        return result;%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A
   @Override%0A
@@ -3837,24 +3837,109 @@
 cacheKey) %7B%0A
+    if (NbtValue.FAKE.hasTag(stack)) %7B%0A      cacheKey.addCacheKey(0x7FF71337);%0A    %7D%0A
     return c

Fix fluid overlay being baked into item model