package net.minecraft.world.item;

import net.minecraft.core.Rotations;
import java.util.Random;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.EntityType;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.AABB;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionResult;

public class ArmorStandItem extends Item {
    public ArmorStandItem(final Properties a) {
        super(a);
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final Direction fp3 = bfz.getClickedFace();
        if (fp3 == Direction.DOWN) {
            return InteractionResult.FAIL;
        }
        final Level bjw4 = bfz.getLevel();
        final BlockPlaceContext bcq5 = new BlockPlaceContext(bfz);
        final BlockPos fk6 = bcq5.getClickedPos();
        final BlockPos fk7 = fk6.above();
        if (!bcq5.canPlace() || !bjw4.getBlockState(fk7).canBeReplaced(bcq5)) {
            return InteractionResult.FAIL;
        }
        final double double8 = fk6.getX();
        final double double9 = fk6.getY();
        final double double10 = fk6.getZ();
        final List<Entity> list14 = bjw4.getEntities(null, new AABB(double8, double9, double10, double8 + 1.0, double9 + 2.0, double10 + 1.0));
        if (!list14.isEmpty()) {
            return InteractionResult.FAIL;
        }
        final ItemStack ben15 = bfz.getItemInHand();
        if (!bjw4.isClientSide) {
            bjw4.removeBlock(fk6, false);
            bjw4.removeBlock(fk7, false);
            final ArmorStand avp16 = new ArmorStand(bjw4, double8 + 0.5, double9, double10 + 0.5);
            final float float17 = Mth.floor((Mth.wrapDegrees(bfz.getRotation() - 180.0f) + 22.5f) / 45.0f) * 45.0f;
            avp16.moveTo(double8 + 0.5, double9, double10 + 0.5, float17, 0.0f);
            this.randomizePose(avp16, bjw4.random);
            EntityType.updateCustomEntityTag(bjw4, bfz.getPlayer(), avp16, ben15.getTag());
            bjw4.addFreshEntity(avp16);
            bjw4.playSound(null, avp16.getX(), avp16.getY(), avp16.getZ(), SoundEvents.ARMOR_STAND_PLACE, SoundSource.BLOCKS, 0.75f, 0.8f);
        }
        ben15.shrink(1);
        return InteractionResult.SUCCESS;
    }
    
    private void randomizePose(final ArmorStand avp, final Random random) {
        Rotations gc4 = avp.getHeadPose();
        float float6 = random.nextFloat() * 5.0f;
        final float float7 = random.nextFloat() * 20.0f - 10.0f;
        Rotations gc5 = new Rotations(gc4.getX() + float6, gc4.getY() + float7, gc4.getZ());
        avp.setHeadPose(gc5);
        gc4 = avp.getBodyPose();
        float6 = random.nextFloat() * 10.0f - 5.0f;
        gc5 = new Rotations(gc4.getX(), gc4.getY() + float6, gc4.getZ());
        avp.setBodyPose(gc5);
    }
}
