package trikzon.moblocks.blocks;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import trikzon.moblocks.MoBlocks;
import trikzon.moblocks.Reference;
import trikzon.moblocks.blocks.item.IMetaBlockName;

public class BlockDyedConcretePowder extends Block implements IMetaBlockName{
	
	public static final PropertyEnum DYE = PropertyEnum.create("dye", EnumDyeColor.class);

	public BlockDyedConcretePowder(String unlocalizedName) {
		super(Material.SAND);
		this.setUnlocalizedName(unlocalizedName);
		this.setRegistryName(new ResourceLocation(Reference.MODID, unlocalizedName));
		this.setDefaultState(this.blockState.getBaseState().withProperty(DYE, EnumDyeColor.WHITE));
		this.setCreativeTab(MoBlocks.vanillaplus);
		this.setSoundType(SoundType.SAND);
	}
	
	@Override
	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] {DYE});
	}
	
	@Override
	public int getMetaFromState(IBlockState state) {
		EnumDyeColor dye = (EnumDyeColor) state.getValue(DYE);
		return dye.getMetadata();
	}
	
	@Override
	public IBlockState getStateFromMeta(int meta) {
		return this.getDefaultState().withProperty(DYE, EnumDyeColor.values()[meta]);
	}
	
	@Override
	public void getSubBlocks(Item itemIn, CreativeTabs tab, List<ItemStack> list) {
		for(int i = 0; i < EnumDyeColor.values().length; i++) {
			list.add(new ItemStack(itemIn, 1, i));
		}
	}
	
	@Override
	public String getSpecialName(ItemStack stack) {
		return EnumDyeColor.values()[stack.getItemDamage()].getName();
	}
	
	@Override
	public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos,
			EntityPlayer player) {
		return new ItemStack(Item.getItemFromBlock(this), 1, getMetaFromState(world.getBlockState(pos)));
	}
	
	@Override
	public int damageDropped(IBlockState state) {
		return getMetaFromState(state);
	}

}
