package net.minecraft.world.item;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.network.chat.ChatType;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.Util;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import java.util.Collection;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.core.Registry;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.level.LevelAccessor;
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.state.BlockState;

public class DebugStickItem extends Item {
    public DebugStickItem(final Properties a) {
        super(a);
    }
    
    @Override
    public boolean isFoil(final ItemStack bki) {
        return true;
    }
    
    @Override
    public boolean canAttackBlock(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec) {
        if (!bqb.isClientSide) {
            this.handleInteraction(bec, cfj, bqb, fu, false, bec.getItemInHand(InteractionHand.MAIN_HAND));
        }
        return false;
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext blv) {
        final Player bec3 = blv.getPlayer();
        final Level bqb4 = blv.getLevel();
        if (!bqb4.isClientSide && bec3 != null) {
            final BlockPos fu5 = blv.getClickedPos();
            this.handleInteraction(bec3, bqb4.getBlockState(fu5), bqb4, fu5, true, blv.getItemInHand());
        }
        return InteractionResult.sidedSuccess(bqb4.isClientSide);
    }
    
    private void handleInteraction(final Player bec, final BlockState cfj, final LevelAccessor bqc, final BlockPos fu, final boolean boolean5, final ItemStack bki) {
        if (!bec.canUseGameMasterBlocks()) {
            return;
        }
        final Block bvr8 = cfj.getBlock();
        final StateDefinition<Block, BlockState> cfk9 = bvr8.getStateDefinition();
        final Collection<Property<?>> collection10 = cfk9.getProperties();
        final String string11 = Registry.BLOCK.getKey(bvr8).toString();
        if (collection10.isEmpty()) {
            message(bec, new TranslatableComponent(this.getDescriptionId() + ".empty", new Object[] { string11 }));
            return;
        }
        final CompoundTag le12 = bki.getOrCreateTagElement("DebugProperty");
        final String string12 = le12.getString(string11);
        Property<?> cgl14 = cfk9.getProperty(string12);
        if (boolean5) {
            if (cgl14 == null) {
                cgl14 = collection10.iterator().next();
            }
            final BlockState cfj2 = DebugStickItem.cycleState(cfj, cgl14, bec.isSecondaryUseActive());
            bqc.setBlock(fu, cfj2, 18);
            message(bec, new TranslatableComponent(this.getDescriptionId() + ".update", new Object[] { cgl14.getName(), DebugStickItem.getNameHelper(cfj2, cgl14) }));
        }
        else {
            cgl14 = DebugStickItem.<Property<?>>getRelative(collection10, cgl14, bec.isSecondaryUseActive());
            final String string13 = cgl14.getName();
            le12.putString(string11, string13);
            message(bec, new TranslatableComponent(this.getDescriptionId() + ".select", new Object[] { string13, DebugStickItem.getNameHelper(cfj, cgl14) }));
        }
    }
    
    private static <T extends Comparable<T>> BlockState cycleState(final BlockState cfj, final Property<T> cgl, final boolean boolean3) {
        return ((StateHolder<O, BlockState>)cfj).<T, Comparable>setValue(cgl, (Comparable)DebugStickItem.<V>getRelative((Iterable<V>)cgl.getPossibleValues(), (V)cfj.<T>getValue((Property<T>)cgl), boolean3));
    }
    
    private static <T> T getRelative(final Iterable<T> iterable, @Nullable final T object, final boolean boolean3) {
        return boolean3 ? Util.<T>findPreviousInIterable(iterable, object) : Util.<T>findNextInIterable(iterable, object);
    }
    
    private static void message(final Player bec, final Component mr) {
        ((ServerPlayer)bec).sendMessage(mr, ChatType.GAME_INFO, Util.NIL_UUID);
    }
    
    private static <T extends Comparable<T>> String getNameHelper(final BlockState cfj, final Property<T> cgl) {
        return cgl.getName(cfj.<T>getValue(cgl));
    }
}
