package common.block;

import java.util.List;

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.properties.IProperty;
import common.properties.PropertyEnum;
import common.util.Identifyable;
import common.world.State;

public class BlockStoneBrick extends Block
{
    public static final PropertyEnum<BlockStoneBrick.EnumType> VARIANT = PropertyEnum.<BlockStoneBrick.EnumType>create("variant", BlockStoneBrick.EnumType.class);
    public static final int DEFAULT_META = BlockStoneBrick.EnumType.DEFAULT.getMetadata();
    public static final int MOSSY_META = BlockStoneBrick.EnumType.MOSSY.getMetadata();
    public static final int CRACKED_META = BlockStoneBrick.EnumType.CRACKED.getMetadata();
    public static final int CHISELED_META = BlockStoneBrick.EnumType.CHISELED.getMetadata();

    public BlockStoneBrick()
    {
        super(Material.rock);
        this.setDefaultState(this.getBaseState().withProperty(VARIANT, BlockStoneBrick.EnumType.DEFAULT));
        this.setTab(CheatTab.tabBlocks);
    }

    /**
     * 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)
    {
        return ((BlockStoneBrick.EnumType)state.getValue(VARIANT)).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)
    {
        for (BlockStoneBrick.EnumType blockstonebrick$enumtype : BlockStoneBrick.EnumType.values())
        {
            list.add(new ItemStack(itemIn, 1, blockstonebrick$enumtype.getMetadata()));
        }
    }

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

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

    protected IProperty[] getProperties()
    {
        return new IProperty[] {VARIANT};
    }
    
    public Model getModel(ModelProvider provider, String name, State state) {
    	return provider.getModel("stonebrick_" + state.getValue(VARIANT).getTexture()).add().all();
    }

    public static enum EnumType implements Identifyable
    {
        DEFAULT("default", 0, "stonebrick", "Steinziegel"),
        MOSSY("mossy", 1, "mossy_stonebrick", "Bemooste Steinziegel"),
        CRACKED("cracked", 2, "cracked_stonebrick", "Rissige Steinziegel"),
        CHISELED("chiseled", 3, "chiseled_stonebrick", "Gemeißelte Steinziegel");

        private static final BlockStoneBrick.EnumType[] META_LOOKUP = new BlockStoneBrick.EnumType[values().length];
        private final int meta;
        private final String texture;
        private final String name;
        private final String display;

        private EnumType(String texture, int meta, String name, String display)
        {
            this.texture = texture;
            this.meta = meta;
            this.name = name;
            this.display = display;
        }

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

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

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

            return META_LOOKUP[meta];
        }

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

        public String getTexture()
        {
            return this.texture;
        }

        public String getDisplay()
        {
            return this.display;
        }

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