package com.zivilon.cinder_loe.blocks;

import com.zivilon.cinder_loe.util.Utilities;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.util.IIcon;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import java.lang.reflect.Field;
import java.util.List;
import net.minecraft.client.Minecraft;
import cpw.mods.fml.common.FMLCommonHandler;

public class EntityBarrier extends Block {
    @SideOnly(Side.CLIENT)
    public IIcon transparentIcon;

    public EntityBarrier() {
        super(Material.rock);
        setHardness(Float.MAX_VALUE);
        setResistance(Float.MAX_VALUE);
        setBlockTextureName(Utilities.toSnakeCase("lotr:entityBarrier"));
        setBlockName(Utilities.toSnakeCase("lotr:entityBarrier"));
    }

    @Override
    public boolean isOpaqueCube() {
        return false;
    }
    @Override
    public boolean renderAsNormalBlock() {
        return false;
    }

    @Override
    public int getRenderType() {
        return -1;
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void registerBlockIcons(IIconRegister iconRegister) {
        this.transparentIcon = iconRegister.registerIcon("lotr:invisible");
    }

    @Override
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(int side, int meta) {
        return this.transparentIcon;
    }

    @SideOnly(Side.CLIENT)
    private boolean isPlayerInSurvivalModeClientSide() {
        EntityPlayer player = Minecraft.getMinecraft().thePlayer;
        return player != null && !player.capabilities.isCreativeMode;
    }

    @Override
    public void setBlockBoundsBasedOnState(IBlockAccess world, int x, int y, int z) {
        if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT && isPlayerInSurvivalModeClientSide()) {
            this.setBlockBounds(0.5F, 1.0F, 0.5F, 0.5F, 0.0F, 0.5F); // Zero-sized bounding box for survival players
        } else {
            this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F); // Full-sized bounding box for others
        }
    }

    @Override
    public void addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB aabb, List list, Entity entity) {
        if (entity instanceof EntityPlayer) {
            return;
        }
        AxisAlignedBB boundingBox = AxisAlignedBB.getBoundingBox(x, y, z, x + 1.0F, y + 1.0F, z + 1.0F);
        if (boundingBox != null && aabb.intersectsWith(boundingBox)) {
            list.add(boundingBox);
        }
    }
}
