package top.r3944realms.eroticdungeongame.content.block;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.block.blockentity.SeatBlockEntity;
import top.r3944realms.eroticdungeongame.content.util.FurnitureHelper;

import java.util.Map;

@SuppressWarnings({"deprecation", "unused"})
public abstract class AbstractSeatBlock extends Block implements EntityBlock {
    public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
    public static final BooleanProperty OCCUPIED = BooleanProperty.create("occupied");
    public static final BooleanProperty FLIPPED = BooleanProperty.create("flipped");

    /* renamed from: shapeMap */
    protected Map<Direction, VoxelShape> shapeMap;

    public AbstractSeatBlock(BlockBehaviour.Properties properties) {
        super(properties);
        registerDefaultState(defaultBlockState()
                .setValue(FACING, Direction.NORTH)
                .setValue(OCCUPIED, false)
                .setValue(FLIPPED, false));
    }

    /**
     * 抽象方法：子类必须实现来设置碰撞箱形状
     */
    public abstract void setupShape();

    @Override
    public BlockEntity newBlockEntity(@NotNull BlockPos blockPos, @NotNull BlockState blockState) {
        return new SeatBlockEntity(blockPos, blockState);
    }

    @Override
    public @NotNull BlockState rotate(@NotNull BlockState blockState, @NotNull Rotation rotation) {
        return blockState;
    }

    @Override
    public @NotNull BlockState mirror(@NotNull BlockState blockState, @NotNull Mirror mirror) {
        return blockState;
    }

    @Override
    public @NotNull VoxelShape getShape(@NotNull BlockState blockState, @NotNull BlockGetter blockGetter,
                               @NotNull BlockPos blockPos, @NotNull CollisionContext collisionContext) {
        return this.shapeMap.get(blockState.getValue(FACING));
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(FACING, OCCUPIED, FLIPPED);
    }

    @Override
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return defaultBlockState()
                .setValue(FACING, context.getHorizontalDirection().getOpposite())
                .setValue(OCCUPIED, false)
                .setValue(FLIPPED, false);
    }

    @Override
    public PushReaction getPistonPushReaction(@NotNull BlockState blockState) {
        return PushReaction.DESTROY;
    }

    @Override
    public boolean useShapeForLightOcclusion(@NotNull BlockState blockState) {
        return true;
    }

    @Override
    public int getLightEmission(BlockState state, BlockGetter level, BlockPos pos) {
        return state.getValue(OCCUPIED) ? 15 : 0;
    }


    @Override
    public boolean isSignalSource(@NotNull BlockState blockState) {
        return true;
    }

    @Override
    public int getSignal(@NotNull BlockState blockState, @NotNull BlockGetter blockGetter,
                         @NotNull BlockPos blockPos, @NotNull Direction direction) {
        return blockState.getValue(OCCUPIED) ? 15 : 0;
    }

    @Override
    @NotNull
    public InteractionResult use(@NotNull BlockState blockState, @NotNull Level level,
                                 @NotNull BlockPos blockPos, @NotNull Player player,
                                 @NotNull InteractionHand hand, @NotNull BlockHitResult hitResult) {
        // 客户端或副手直接返回
        if (level.isClientSide || hand == InteractionHand.OFF_HAND) {
            return InteractionResult.SUCCESS;
        }

        SeatBlockEntity deviceEntity = FurnitureHelper.getSeatBlockEntity(level, blockPos);
        if (deviceEntity != null) {
            if (blockState.getValue(OCCUPIED)) {
                // 设备已被占用 - 释放玩家
                if (FurnitureHelper.isRestraintDevice(blockState.getBlock())) {
                    Player boundPlayer = level.getPlayerByUUID(deviceEntity.getBoundPlayerUUID());
                    if (boundPlayer != null) {
                        deviceEntity.flipSeatDirection(level, blockPos, blockState);
                        return InteractionResult.SUCCESS;
                    }
                }
            } else {
                // 设备空闲 - 绑定玩家
                if (!player.isShiftKeyDown()) {
                    deviceEntity.bindPlayerToSeat(level, blockPos, blockState, player);
                    return InteractionResult.SUCCESS;
                }
            }
        }
        return InteractionResult.SUCCESS;
    }

    @Override
    public void onRemove(@NotNull BlockState blockState, Level level, @NotNull BlockPos blockPos,
                         @NotNull BlockState newState, boolean moved) {
        if (!level.isClientSide && blockState.getBlock() != newState.getBlock()) {
            SeatBlockEntity deviceEntity = FurnitureHelper.getSeatBlockEntity(level, blockPos);
            if (deviceEntity != null && blockState.getValue(OCCUPIED)) {
                Player boundPlayer = level.getPlayerByUUID(deviceEntity.getBoundPlayerUUID());
                if (boundPlayer != null) {
                    FurnitureHelper.releasePlayerFromDevice(boundPlayer);
                }
            }
        }
        super.onRemove(blockState, level, blockPos, newState, moved);
    }

    @Override
    @NotNull
    public RenderShape getRenderShape(@NotNull BlockState blockState) {
        return RenderShape.MODEL;
    }
}
