package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.entity.JigsawBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.core.Direction;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.core.FrontAndTop;
import net.minecraft.world.level.block.state.properties.EnumProperty;

public class JigsawBlock extends Block implements EntityBlock {
    public static final EnumProperty<FrontAndTop> ORIENTATION;
    
    protected JigsawBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<FrontAndTop, FrontAndTop>setValue(JigsawBlock.ORIENTATION, FrontAndTop.NORTH_UP));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(JigsawBlock.ORIENTATION);
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<FrontAndTop, FrontAndTop>setValue(JigsawBlock.ORIENTATION, cap.rotation().rotate(cfj.<FrontAndTop>getValue(JigsawBlock.ORIENTATION)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return ((StateHolder<O, BlockState>)cfj).<FrontAndTop, FrontAndTop>setValue(JigsawBlock.ORIENTATION, bzj.rotation().rotate(cfj.<FrontAndTop>getValue(JigsawBlock.ORIENTATION)));
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final Direction fz3 = bin.getClickedFace();
        Direction fz4;
        if (fz3.getAxis() == Direction.Axis.Y) {
            fz4 = bin.getHorizontalDirection().getOpposite();
        }
        else {
            fz4 = Direction.UP;
        }
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<FrontAndTop, FrontAndTop>setValue(JigsawBlock.ORIENTATION, FrontAndTop.fromFrontAndTop(fz3, fz4));
    }
    
    @Nullable
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new JigsawBlockEntity();
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        final BlockEntity cdl8 = bqb.getBlockEntity(fu);
        if (cdl8 instanceof JigsawBlockEntity && bec.canUseGameMasterBlocks()) {
            bec.openJigsawBlock((JigsawBlockEntity)cdl8);
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        return InteractionResult.PASS;
    }
    
    public static boolean canAttach(final StructureTemplate.StructureBlockInfo c1, final StructureTemplate.StructureBlockInfo c2) {
        final Direction fz3 = getFrontFacing(c1.state);
        final Direction fz4 = getFrontFacing(c2.state);
        final Direction fz5 = getTopFacing(c1.state);
        final Direction fz6 = getTopFacing(c2.state);
        final JigsawBlockEntity.JointType a7 = JigsawBlockEntity.JointType.byName(c1.nbt.getString("joint")).orElseGet(() -> fz3.getAxis().isHorizontal() ? JigsawBlockEntity.JointType.ALIGNED : JigsawBlockEntity.JointType.ROLLABLE);
        final boolean boolean8 = a7 == JigsawBlockEntity.JointType.ROLLABLE;
        return fz3 == fz4.getOpposite() && (boolean8 || fz5 == fz6) && c1.nbt.getString("target").equals(c2.nbt.getString("name"));
    }
    
    public static Direction getFrontFacing(final BlockState cfj) {
        return cfj.<FrontAndTop>getValue(JigsawBlock.ORIENTATION).front();
    }
    
    public static Direction getTopFacing(final BlockState cfj) {
        return cfj.<FrontAndTop>getValue(JigsawBlock.ORIENTATION).top();
    }
    
    static {
        ORIENTATION = BlockStateProperties.ORIENTATION;
    }
}
