package net.minecraft.world.item;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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 ben) {
        return true;
    }
    
    @Override
    public boolean canAttackBlock(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj) {
        if (!bjw.isClientSide) {
            this.handleInteraction(ayj, byj, bjw, fk, false, ayj.getItemInHand(InteractionHand.MAIN_HAND));
        }
        return false;
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final Player ayj3 = bfz.getPlayer();
        final Level bjw4 = bfz.getLevel();
        if (!bjw4.isClientSide && ayj3 != null) {
            final BlockPos fk5 = bfz.getClickedPos();
            this.handleInteraction(ayj3, bjw4.getBlockState(fk5), bjw4, fk5, true, bfz.getItemInHand());
        }
        return InteractionResult.SUCCESS;
    }
    
    private void handleInteraction(final Player ayj, final BlockState byj, final LevelAccessor bjx, final BlockPos fk, final boolean boolean5, final ItemStack ben) {
        if (!ayj.canUseGameMasterBlocks()) {
            return;
        }
        final Block bph8 = byj.getBlock();
        final StateDefinition<Block, BlockState> byk9 = bph8.getStateDefinition();
        final Collection<Property<?>> collection10 = byk9.getProperties();
        final String string11 = Registry.BLOCK.getKey(bph8).toString();
        if (collection10.isEmpty()) {
            message(ayj, new TranslatableComponent(this.getDescriptionId() + ".empty", new Object[] { string11 }));
            return;
        }
        final CompoundTag jt12 = ben.getOrCreateTagElement("DebugProperty");
        final String string12 = jt12.getString(string11);
        Property<?> bzm14 = byk9.getProperty(string12);
        if (boolean5) {
            if (bzm14 == null) {
                bzm14 = collection10.iterator().next();
            }
            final BlockState byj2 = DebugStickItem.cycleState(byj, bzm14, ayj.isSecondaryUseActive());
            bjx.setBlock(fk, byj2, 18);
            message(ayj, new TranslatableComponent(this.getDescriptionId() + ".update", new Object[] { bzm14.getName(), DebugStickItem.getNameHelper(byj2, bzm14) }));
        }
        else {
            bzm14 = DebugStickItem.<Property<?>>getRelative(collection10, bzm14, ayj.isSecondaryUseActive());
            final String string13 = bzm14.getName();
            jt12.putString(string11, string13);
            message(ayj, new TranslatableComponent(this.getDescriptionId() + ".select", new Object[] { string13, DebugStickItem.getNameHelper(byj, bzm14) }));
        }
    }
    
    private static <T extends Comparable<T>> BlockState cycleState(final BlockState byj, final Property<T> bzm, final boolean boolean3) {
        return ((AbstractStateHolder<O, BlockState>)byj).<T, Comparable>setValue(bzm, (Comparable)DebugStickItem.<V>getRelative((Iterable<V>)bzm.getPossibleValues(), (V)byj.<T>getValue((Property<T>)bzm), 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 ayj, final Component lf) {
        ((ServerPlayer)ayj).sendMessage(lf, ChatType.GAME_INFO);
    }
    
    private static <T extends Comparable<T>> String getNameHelper(final BlockState byj, final Property<T> bzm) {
        return bzm.getName(byj.<T>getValue(bzm));
    }
}
