package common.block;

import java.util.List;

import common.entity.types.EntityLiving;
import common.init.ItemRegistry;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.material.Material;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ModelRotation;
import common.properties.IProperty;
import common.properties.PropertyEnum;
import common.util.BlockPos;
import common.util.Facing;
import common.util.Identifyable;
import common.world.State;
import common.world.World;

public class BlockQuartz extends Block
{
    public static final PropertyEnum<BlockQuartz.EnumType> VARIANT = PropertyEnum.<BlockQuartz.EnumType>create("variant", BlockQuartz.EnumType.class);

    private final String prefix;
    
    public BlockQuartz(String prefix)
    {
        super(Material.rock);
        this.setDefaultState(this.getBaseState().withProperty(VARIANT, BlockQuartz.EnumType.DEFAULT));
        this.setTab(CheatTab.tabBlocks);
        this.prefix = prefix;
    }

    /**
     * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
     * IBlockstate
     */
    public State onBlockPlaced(World worldIn, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, int meta, EntityLiving placer)
    {
        if (meta == BlockQuartz.EnumType.LINES_Y.getMetadata())
        {
            switch (facing.getAxis())
            {
                case Z:
                    return this.getState().withProperty(VARIANT, BlockQuartz.EnumType.LINES_Z);

                case X:
                    return this.getState().withProperty(VARIANT, BlockQuartz.EnumType.LINES_X);

                case Y:
                default:
                    return this.getState().withProperty(VARIANT, BlockQuartz.EnumType.LINES_Y);
            }
        }
        else
        {
            return meta == BlockQuartz.EnumType.CHISELED.getMetadata() ? this.getState().withProperty(VARIANT, BlockQuartz.EnumType.CHISELED) : this.getState().withProperty(VARIANT, BlockQuartz.EnumType.DEFAULT);
        }
    }

    /**
     * Gets the metadata of the item this Block can drop. This method is called when the block gets destroyed. It
     * returns the metadata of the dropped item based on the old metadata of the block.
     */
    public int damageDropped(State state)
    {
        BlockQuartz.EnumType blockquartz$enumtype = (BlockQuartz.EnumType)state.getValue(VARIANT);
        return blockquartz$enumtype != BlockQuartz.EnumType.LINES_X && blockquartz$enumtype != BlockQuartz.EnumType.LINES_Z ? blockquartz$enumtype.getMetadata() : BlockQuartz.EnumType.LINES_Y.getMetadata();
    }

    public ItemStack createStackedBlock(State state)
    {
        BlockQuartz.EnumType blockquartz$enumtype = (BlockQuartz.EnumType)state.getValue(VARIANT);
        return blockquartz$enumtype != BlockQuartz.EnumType.LINES_X && blockquartz$enumtype != BlockQuartz.EnumType.LINES_Z ? super.createStackedBlock(state) : new ItemStack(ItemRegistry.getItemFromBlock(this), 1, BlockQuartz.EnumType.LINES_Y.getMetadata());
    }

    /**
     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
     */
    public void getSubBlocks(Item itemIn, CheatTab tab, List<ItemStack> list)
    {
        list.add(new ItemStack(itemIn, 1, BlockQuartz.EnumType.DEFAULT.getMetadata()));
        list.add(new ItemStack(itemIn, 1, BlockQuartz.EnumType.CHISELED.getMetadata()));
        list.add(new ItemStack(itemIn, 1, BlockQuartz.EnumType.LINES_Y.getMetadata()));
    }

//    /**
//     * Get the MapColor for this Block and the given BlockState
//     */
//    public MapColor getMapColor(IBlockState state)
//    {
//        return MapColor.quartzColor;
//    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(VARIANT, BlockQuartz.EnumType.byMetadata(meta));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return ((BlockQuartz.EnumType)state.getValue(VARIANT)).getMetadata();
    }

    protected IProperty[] getProperties()
    {
        return new IProperty[] {VARIANT};
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	switch(state.getValue(VARIANT)) {
	    	case DEFAULT:
	    	default:
	    		return provider.getModel(this.prefix + "quartz_block_side").add().nswe().d(this.prefix + "quartz_block_bottom").u(this.prefix + "quartz_top");
	    	case CHISELED:
	    		return provider.getModel(this.prefix + "quartz_block_chiseled").add().nswe().du(this.prefix + "quartz_block_chiseled_top");
	    	case LINES_X:
	    		return provider.getModel(this.prefix + "quartz_block_lines").add().nswe().du(this.prefix + "quartz_block_lines_top").rotate(ModelRotation.X90_Y90);
	    	case LINES_Y:
	    		return provider.getModel(this.prefix + "quartz_block_lines").add().nswe().du(this.prefix + "quartz_block_lines_top");
	    	case LINES_Z:
	    		return provider.getModel(this.prefix + "quartz_block_lines").add().nswe().du(this.prefix + "quartz_block_lines_top").rotate(ModelRotation.X90_Y0);
		}
    }

    public static enum EnumType implements Identifyable
    {
        DEFAULT(0, "default", "default", null),
        CHISELED(1, "chiseled", "chiseled", null),
        LINES_Y(2, "lines_y", "lines", Facing.Axis.Y),
        LINES_X(3, "lines_x", "lines", Facing.Axis.X),
        LINES_Z(4, "lines_z", "lines", Facing.Axis.Z);

        private static final BlockQuartz.EnumType[] META_LOOKUP = new BlockQuartz.EnumType[values().length];
        private final int meta;
        private final String name;
        private final String unlocalizedName;
        private final Facing.Axis axis;

        private EnumType(int meta, String name, String unlocalizedName, Facing.Axis axis)
        {
            this.meta = meta;
            this.name = name;
            this.unlocalizedName = unlocalizedName;
            this.axis = axis;
        }

        public int getMetadata()
        {
            return this.meta;
        }

        public String toString()
        {
            return this.name;
            // return this.unlocalizedName;
        }

        public static BlockQuartz.EnumType byMetadata(int meta)
        {
            if (meta < 0 || meta >= META_LOOKUP.length)
            {
                meta = 0;
            }

            return META_LOOKUP[meta];
        }

        public String getName()
        {
            return this.name;
        }

        public Facing.Axis getAxis()
        {
            return this.axis;
        }

        static {
            for (BlockQuartz.EnumType blockquartz$enumtype : values())
            {
                META_LOOKUP[blockquartz$enumtype.getMetadata()] = blockquartz$enumtype;
            }
        }
    }
}
