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 lotr.common.LOTRMod;
import lotr.common.block.LOTRBlockChandelier;
import lotr.common.block.LOTRBlockOrcChain;
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

public class silverChain extends LOTRBlockOrcChain {

    public IIcon icon;
    @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;

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

    @SideOnly(value=Side.CLIENT)
    public void registerBlockIcons(IIconRegister iconregister) {
        this.iconMiddle = iconregister.registerIcon(this.getTextureName() + "_mid");
        this.iconTop = iconregister.registerIcon(this.getTextureName() + "_top");
        this.iconBottom = iconregister.registerIcon(this.getTextureName() + "_bottom");
        this.iconSingle = iconregister.registerIcon(this.getTextureName() + "_single");
        this.blockIcon = iconregister.registerIcon(this.getTextureName());
    }

    @SideOnly(value=Side.CLIENT)
    public IIcon getIcon(IBlockAccess world, int i, int j, int k, int side) {
        boolean chainBelow;
        Block above = world.getBlock(i, j + 1, k);
        Block below = world.getBlock(i, j - 1, k);
        boolean chainAbove = above instanceof silverChain;
        boolean bl = chainBelow = below instanceof silverChain || below instanceof LOTRBlockChandelier;
        if (chainAbove && chainBelow) {
            return this.iconMiddle;
        }
        if (chainAbove) {
            return this.iconBottom;
        }
        if (chainBelow) {
            return this.iconTop;
        }
        return this.iconSingle;
    }

    @SideOnly(value=Side.CLIENT)
    public IIcon getIcon(int i, int j) {
        return this.iconMiddle;
    }

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

    public boolean canPlaceBlockAt(World world, int i, int j, int k) {
        Block block = world.getBlock(i, j + 1, k);
        int meta = world.getBlockMetadata(i, j + 1, k);
        if (block instanceof silverChain) {
            return true;
        }
        if (block instanceof BlockFence || block instanceof BlockWall) {
            return true;
        }
        if (block instanceof BlockSlab && !block.isOpaqueCube() && (meta & 8) == 0) {
            return true;
        }
        if (block instanceof BlockStairs && (meta & 4) == 0) {
            return true;
        }
        return world.getBlock(i, j + 1, k).isSideSolid((IBlockAccess)world, i, j + 1, k, ForgeDirection.DOWN);
    }

    public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer entityplayer, int side, float f, float f1, float f2) {
        ItemStack itemstack = entityplayer.getHeldItem();
        if (itemstack != null && itemstack.getItem() == Item.getItemFromBlock((Block)this)) {
            Block block;
            int j1;
            for (j1 = j; j1 >= 0 && j1 < world.getHeight() && (block = world.getBlock(i, j1, k)) == this; --j1) {
            }
            if (j1 >= 0 && j1 < world.getHeight()) {
                block = world.getBlock(i, j1, k);
                if (this.canPlaceBlockOnSide(world, i, j1, k, side) && block.isReplaceable((IBlockAccess)world, i, j1, k) && !block.getMaterial().isLiquid()) {
                    int thisMeta = world.getBlockMetadata(i, j, k);
                    world.setBlock(i, j1, k, (Block)this, thisMeta, 3);
                    world.playSoundEffect((double)((float)i + 0.5f), (double)((float)j1 + 0.5f), (double)((float)k + 0.5f), this.stepSound.func_150496_b(), (this.stepSound.getVolume() + 1.0f) / 2.0f, this.stepSound.getPitch() * 0.8f);
                    if (!entityplayer.capabilities.isCreativeMode) {
                        --itemstack.stackSize;
                    }
                    if (itemstack.stackSize <= 0) {
                        entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, null);
                    }
                    return true;
                }
            }
        }
        return false;
    }
}
