package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import java.util.function.Consumer;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Item;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.Container;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.material.PushReaction;
import java.util.Iterator;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.ContainerHelper;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import java.util.List;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.phys.AABB;
import net.minecraft.stats.Stats;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import javax.annotation.Nullable;
import net.minecraft.world.item.DyeColor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.properties.EnumProperty;

public class ShulkerBoxBlock extends BaseEntityBlock {
    public static final EnumProperty<Direction> FACING;
    public static final ResourceLocation CONTENTS;
    @Nullable
    private final DyeColor color;
    
    public ShulkerBoxBlock(@Nullable final DyeColor bdj, final Properties c) {
        super(c);
        this.color = bdj;
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, Direction.UP));
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        return new ShulkerBoxBlockEntity(this.color);
    }
    
    @Override
    public boolean isSuffocating(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return true;
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.ENTITYBLOCK_ANIMATED;
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (bjw.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        if (ayj.isSpectator()) {
            return InteractionResult.SUCCESS;
        }
        final BlockEntity bwl8 = bjw.getBlockEntity(fk);
        if (bwl8 instanceof ShulkerBoxBlockEntity) {
            final Direction fp9 = byj.<Direction>getValue(ShulkerBoxBlock.FACING);
            final ShulkerBoxBlockEntity bxg11 = (ShulkerBoxBlockEntity)bwl8;
            boolean boolean10;
            if (bxg11.getAnimationStatus() == ShulkerBoxBlockEntity.AnimationStatus.CLOSED) {
                final AABB cvf12 = Shapes.block().bounds().expandTowards(0.5f * fp9.getStepX(), 0.5f * fp9.getStepY(), 0.5f * fp9.getStepZ()).contract(fp9.getStepX(), fp9.getStepY(), fp9.getStepZ());
                boolean10 = bjw.noCollision(cvf12.move(fk.relative(fp9)));
            }
            else {
                boolean10 = true;
            }
            if (boolean10) {
                ayj.openMenu(bxg11);
                ayj.awardStat(Stats.OPEN_SHULKER_BOX);
            }
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.PASS;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, bcq.getClickedFace());
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(ShulkerBoxBlock.FACING);
    }
    
    @Override
    public void playerWillDestroy(final Level bjw, final BlockPos fk, final BlockState byj, final Player ayj) {
        final BlockEntity bwl6 = bjw.getBlockEntity(fk);
        if (bwl6 instanceof ShulkerBoxBlockEntity) {
            final ShulkerBoxBlockEntity bxg7 = (ShulkerBoxBlockEntity)bwl6;
            if (!bjw.isClientSide && ayj.isCreative() && !bxg7.isEmpty()) {
                final ItemStack ben8 = getColoredItemStack(this.getColor());
                final CompoundTag jt9 = bxg7.saveToTag(new CompoundTag());
                if (!jt9.isEmpty()) {
                    ben8.addTagElement("BlockEntityTag", jt9);
                }
                if (bxg7.hasCustomName()) {
                    ben8.setHoverName(bxg7.getCustomName());
                }
                final ItemEntity awb10 = new ItemEntity(bjw, fk.getX(), fk.getY(), fk.getZ(), ben8);
                awb10.setDefaultPickUpDelay();
                bjw.addFreshEntity(awb10);
            }
            else {
                bxg7.unpackLootTable(ayj);
            }
        }
        super.playerWillDestroy(bjw, fk, byj, ayj);
    }
    
    @Override
    public List<ItemStack> getDrops(final BlockState byj, LootContext.Builder a) {
        final BlockEntity bwl4 = a.<BlockEntity>getOptionalParameter(LootContextParams.BLOCK_ENTITY);
        if (bwl4 instanceof ShulkerBoxBlockEntity) {
            final ShulkerBoxBlockEntity bxg5 = (ShulkerBoxBlockEntity)bwl4;
            int integer4;
            final ShulkerBoxBlockEntity shulkerBoxBlockEntity;
            a = a.withDynamicDrop(ShulkerBoxBlock.CONTENTS, (crx, consumer) -> {
                for (integer4 = 0; integer4 < shulkerBoxBlockEntity.getContainerSize(); ++integer4) {
                    consumer.accept(shulkerBoxBlockEntity.getItem(integer4));
                }
                return;
            });
        }
        return super.getDrops(byj, a);
    }
    
    @Override
    public void setPlacedBy(final Level bjw, final BlockPos fk, final BlockState byj, final LivingEntity akz, final ItemStack ben) {
        if (ben.hasCustomHoverName()) {
            final BlockEntity bwl7 = bjw.getBlockEntity(fk);
            if (bwl7 instanceof ShulkerBoxBlockEntity) {
                ((ShulkerBoxBlockEntity)bwl7).setCustomName(ben.getHoverName());
            }
        }
    }
    
    @Override
    public void onRemove(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj1.getBlock() == byj4.getBlock()) {
            return;
        }
        final BlockEntity bwl7 = bjw.getBlockEntity(fk);
        if (bwl7 instanceof ShulkerBoxBlockEntity) {
            bjw.updateNeighbourForOutputSignal(fk, byj1.getBlock());
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
    }
    
    @Override
    public void appendHoverText(final ItemStack ben, @Nullable final BlockGetter bjg, final List<Component> list, final TooltipFlag bfw) {
        super.appendHoverText(ben, bjg, list, bfw);
        final CompoundTag jt6 = ben.getTagElement("BlockEntityTag");
        if (jt6 != null) {
            if (jt6.contains("LootTable", 8)) {
                list.add(new TextComponent("???????"));
            }
            if (jt6.contains("Items", 9)) {
                final NonNullList<ItemStack> fy7 = NonNullList.<ItemStack>withSize(27, ItemStack.EMPTY);
                ContainerHelper.loadAllItems(jt6, fy7);
                int integer8 = 0;
                int integer9 = 0;
                for (final ItemStack ben2 : fy7) {
                    if (!ben2.isEmpty()) {
                        ++integer9;
                        if (integer8 > 4) {
                            continue;
                        }
                        ++integer8;
                        final Component lf12 = ben2.getHoverName().deepCopy();
                        lf12.append(" x").append(String.valueOf(ben2.getCount()));
                        list.add(lf12);
                    }
                }
                if (integer9 - integer8 > 0) {
                    list.add(new TranslatableComponent("container.shulkerBox.more", new Object[] { integer9 - integer8 }).withStyle(ChatFormatting.ITALIC));
                }
            }
        }
    }
    
    @Override
    public PushReaction getPistonPushReaction(final BlockState byj) {
        return PushReaction.DESTROY;
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        final BlockEntity bwl6 = bjg.getBlockEntity(fk);
        if (bwl6 instanceof ShulkerBoxBlockEntity) {
            return Shapes.create(((ShulkerBoxBlockEntity)bwl6).getBoundingBox(byj));
        }
        return Shapes.block();
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState byj, final Level bjw, final BlockPos fk) {
        return AbstractContainerMenu.getRedstoneSignalFromContainer((Container)bjw.getBlockEntity(fk));
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bjg, final BlockPos fk, final BlockState byj) {
        final ItemStack ben5 = super.getCloneItemStack(bjg, fk, byj);
        final ShulkerBoxBlockEntity bxg6 = (ShulkerBoxBlockEntity)bjg.getBlockEntity(fk);
        final CompoundTag jt7 = bxg6.saveToTag(new CompoundTag());
        if (!jt7.isEmpty()) {
            ben5.addTagElement("BlockEntityTag", jt7);
        }
        return ben5;
    }
    
    @Nullable
    public static DyeColor getColorFromItem(final Item bei) {
        return getColorFromBlock(Block.byItem(bei));
    }
    
    @Nullable
    public static DyeColor getColorFromBlock(final Block bph) {
        if (bph instanceof ShulkerBoxBlock) {
            return ((ShulkerBoxBlock)bph).getColor();
        }
        return null;
    }
    
    public static Block getBlockByColor(@Nullable final DyeColor bdj) {
        if (bdj == null) {
            return Blocks.SHULKER_BOX;
        }
        switch (bdj) {
            case WHITE: {
                return Blocks.WHITE_SHULKER_BOX;
            }
            case ORANGE: {
                return Blocks.ORANGE_SHULKER_BOX;
            }
            case MAGENTA: {
                return Blocks.MAGENTA_SHULKER_BOX;
            }
            case LIGHT_BLUE: {
                return Blocks.LIGHT_BLUE_SHULKER_BOX;
            }
            case YELLOW: {
                return Blocks.YELLOW_SHULKER_BOX;
            }
            case LIME: {
                return Blocks.LIME_SHULKER_BOX;
            }
            case PINK: {
                return Blocks.PINK_SHULKER_BOX;
            }
            case GRAY: {
                return Blocks.GRAY_SHULKER_BOX;
            }
            case LIGHT_GRAY: {
                return Blocks.LIGHT_GRAY_SHULKER_BOX;
            }
            case CYAN: {
                return Blocks.CYAN_SHULKER_BOX;
            }
            default: {
                return Blocks.PURPLE_SHULKER_BOX;
            }
            case BLUE: {
                return Blocks.BLUE_SHULKER_BOX;
            }
            case BROWN: {
                return Blocks.BROWN_SHULKER_BOX;
            }
            case GREEN: {
                return Blocks.GREEN_SHULKER_BOX;
            }
            case RED: {
                return Blocks.RED_SHULKER_BOX;
            }
            case BLACK: {
                return Blocks.BLACK_SHULKER_BOX;
            }
        }
    }
    
    @Nullable
    public DyeColor getColor() {
        return this.color;
    }
    
    public static ItemStack getColoredItemStack(@Nullable final DyeColor bdj) {
        return new ItemStack(getBlockByColor(bdj));
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, btu.rotate(byj.<Direction>getValue(ShulkerBoxBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<Direction>getValue(ShulkerBoxBlock.FACING)));
    }
    
    static {
        FACING = DirectionalBlock.FACING;
        CONTENTS = new ResourceLocation("contents");
    }
}
