package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import javax.annotation.Nullable;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.LevelReader;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class CocoaBlock extends HorizontalDirectionalBlock implements BonemealableBlock {
    public static final IntegerProperty AGE;
    protected static final VoxelShape[] EAST_AABB;
    protected static final VoxelShape[] WEST_AABB;
    protected static final VoxelShape[] NORTH_AABB;
    protected static final VoxelShape[] SOUTH_AABB;
    
    public CocoaBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)CocoaBlock.FACING, Direction.NORTH)).<Comparable, Integer>setValue((Property<Comparable>)CocoaBlock.AGE, 0));
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (xd.random.nextInt(5) == 0) {
            final int integer6 = byj.<Integer>getValue((Property<Integer>)CocoaBlock.AGE);
            if (integer6 < 2) {
                xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)CocoaBlock.AGE, integer6 + 1), 2);
            }
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final Block bph5 = bjz.getBlockState(fk.relative(byj.<Direction>getValue((Property<Direction>)CocoaBlock.FACING))).getBlock();
        return bph5.is(BlockTags.JUNGLE_LOGS);
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        final int integer6 = byj.<Integer>getValue((Property<Integer>)CocoaBlock.AGE);
        switch (byj.<Direction>getValue((Property<Direction>)CocoaBlock.FACING)) {
            case SOUTH: {
                return CocoaBlock.SOUTH_AABB[integer6];
            }
            default: {
                return CocoaBlock.NORTH_AABB[integer6];
            }
            case WEST: {
                return CocoaBlock.WEST_AABB[integer6];
            }
            case EAST: {
                return CocoaBlock.EAST_AABB[integer6];
            }
        }
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        BlockState byj3 = this.defaultBlockState();
        final LevelReader bjz4 = bcq.getLevel();
        final BlockPos fk5 = bcq.getClickedPos();
        for (final Direction fp9 : bcq.getNearestLookingDirections()) {
            if (fp9.getAxis().isHorizontal()) {
                byj3 = ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Direction>setValue((Property<Comparable>)CocoaBlock.FACING, fp9);
                if (byj3.canSurvive(bjz4, fk5)) {
                    return byj3;
                }
            }
        }
        return null;
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (fp == byj1.<Direction>getValue((Property<Direction>)CocoaBlock.FACING) && !byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        return byj.<Integer>getValue((Property<Integer>)CocoaBlock.AGE) < 2;
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bjw, final Random random, final BlockPos fk, final BlockState byj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel xd, final Random random, final BlockPos fk, final BlockState byj) {
        xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)CocoaBlock.AGE, byj.<Integer>getValue((Property<Integer>)CocoaBlock.AGE) + 1), 2);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(CocoaBlock.FACING, CocoaBlock.AGE);
    }
    
    static {
        AGE = BlockStateProperties.AGE_2;
        EAST_AABB = new VoxelShape[] { Block.box(11.0, 7.0, 6.0, 15.0, 12.0, 10.0), Block.box(9.0, 5.0, 5.0, 15.0, 12.0, 11.0), Block.box(7.0, 3.0, 4.0, 15.0, 12.0, 12.0) };
        WEST_AABB = new VoxelShape[] { Block.box(1.0, 7.0, 6.0, 5.0, 12.0, 10.0), Block.box(1.0, 5.0, 5.0, 7.0, 12.0, 11.0), Block.box(1.0, 3.0, 4.0, 9.0, 12.0, 12.0) };
        NORTH_AABB = new VoxelShape[] { Block.box(6.0, 7.0, 1.0, 10.0, 12.0, 5.0), Block.box(5.0, 5.0, 1.0, 11.0, 12.0, 7.0), Block.box(4.0, 3.0, 1.0, 12.0, 12.0, 9.0) };
        SOUTH_AABB = new VoxelShape[] { Block.box(6.0, 7.0, 11.0, 10.0, 12.0, 15.0), Block.box(5.0, 5.0, 9.0, 11.0, 12.0, 15.0), Block.box(4.0, 3.0, 7.0, 12.0, 12.0, 15.0) };
    }
}
