package top.r3944realms.lib39.content.block;

import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.RotationSegment;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.lib39.content.block.property.DollPose;

/**
 * The type Doll block.
 */
@SuppressWarnings("deprecation")
public class DollBlock extends AbstractDollBlock{
    /**
     * The constant MAX.
     */
    public static final int MAX = RotationSegment.getMaxSegmentIndex();

    private static final int ROTATIONS = MAX + 1;
    /**
     * The constant ROTATION.
     */
    public static final IntegerProperty ROTATION = BlockStateProperties.ROTATION_16;

    /**
     * Instantiates a new Doll block.
     */
    public DollBlock() {
        super();
        this.registerDefaultState(
                this.stateDefinition.any()
                        .setValue(POSE, DollPose.DEFAULT)
                        .setValue(WATERLOGGED, false)
                        .setValue(ROTATION, 0)
        );
    }
    @Override
    public @Nullable BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
        BlockState stateForPlacement = super.getStateForPlacement(context);
        return stateForPlacement != null ? stateForPlacement.setValue(ROTATION, RotationSegment.convertToSegment((context.getRotation()+180) % 360)) : null;
    }

    @Override
    public @NotNull BlockState rotate(@NotNull BlockState state, @NotNull Rotation rotation) {
        return state.setValue(ROTATION, rotation.rotate(state.getValue(ROTATION), ROTATIONS));
    }

    @Override
    public @NotNull BlockState mirror(@NotNull BlockState state, @NotNull Mirror mirror) {
        return state.setValue(ROTATION, mirror.mirror(state.getValue(ROTATION), ROTATIONS));
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.@NotNull Builder<Block, BlockState> builder) {
        super.createBlockStateDefinition(builder);
        builder.add(ROTATION);
    }
}
