package com.zivilon.cinder_loe.blocks;

import com.zivilon.cinder_loe.LoECreativeTabs;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import lotr.common.block.LOTRBlockOrcChain;
import net.minecraft.block.*;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

import java.util.List;

public class CinderChain extends LOTRBlockOrcChain {

    public static final String[] chainTypes = {"bronze", "gold", "silver", "iron"};
    @SideOnly(value= Side.CLIENT)
    private IIcon[] iconMiddle;
    @SideOnly(value=Side.CLIENT)
    private IIcon[] iconTop;
    @SideOnly(value=Side.CLIENT)
    private IIcon[] iconBottom;
    @SideOnly(value=Side.CLIENT)
    private IIcon[] iconSingle;
    @SideOnly(Side.CLIENT)
    private IIcon[] iconItem;


    public CinderChain() {
        this.setCreativeTab(LoECreativeTabs.tabDecoLoE);
        this.setHardness(1.0f);
        this.setStepSound(Block.soundTypeMetal);
        setBlockName("lotr:cinderchain");
        setBlockTextureName("lotr:cinderchain");
        float f = 0.2f;
        this.setBlockBounds(0.5f - f, 0.0f, 0.5f - f, 0.5f + f, 1.0f, 0.5f + f);
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void registerBlockIcons(IIconRegister iconRegister) {

        this.iconItem = new IIcon[chainTypes.length];

        for (int i = 0; i < chainTypes.length; ++i) {
            this.iconItem[i] = iconRegister.registerIcon(this.getTextureName() + "_" + chainTypes[i] + "_item");
        }

        this.iconMiddle = new IIcon[chainTypes.length];
        this.iconTop = new IIcon[chainTypes.length];
        this.iconBottom = new IIcon[chainTypes.length];
        this.iconSingle = new IIcon[chainTypes.length];

        for (int i = 0; i < chainTypes.length; ++i) {
            this.iconMiddle[i] = iconRegister.registerIcon(this.getTextureName() + "_" + chainTypes[i] + "_mid");
            this.iconTop[i] = iconRegister.registerIcon(this.getTextureName() + "_" + chainTypes[i] + "_top");
            this.iconBottom[i] = iconRegister.registerIcon(this.getTextureName() + "_" + chainTypes[i] + "_bottom");
            this.iconSingle[i] = iconRegister.registerIcon(this.getTextureName() + "_" + chainTypes[i] + "_single");
        }
    }
    @SideOnly(Side.CLIENT)
    @Override
    public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) {
        Block above = world.getBlock(x, y + 1, z);
        Block below = world.getBlock(x, y - 1, z);
        boolean chainAbove = above instanceof CinderChain;
        boolean chainBelow = below instanceof CinderChain;

        int meta = world.getBlockMetadata(x, y, z);
        if (chainAbove && chainBelow) {
            return this.iconMiddle[meta];
        }
        if (chainAbove) {
            return this.iconBottom[meta];
        }
        if (chainBelow) {
            return this.iconTop[meta];
        }
        return this.iconSingle[meta];
    }

    @SideOnly(value=Side.CLIENT)
    public IIcon getIcon(int i, int j) {
        return i == 1 ? this.iconItem[0] : this.iconItem[1];
    }

    @SideOnly(value=Side.CLIENT)
    public String getItemIconName() {
        return this.getTextureName();
    }

    @Override
    public int damageDropped(int meta) {
        return meta;
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void getSubBlocks(Item item, CreativeTabs tab, List list) {
        for (int i = 0; i < chainTypes.length; ++i) {
            list.add(new ItemStack(item, 1, i));
        }
    }

    @Override
    public int onBlockPlaced(World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata) {
        // The metadata provided here is the metadata of the block being placed (from the item stack).
        return metadata; // Return the metadata of the block that should be placed.
    }


    @Override
    public boolean canPlaceBlockAt(World world, int x, int y, int z) {
        Block blockAbove = world.getBlock(x, y + 1, z);
        int metaAbove = world.getBlockMetadata(x, y + 1, z);

        // Get the metadata of the block to be placed (already handled by onBlockPlaced)
        int currentMeta = world.getBlockMetadata(x, y, z); // Metadata of the block being placed

        // Check if the block above is a chain block and if the metadata matches
        if (blockAbove instanceof CinderChain) {
            if (metaAbove != currentMeta) {
                return false; // Prevent placement of different types of chains
            }
            return true; // Same type, allow placement
        }

        // Other checks for fences, walls, slabs, etc.
        if (blockAbove instanceof BlockFence || blockAbove instanceof BlockWall) {
            return true;
        }
        if (blockAbove instanceof BlockSlab && !blockAbove.isOpaqueCube() && (metaAbove & 8) == 0) {
            return true;
        }
        if (blockAbove instanceof BlockStairs && (metaAbove & 4) == 0) {
            return true;
        }

        // Check if the block above is solid on the bottom
        return blockAbove.isSideSolid(world, x, y + 1, z, ForgeDirection.DOWN);
    }


}
