package common.block;

import common.entity.types.EntityLiving;
import common.item.CheatTab;
import common.material.Material;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ModelRotation;
import common.properties.IProperty;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class BlockPumpkin extends BlockDirectional
{
//    private BlockPattern snowmanBasePattern;
//    private BlockPattern snowmanPattern;
//    private BlockPattern golemBasePattern;
//    private BlockPattern golemPattern;
//    private static final Predicate<IBlockState> field_181085_Q = new Predicate<IBlockState>()
//    {
//        public boolean test(IBlockState p_apply_1_)
//        {
//            return p_apply_1_ != null && (p_apply_1_.getBlock() == Blocks.pumpkin || p_apply_1_.getBlock() == Blocks.lit_pumpkin);
//        }
//    };

    public BlockPumpkin()
    {
        super(Material.gourd);
        this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.NORTH));
//        this.setTickRandomly(true);
        this.setTab(CheatTab.tabPlants);
    }

//    public void onBlockAdded(IWorldServer worldIn, BlockPos pos, IBlockState state)
//    {
//        super.onBlockAdded(worldIn, pos, state);
//        this.trySpawnGolem(worldIn, pos);
//    }

//    public boolean canDispenserPlace(World worldIn, BlockPos pos)
//    {
//        return this.getSnowmanBasePattern().match(worldIn, pos) != null; // || this.getGolemBasePattern().match(worldIn, pos) != null;
//    }

//    private void trySpawnGolem(World worldIn, BlockPos pos)
//    {
//    	if(!Config.createSnowman) {
//    		return;
//    	}
//    	
//        BlockPattern.PatternHelper blockpattern$patternhelper;
//
//        if ((blockpattern$patternhelper = this.getSnowmanPattern().match(worldIn, pos)) != null)
//        {
//            for (int i = 0; i < this.getSnowmanPattern().getThumbLength(); ++i)
//            {
//                BlockWorldState blockworldstate = blockpattern$patternhelper.translateOffset(0, i, 0);
//                worldIn.setBlockState(blockworldstate.getPos(), Blocks.air.getDefaultState(), 2);
//            }
//
//            EntitySnowman entitysnowman = new EntitySnowman(worldIn);
//            BlockPos blockpos1 = blockpattern$patternhelper.translateOffset(0, 2, 0).getPos();
//            entitysnowman.setLocationAndAngles((double)blockpos1.getX() + 0.5D, (double)blockpos1.getY() + 0.05D, (double)blockpos1.getZ() + 0.5D, 0.0F, 0.0F);
//            worldIn.spawnEntityInWorld(entitysnowman);
//
//            for (int j = 0; j < 120; ++j)
//            {
//                worldIn.spawnParticle(EnumParticleTypes.SNOW_SHOVEL, (double)blockpos1.getX() + worldIn.rand.doublev(), (double)blockpos1.getY() + worldIn.rand.doublev() * 2.5D, (double)blockpos1.getZ() + worldIn.rand.doublev(), 0.0D, 0.0D, 0.0D);
//            }
//
//            for (int i1 = 0; i1 < this.getSnowmanPattern().getThumbLength(); ++i1)
//            {
//                BlockWorldState blockworldstate1 = blockpattern$patternhelper.translateOffset(0, i1, 0);
//                worldIn.notifyNeighborsRespectDebug(blockworldstate1.getPos(), Blocks.air);
//            }
//        }
////        else if ((blockpattern$patternhelper = this.getGolemPattern().match(worldIn, pos)) != null)
////        {
////            for (int k = 0; k < this.getGolemPattern().getPalmLength(); ++k)
////            {
////                for (int l = 0; l < this.getGolemPattern().getThumbLength(); ++l)
////                {
////                    worldIn.setBlockState(blockpattern$patternhelper.translateOffset(k, l, 0).getPos(), Blocks.air.getDefaultState(), 2);
////                }
////            }
////
////            BlockPos blockpos = blockpattern$patternhelper.translateOffset(1, 2, 0).getPos();
////            EntityIronGolem entityirongolem = new EntityIronGolem(worldIn);
//////            entityirongolem.setPlayerCreated(true);
////            entityirongolem.setLocationAndAngles((double)blockpos.getX() + 0.5D, (double)blockpos.getY() + 0.05D, (double)blockpos.getZ() + 0.5D, 0.0F, 0.0F);
////            worldIn.spawnEntityInWorld(entityirongolem);
////
////            for (int j1 = 0; j1 < 120; ++j1)
////            {
////                worldIn.spawnParticle(EnumParticleTypes.SNOWBALL, (double)blockpos.getX() + worldIn.rand.doublev(), (double)blockpos.getY() + worldIn.rand.doublev() * 3.9D, (double)blockpos.getZ() + worldIn.rand.doublev(), 0.0D, 0.0D, 0.0D);
////            }
////
////            for (int k1 = 0; k1 < this.getGolemPattern().getPalmLength(); ++k1)
////            {
////                for (int l1 = 0; l1 < this.getGolemPattern().getThumbLength(); ++l1)
////                {
////                    BlockWorldState blockworldstate2 = blockpattern$patternhelper.translateOffset(k1, l1, 0);
////                    worldIn.notifyNeighborsRespectDebug(blockworldstate2.getPos(), Blocks.air);
////                }
////            }
////        }
//    }

    public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
    {
        return worldIn.getState(pos).getBlock().material.isReplaceable() && worldIn.isBlockSolid(pos.down());
    }

    /**
     * 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)
    {
        return this.getState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(FACING, Facing.getHorizontal(meta));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return ((Facing)state.getValue(FACING)).getHorizontalIndex();
    }

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

//    protected BlockPattern getSnowmanBasePattern()
//    {
//        if (this.snowmanBasePattern == null)
//        {
//            this.snowmanBasePattern = FactoryBlockPattern.start().aisle(" ", "#", "#").where('#', BlockWorldState.hasState(BlockStateHelper.forBlock(Blocks.snow))).build();
//        }
//
//        return this.snowmanBasePattern;
//    }
//
//    protected BlockPattern getSnowmanPattern()
//    {
//        if (this.snowmanPattern == null)
//        {
//            this.snowmanPattern = FactoryBlockPattern.start().aisle("^", "#", "#").where('^', BlockWorldState.hasState(field_181085_Q)).where('#', BlockWorldState.hasState(BlockStateHelper.forBlock(Blocks.snow))).build();
//        }
//
//        return this.snowmanPattern;
//    }

//    protected BlockPattern getGolemBasePattern()
//    {
//        if (this.golemBasePattern == null)
//        {
//            this.golemBasePattern = FactoryBlockPattern.start().aisle("~ ~", "###", "~#~").where('#', BlockWorldState.hasState(BlockStateHelper.forBlock(Blocks.iron_block))).where('~', BlockWorldState.hasState(BlockStateHelper.forBlock(Blocks.air))).build();
//        }
//
//        return this.golemBasePattern;
//    }

//    protected BlockPattern getGolemPattern()
//    {
//        if (this.golemPattern == null)
//        {
//            this.golemPattern = FactoryBlockPattern.start().aisle("~^~", "###", "~#~").where('^', BlockWorldState.hasState(field_181085_Q)).where('#', BlockWorldState.hasState(BlockStateHelper.forBlock(Blocks.iron_block))).where('~', BlockWorldState.hasState(BlockStateHelper.forBlock(Blocks.air))).build();
//        }
//
//        return this.golemPattern;
//    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return provider.getModel("pumpkin_side").add().du("pumpkin_top").n("pumpkin_face_" + (this.getLightValue() == 0 ? "off" : "on"))
    			.s().we().rotate(ModelRotation.getNorthRot(state.getValue(FACING)));
    }
}
