package com.kitsu.medievalcraft.block.wood.clean;

import net.minecraft.block.Block;
import net.minecraft.block.BlockRotatedPillar;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import com.kitsu.medievalcraft.Main;
import com.kitsu.medievalcraft.block.ModBlocks;
import com.kitsu.medievalcraft.util.CustomTab;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class CleanBirch extends BlockRotatedPillar {

	@SideOnly(Side.CLIENT)
	private IIcon iconSide;
	
	@SideOnly(Side.CLIENT)
	private IIcon iconTop;
	
	@SideOnly(Side.CLIENT)
	private IIcon iconBot;

	public CleanBirch(String unlocalizedName, Material mat) {
		super(mat);
		this.setBlockName(unlocalizedName);
		this.setBlockTextureName(Main.MODID + ":" + unlocalizedName);
		this.setCreativeTab(CustomTab.MedievalCraftTab);
		this.setHardness(3.0F);
		this.setResistance(3.0F);
		this.setStepSound(Block.soundTypeWood);

	}
	
public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int q, float a, float b, float c) {
		
		if(player.inventory.getCurrentItem()!=null){
			if(player.getCurrentEquippedItem().getItem() instanceof ItemAxe){
				player.worldObj.playSoundAtEntity(player, Main.MODID + ":splitlog", 1.0F, 1.0F);
				player.swingItem();
				if(world.rand.nextInt(4)==0){
					if(!world.isRemote){
						world.setBlock(x, y, z, Blocks.air, 0, 2);
						world.spawnEntityInWorld(new EntityItem(world, x, y, z, new ItemStack(ModBlocks.birchSplitLog, 4, 0)));
						return true;
					}
				}
			}
		}	
		return false;
	}
	
    public int onBlockPlaced(World p_149660_1_, int p_149660_2_, int p_149660_3_, int p_149660_4_, int p_149660_5_, float p_149660_6_, float p_149660_7_, float p_149660_8_, int p_149660_9_)
    {
        int j1 = p_149660_9_ & 3;
        byte b0 = 0;

        switch (p_149660_5_)
        {
            case 0:
            case 1:
                b0 = 0;
                break;
            case 2:
            case 3:
                b0 = 8;
                break;
            case 4:
            case 5:
                b0 = 4;
        }

        return j1 | b0;
    }
	
	@SideOnly(Side.CLIENT)
	public void registerBlockIcons(IIconRegister iconRegister) {
		this.blockIcon = iconRegister.registerIcon(Main.MODID + ":" + "cleanBirchSide");
		this.iconSide = iconRegister.registerIcon(Main.MODID + ":" + "cleanBirchSide");
		this.iconBot  = iconRegister.registerIcon(Main.MODID + ":" + "cleanBirchTop");
		this.iconTop   = iconRegister.registerIcon(Main.MODID + ":" + "cleanBirchTop");
	}
	
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(int side, int metadata)
    {
        int k = metadata & 12;
        int l = metadata & 3;
        return k == 0 && (side == 1 || side == 0) ? this.getTopIcon(l) : (k == 4 && (side == 5 || side == 4) ? this.getTopIcon(l) : (k == 8 && (side == 2 || side == 3) ? this.getTopIcon(l) : this.getSideIcon(l)));
    }

    @SideOnly(Side.CLIENT)
    protected IIcon getSideIcon(int p_150163_1_)
    {
        return this.iconSide;
    }

    @SideOnly(Side.CLIENT)
    protected IIcon getTopIcon(int p_150161_1_)
    {
        return this.iconTop;
    }

}
