package net.minecraft.world.item;

import net.minecraft.core.Rotations;
import java.util.Random;
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.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.ArmorStand;
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 blv) {
        final Direction fz3 = blv.getClickedFace();
        if (fz3 == Direction.DOWN) {
            return InteractionResult.FAIL;
        }
        final Level bqb4 = blv.getLevel();
        final BlockPlaceContext bin5 = new BlockPlaceContext(blv);
        final BlockPos fu6 = bin5.getClickedPos();
        final ItemStack bki7 = blv.getItemInHand();
        final ArmorStand bay8 = EntityType.ARMOR_STAND.create(bqb4, bki7.getTag(), null, blv.getPlayer(), fu6, MobSpawnType.SPAWN_EGG, true, true);
        if (!bqb4.noCollision(bay8) || !bqb4.getEntities(bay8, bay8.getBoundingBox()).isEmpty()) {
            return InteractionResult.FAIL;
        }
        if (!bqb4.isClientSide) {
            final float float9 = Mth.floor((Mth.wrapDegrees(blv.getRotation() - 180.0f) + 22.5f) / 45.0f) * 45.0f;
            bay8.moveTo(bay8.getX(), bay8.getY(), bay8.getZ(), float9, 0.0f);
            this.randomizePose(bay8, bqb4.random);
            bqb4.addFreshEntity(bay8);
            bqb4.playSound(null, bay8.getX(), bay8.getY(), bay8.getZ(), SoundEvents.ARMOR_STAND_PLACE, SoundSource.BLOCKS, 0.75f, 0.8f);
        }
        bki7.shrink(1);
        return InteractionResult.sidedSuccess(bqb4.isClientSide);
    }
    
    private void randomizePose(final ArmorStand bay, final Random random) {
        Rotations gn4 = bay.getHeadPose();
        float float6 = random.nextFloat() * 5.0f;
        final float float7 = random.nextFloat() * 20.0f - 10.0f;
        Rotations gn5 = new Rotations(gn4.getX() + float6, gn4.getY() + float7, gn4.getZ());
        bay.setHeadPose(gn5);
        gn4 = bay.getBodyPose();
        float6 = random.nextFloat() * 10.0f - 5.0f;
        gn5 = new Rotations(gn4.getX(), gn4.getY() + float6, gn4.getZ());
        bay.setBodyPose(gn5);
    }
}
