/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.content.block.type;


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.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.content.block.ISeatBlock;
import top.r3944realms.eroticdungeongame.content.block.blockentity.IronCageBlockEntity;
import top.r3944realms.eroticdungeongame.content.block.multiply.vertical.VerticalDoubleSeatBlock;
import top.r3944realms.eroticdungeongame.content.block.part.SeatPart;
import top.r3944realms.eroticdungeongame.core.register.EDGBlockEntities;
import top.r3944realms.lib39.util.shape.ShapeUtil;

import java.util.EnumMap;
import java.util.Map;

public class IronCageBlock extends VerticalDoubleSeatBlock {
    private static final BlockBehaviour.Properties PROPERTIES =
            BlockBehaviour.Properties.copy(Blocks.DIRT)
            .noOcclusion()
            .sound(SoundType.ANVIL)
            .isViewBlocking((state, level, pos) -> false)
            .isSuffocating((state, level, pos) -> false);
    public IronCageBlock() {
        super(PROPERTIES);
    }

    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(@NotNull Level pLevel, @NotNull BlockState pState, @NotNull BlockEntityType<T> pBlockEntityType) {
        return pLevel.isClientSide() ? null : ISeatBlock.createTickerHelper(pBlockEntityType, EDGBlockEntities.IRON_CAGE_SEAT_BLOCK_ENTITY.get(), IronCageBlockEntity::serverTick);
    }

    @Override
    public boolean canSurvive(@NotNull BlockState state, @NotNull LevelReader level, @NotNull BlockPos pos) {
        // 检查上方一格是否有空间
        BlockPos abovePos = pos.above();
        BlockState aboveState = level.getBlockState(abovePos);
        return aboveState.canBeReplaced();
    }

    @Override
    public void setupShape() {
        VoxelShape voxelShape = ShapeUtil.builder()
                .addPixelBox(0, 0, 0, 16, 16, 16)
                .build();
        Map<Direction, VoxelShape> uniformDirectionMap = ShapeUtil.createUniformDirectionMap(voxelShape);
        EnumMap<SeatPart, Map<Direction, VoxelShape>> shapes = new EnumMap<>(SeatPart.class);
        shapes.put(SeatPart.FOOT, uniformDirectionMap);
        shapes.put(SeatPart.HEAD, uniformDirectionMap);
        setPartMap(shapes);
    }

    @Override
    public @NotNull InteractionResult useOnPrimaryPart(@NotNull BlockState useBlockState, @NotNull Level level, @NotNull BlockPos primaryBlockPos, @NotNull Player player, @NotNull InteractionHand hand, @NotNull BlockHitResult hitResult) {
        if(hand == InteractionHand.OFF_HAND) return InteractionResult.SUCCESS;
        BlockEntity blockEntity = getBlockEntity(useBlockState, level, primaryBlockPos);
        if (player.isShiftKeyDown() && blockEntity instanceof IronCageBlockEntity ironCageBlockEntity) {
            ironCageBlockEntity.toggleLock();
            ironCageBlockEntity.setChanged();
        }
        return super.useOnPrimaryPart(useBlockState, level, primaryBlockPos, player, hand, hitResult);
    }

    @SuppressWarnings("deprecation")
    @Override
    public @NotNull RenderShape getRenderShape(@NotNull BlockState state) {
        return RenderShape.ENTITYBLOCK_ANIMATED;
    }

    @Override
    @Nullable
    public BlockEntity createPrimaryBlockEntity(@NotNull BlockPos blockPos, @NotNull BlockState blockState) {
        return EDGBlockEntities.IRON_CAGE_SEAT_BLOCK_ENTITY.get().create(blockPos, blockState);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onPlace(@NotNull BlockState state, Level level, @NotNull BlockPos pos, @NotNull BlockState oldState, boolean movedByPiston) {
        if (!level.isClientSide) {
            return;
        }
        BlockEntity blockEntity = level.getBlockEntity(pos);
        if (blockEntity instanceof IronCageBlockEntity ironCageBlockEntity) {
            ironCageBlockEntity.setShouldPlayLocking(false);
            ironCageBlockEntity.setShouldPlayUnlocking(false);
            ironCageBlockEntity.setChanged();
        }
    }
}
