package com.zivilon.cinder_loe.blocks;

import com.zivilon.cinder_loe.util.Utilities;
import com.zivilon.cinder_loe.client.render.block.RenderIceCage;

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.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;

import java.util.List;
import java.util.Random;

public class IceCage extends Block {
    @SideOnly(Side.CLIENT)
    private IIcon blockIcon;
    @SideOnly(Side.CLIENT)
    private IIcon emptyIcon;

    public IceCage() {
        super(Material.ice);
        setHardness(2.0F);
        setResistance(1.0F);
        setStepSound(soundTypeGlass);
        setBlockTextureName(Utilities.toSnakeCase("lotr:iceCage"));
        setBlockName(Utilities.toSnakeCase("lotr:iceCage"));
        setLightOpacity(5); // Semi-transparent
        setCreativeTab(CreativeTabs.tabBlock);
    }

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

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

    @Override
    public int getRenderType() {
        return RenderIceCage.RENDER_ID; // Use the custom render type
    }

    @Override
    @SideOnly(Side.CLIENT)
    public int getRenderBlockPass() {
        return 1;
    }

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

    @Override
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(int side, int meta) {
        if ((meta == 0 && side == 1) || (meta == 1 && side == 0)) {
            return this.emptyIcon;
        }
        return this.blockIcon;
    }

    @Override
    public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z) {
        return AxisAlignedBB.getBoundingBox(x, y, z, x + 1.0F, y + 1.0F, z + 1.0F);
    }

    @Override
    public void addCollisionBoxesToList(World world, int x, int y, int z, AxisAlignedBB aabb, List list, Entity entity) {
        int meta = world.getBlockMetadata(x, y, z);
        if (meta == 0) { // Lower part
            float thickness = 0.0625F; // 1/16th of a block thickness

            AxisAlignedBB[] boundingBoxes = new AxisAlignedBB[]{
                    AxisAlignedBB.getBoundingBox(x, y, z, x + thickness, y + 1.0F, z + 1.0F), // Left wall
                    AxisAlignedBB.getBoundingBox(x + 1.0F - thickness, y, z, x + 1.0F, y + 1.0F, z + 1.0F), // Right wall
                    AxisAlignedBB.getBoundingBox(x, y, z, x + 1.0F, y + 1.0F, z + thickness), // Front wall
                    AxisAlignedBB.getBoundingBox(x, y, z + 1.0F - thickness, x + 1.0F, y + 1.0F, z + 1.0F) // Back wall
            };

            for (AxisAlignedBB boundingBox : boundingBoxes) {
                if (boundingBox != null && aabb.intersectsWith(boundingBox)) {
                    list.add(boundingBox);
                }
            }
        } else if (meta == 1) { // Upper partplayer.addPotionEffect(new PotionEffect(Potion.digSlowdown.id, 20, 2, true)); // Mining Fatigue III
            float thickness = 0.0625F; // 1/16th of a block thickness

            AxisAlignedBB[] boundingBoxes = new AxisAlignedBB[]{
                    AxisAlignedBB.getBoundingBox(x, y, z, x + thickness, y + 1.0F, z + 1.0F), // Left wall
                    AxisAlignedBB.getBoundingBox(x + 1.0F - thickness, y, z, x + 1.0F, y + 1.0F, z + 1.0F), // Right wall
                    AxisAlignedBB.getBoundingBox(x, y, z, x + 1.0F, y + 1.0F, z + thickness), // Front wall
                    AxisAlignedBB.getBoundingBox(x, y, z + 1.0F - thickness, x + 1.0F, y + 1.0F, z + 1.0F), // Back wall
                    AxisAlignedBB.getBoundingBox(x, y + 1.0F - thickness, z, x + 1.0F, y + 1.0F, z + 1.0F) // Ceiling
            };

            for (AxisAlignedBB boundingBox : boundingBoxes) {
                if (boundingBox != null && aabb.intersectsWith(boundingBox)) {
                    list.add(boundingBox);
                }
            }
        }
    }

    @Override
    public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity) {
        if (entity instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) entity;
            AxisAlignedBB blockBounds = AxisAlignedBB.getBoundingBox(x, y, z, x + 1.0F, y + 1.0F, z + 1.0F);
            if (blockBounds != null && blockBounds.intersectsWith(player.boundingBox)) {
                player.motionX = 0;
                player.motionY = 0;
                player.motionZ = 0;
                player.addPotionEffect(new PotionEffect(Potion.digSlowdown.id, 20, 2, true)); // Mining Fatigue III
            }
        }
    }

    @Override
    public void updateTick(World world, int x, int y, int z, Random random) {
        int meta = world.getBlockMetadata(x, y, z);
        if (meta == 0) { // Lower part
            List<EntityPlayer> players = world.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(x, y, z, x + 1.0F, y + 2.0F, z + 1.0F));
            for (EntityPlayer player : players) {
                player.motionX = 0;
                player.motionY = 0;
                player.motionZ = 0;
                player.addPotionEffect(new PotionEffect(Potion.digSlowdown.id, 20, 2, true)); // Mining Fatigue III
            }
        }
        world.scheduleBlockUpdate(x, y, z, this, this.tickRate(world));
    }

    @Override
    public int tickRate(World world) {
        return 1; // Adjust the tick rate as needed
    }

    @Override
    @SideOnly(Side.CLIENT)
    public boolean shouldSideBeRendered(IBlockAccess world, int x, int y, int z, int side) {
        return true;
    }

    @Override
    public boolean canPlaceBlockAt(World world, int x, int y, int z) {
        return world.isAirBlock(x, y, z) && world.isAirBlock(x, y + 1, z);
    }

    @Override
    public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase placer, ItemStack itemStack) {
        world.setBlockMetadataWithNotify(x, y, z, 0, 2); // Lower part
        world.setBlock(x, y + 1, z, this, 1, 2); // Upper part
    }

    @Override
    public void onNeighborBlockChange(World world, int x, int y, int z, Block neighbor) {
        int meta = world.getBlockMetadata(x, y, z);
        if (meta == 0) { // Lower part
            if (world.getBlock(x, y + 1, z) != this) {
                world.setBlockToAir(x, y, z);
            }
        } else if (meta == 1) { // Upper part
            if (world.getBlock(x, y - 1, z) != this) {
                world.setBlockToAir(x, y, z);
            }
        }
    }

    @Override
    public void breakBlock(World world, int x, int y, int z, Block block, int meta) {
        if (meta == 0) { // Lower part
            if (world.getBlock(x, y + 1, z) == this) {
                world.setBlockToAir(x, y + 1, z);
            }
        } else if (meta == 1) { // Upper part
            if (world.getBlock(x, y - 1, z) == this) {
                world.setBlockToAir(x, y - 1, z);
            }
        }
        super.breakBlock(world, x, y, z, block, meta);
    }
}
