package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
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.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.network.chat.MutableComponent;
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.entity.monster.piglin.PiglinAi;
import net.minecraft.stats.Stats;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.ShulkerSharedHelper;
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.core.BlockPos;
import net.minecraft.world.level.Level;
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 net.minecraft.world.level.block.state.BlockBehaviour;
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 bje, final Properties c) {
        super(c);
        this.color = bje;
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, Direction.UP));
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new ShulkerBoxBlockEntity(this.color);
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.ENTITYBLOCK_ANIMATED;
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (bqb.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        if (bec.isSpectator()) {
            return InteractionResult.CONSUME;
        }
        final BlockEntity cdl8 = bqb.getBlockEntity(fu);
        if (cdl8 instanceof ShulkerBoxBlockEntity) {
            final ShulkerBoxBlockEntity ceg9 = (ShulkerBoxBlockEntity)cdl8;
            boolean boolean10;
            if (ceg9.getAnimationStatus() == ShulkerBoxBlockEntity.AnimationStatus.CLOSED) {
                final Direction fz11 = cfj.<Direction>getValue(ShulkerBoxBlock.FACING);
                boolean10 = bqb.noCollision(ShulkerSharedHelper.openBoundingBox(fu, fz11));
            }
            else {
                boolean10 = true;
            }
            if (boolean10) {
                bec.openMenu(ceg9);
                bec.awardStat(Stats.OPEN_SHULKER_BOX);
                PiglinAi.angerNearbyPiglins(bec, true);
            }
            return InteractionResult.CONSUME;
        }
        return InteractionResult.PASS;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, bin.getClickedFace());
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(ShulkerBoxBlock.FACING);
    }
    
    @Override
    public void playerWillDestroy(final Level bqb, final BlockPos fu, final BlockState cfj, final Player bec) {
        final BlockEntity cdl6 = bqb.getBlockEntity(fu);
        if (cdl6 instanceof ShulkerBoxBlockEntity) {
            final ShulkerBoxBlockEntity ceg7 = (ShulkerBoxBlockEntity)cdl6;
            if (!bqb.isClientSide && bec.isCreative() && !ceg7.isEmpty()) {
                final ItemStack bki8 = getColoredItemStack(this.getColor());
                final CompoundTag le9 = ceg7.saveToTag(new CompoundTag());
                if (!le9.isEmpty()) {
                    bki8.addTagElement("BlockEntityTag", le9);
                }
                if (ceg7.hasCustomName()) {
                    bki8.setHoverName(ceg7.getCustomName());
                }
                final ItemEntity bbg10 = new ItemEntity(bqb, fu.getX() + 0.5, fu.getY() + 0.5, fu.getZ() + 0.5, bki8);
                bbg10.setDefaultPickUpDelay();
                bqb.addFreshEntity(bbg10);
            }
            else {
                ceg7.unpackLootTable(bec);
            }
        }
        super.playerWillDestroy(bqb, fu, cfj, bec);
    }
    
    @Override
    public List<ItemStack> getDrops(final BlockState cfj, LootContext.Builder a) {
        final BlockEntity cdl4 = a.<BlockEntity>getOptionalParameter(LootContextParams.BLOCK_ENTITY);
        if (cdl4 instanceof ShulkerBoxBlockEntity) {
            final ShulkerBoxBlockEntity ceg5 = (ShulkerBoxBlockEntity)cdl4;
            int integer4;
            final ShulkerBoxBlockEntity shulkerBoxBlockEntity;
            a = a.withDynamicDrop(ShulkerBoxBlock.CONTENTS, (dat, consumer) -> {
                for (integer4 = 0; integer4 < shulkerBoxBlockEntity.getContainerSize(); ++integer4) {
                    consumer.accept(shulkerBoxBlockEntity.getItem(integer4));
                }
                return;
            });
        }
        return super.getDrops(cfj, a);
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, final LivingEntity aoy, final ItemStack bki) {
        if (bki.hasCustomHoverName()) {
            final BlockEntity cdl7 = bqb.getBlockEntity(fu);
            if (cdl7 instanceof ShulkerBoxBlockEntity) {
                ((ShulkerBoxBlockEntity)cdl7).setCustomName(bki.getHoverName());
            }
        }
    }
    
    @Override
    public void onRemove(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj1.is(cfj4.getBlock())) {
            return;
        }
        final BlockEntity cdl7 = bqb.getBlockEntity(fu);
        if (cdl7 instanceof ShulkerBoxBlockEntity) {
            bqb.updateNeighbourForOutputSignal(fu, cfj1.getBlock());
        }
        super.onRemove(cfj1, bqb, fu, cfj4, boolean5);
    }
    
    @Override
    public void appendHoverText(final ItemStack bki, @Nullable final BlockGetter bpg, final List<Component> list, final TooltipFlag bls) {
        super.appendHoverText(bki, bpg, list, bls);
        final CompoundTag le6 = bki.getTagElement("BlockEntityTag");
        if (le6 != null) {
            if (le6.contains("LootTable", 8)) {
                list.add(new TextComponent("???????"));
            }
            if (le6.contains("Items", 9)) {
                final NonNullList<ItemStack> gi7 = NonNullList.<ItemStack>withSize(27, ItemStack.EMPTY);
                ContainerHelper.loadAllItems(le6, gi7);
                int integer8 = 0;
                int integer9 = 0;
                for (final ItemStack bki2 : gi7) {
                    if (!bki2.isEmpty()) {
                        ++integer9;
                        if (integer8 > 4) {
                            continue;
                        }
                        ++integer8;
                        final MutableComponent mx12 = bki2.getHoverName().copy();
                        mx12.append(" x").append(String.valueOf(bki2.getCount()));
                        list.add(mx12);
                    }
                }
                if (integer9 - integer8 > 0) {
                    list.add(new TranslatableComponent("container.shulkerBox.more", new Object[] { integer9 - integer8 }).withStyle(ChatFormatting.ITALIC));
                }
            }
        }
    }
    
    @Override
    public PushReaction getPistonPushReaction(final BlockState cfj) {
        return PushReaction.DESTROY;
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        final BlockEntity cdl6 = bpg.getBlockEntity(fu);
        if (cdl6 instanceof ShulkerBoxBlockEntity) {
            return Shapes.create(((ShulkerBoxBlockEntity)cdl6).getBoundingBox(cfj));
        }
        return Shapes.block();
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState cfj, final Level bqb, final BlockPos fu) {
        return AbstractContainerMenu.getRedstoneSignalFromContainer((Container)bqb.getBlockEntity(fu));
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bpg, final BlockPos fu, final BlockState cfj) {
        final ItemStack bki5 = super.getCloneItemStack(bpg, fu, cfj);
        final ShulkerBoxBlockEntity ceg6 = (ShulkerBoxBlockEntity)bpg.getBlockEntity(fu);
        final CompoundTag le7 = ceg6.saveToTag(new CompoundTag());
        if (!le7.isEmpty()) {
            bki5.addTagElement("BlockEntityTag", le7);
        }
        return bki5;
    }
    
    @Nullable
    public static DyeColor getColorFromItem(final Item bke) {
        return getColorFromBlock(Block.byItem(bke));
    }
    
    @Nullable
    public static DyeColor getColorFromBlock(final Block bvr) {
        if (bvr instanceof ShulkerBoxBlock) {
            return ((ShulkerBoxBlock)bvr).getColor();
        }
        return null;
    }
    
    public static Block getBlockByColor(@Nullable final DyeColor bje) {
        if (bje == null) {
            return Blocks.SHULKER_BOX;
        }
        switch (bje) {
            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 bje) {
        return new ItemStack(getBlockByColor(bje));
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, cap.rotate(cfj.<Direction>getValue(ShulkerBoxBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<Direction>getValue(ShulkerBoxBlock.FACING)));
    }
    
    static {
        FACING = DirectionalBlock.FACING;
        CONTENTS = new ResourceLocation("contents");
    }
}
