package common.block.foliage;

import common.block.BlockRotatedPillar;
import common.block.Material;
import common.block.SoundType;
import common.entity.types.EntityLiving;
import common.item.CheatTab;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ModelRotation;
import common.properties.Property;
import common.properties.PropertyEnum;
import common.util.BlockPos;
import common.util.Facing;
import common.util.Identifyable;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockLog extends BlockRotatedPillar
{
    public static final PropertyEnum<BlockLog.EnumAxis> LOG_AXIS = PropertyEnum.<BlockLog.EnumAxis>create("axis", BlockLog.EnumAxis.class);

    public BlockLog()
    {
        super(Material.WOOD);
        this.setDefaultState(this.getBaseState().withProperty(LOG_AXIS, BlockLog.EnumAxis.Y));
        this.setTab(CheatTab.WOOD);
        this.setHardness(2.0F);
        this.setStepSound(SoundType.WOOD);
        this.setFlammable(5, 5);
    }

    public void onBlockRemoved(AWorldServer worldIn, BlockPos pos, State state)
    {
        int i = 4;
        int j = i + 1;

        if (worldIn.isAreaLoaded(pos.add(-j, -j, -j), pos.add(j, j, j)))
        {
            for (BlockPos blockpos : BlockPos.getAllInBox(pos.add(-i, -i, -i), pos.add(i, i, i)))
            {
                State iblockstate = worldIn.getState(blockpos);

                if (iblockstate.getBlock().getMaterial() == Material.LEAVES && !((Boolean)iblockstate.getValue(BlockLeaves.DECAY)).booleanValue())
                {
                    worldIn.setState(blockpos, iblockstate.withProperty(BlockLeaves.DECAY, Boolean.valueOf(true)), 4);
                }
            }
        }
    }

    /**
     * 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, EntityLiving placer)
    {
        return super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, placer).withProperty(LOG_AXIS, BlockLog.EnumAxis.fromFacingAxis(facing.getAxis()));
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	switch(state.getValue(LOG_AXIS)) {
	    	case X:
	    		return provider.getModel(name + "_bark").add().d().rot(180).u()
	    				.n(name + "_top").rot(180).s(name + "_top").w().rot(270)
	    				.e().rot(90).rotate(ModelRotation.X0_Y90);
	    	case Y:
	    	default:
	    		return provider.getModel(name + "_bark").add().nswe().du(name + "_top");
	    	case Z:
	    		return provider.getModel(name + "_bark").add().d().rot(180).u()
	    				.n(name + "_top").rot(180).s(name + "_top").w().rot(270)
	    				.e().rot(90);
	    	case NONE:
	    		return provider.getModel(name + "_bark").add().all();
		}
    }
    
    public State getStateFromMeta(int meta)
    {
        State iblockstate = this.getState();

        switch (meta & 3)
        {
            default:
                iblockstate = iblockstate.withProperty(LOG_AXIS, BlockLog.EnumAxis.Y);
                break;

            case 1:
                iblockstate = iblockstate.withProperty(LOG_AXIS, BlockLog.EnumAxis.X);
                break;

            case 2:
                iblockstate = iblockstate.withProperty(LOG_AXIS, BlockLog.EnumAxis.Z);
                break;

            case 3:
                iblockstate = iblockstate.withProperty(LOG_AXIS, BlockLog.EnumAxis.NONE);
        }

        return iblockstate;
    }

    

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        int i = 0;

        switch ((BlockLog.EnumAxis)state.getValue(LOG_AXIS))
        {
            case X:
                i = 1;
                break;

            case Z:
                i = 2;
                break;

            case NONE:
                i = 3;
        }

        return i;
    }

    protected Property[] getProperties()
    {
        return new Property[] {LOG_AXIS};
    }

    public static enum EnumAxis implements Identifyable
    {
        X("x", Facing.Axis.X),
        Y("y", Facing.Axis.Y),
        Z("z", Facing.Axis.Z),
        NONE("none", null);

        private final String name;
        private final Facing.Axis axis;

        private EnumAxis(String name, Facing.Axis axis)
        {
            this.name = name;
            this.axis = axis;
        }

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

        public static BlockLog.EnumAxis fromFacingAxis(Facing.Axis axis)
        {
            switch (axis)
            {
                case X:
                    return X;

                case Y:
                    return Y;

                case Z:
                    return Z;

                default:
                    return NONE;
            }
        }

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

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