/*
 *  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.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.block.IWoodTypeBlock;
import top.r3944realms.eroticdungeongame.content.block.multiply.vertical.VerticalDoubleSeatBlock;
import top.r3944realms.eroticdungeongame.content.block.part.SeatPart;
import top.r3944realms.lib39.util.shape.ShapeUtil;

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

public class XCrossBlock extends VerticalDoubleSeatBlock implements IWoodTypeBlock {
    private final WoodType woodType;

    public XCrossBlock(WoodType woodType) {
        super(PROPERTIES);
        this.woodType = woodType;
    }

    public WoodType getWoodType() {
        return woodType;
    }

    @Override
    public void setupShape() {
        VoxelShape voxelShape = ShapeUtil.builder()
                .addPixelBox(-2, 0, 14, 18, 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 BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
        BlockPos pos = context.getClickedPos();
        BlockPos abovePos = pos.above();
        Level level = context.getLevel();
        Direction direction = context.getHorizontalDirection().getOpposite();

        // 检查上方位置是否可以放置
        if (level.getBlockState(abovePos).canBeReplaced(context) &&
                level.getWorldBorder().isWithinBounds(abovePos)) {

            return setPrimaryStatePart(
                    this.defaultBlockState()
                            .setValue(FACING, direction)
            );
        }
        return null;
    }
}
