/*
 *  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 CuffPoleBlock extends VerticalDoubleSeatBlock implements IWoodTypeBlock {
    private final WoodType woodType;
    public CuffPoleBlock(WoodType woodType) {
        super(PROPERTIES);
        this.woodType = woodType;
    }

    @Override
    public void setupShape() {
        VoxelShape head = ShapeUtil.builder()
                .addShape(
                        ShapeUtil.builder()
                                .addPixelBox(8, 0, 6, 10, 16, 8)
                                .addPixelBox(6, 0, 8, 8, 16, 10)
                                .addPixelBox(8, 0, 8, 10, 16, 10)
                                .addPixelBox(6, 0, 6, 8, 16, 8)
                                .build()
                )
                .addShape(
                        ShapeUtil.builder()
                                .addPixelBox(1, 13.5, 10, 15, 15, 11)
                                .addPixelBox(1, 10.5, 10, 2.5, 13.5, 11)
                                .addPixelBox(1, 9, 10, 15, 10.5, 11)
                                .addPixelBox(5.5, 10.5, 10, 10.5, 13.5, 11)
                                .addPixelBox(13.5, 10.5, 10, 15, 13.5, 11)
                                .build()
                )
                .build();
        VoxelShape foot = ShapeUtil.builder()
                .addShape(
                        ShapeUtil.builder()
                                .addPixelBox(1, 6.5, 10, 15, 8, 11)
                                .addPixelBox(1, 3.5, 10, 2.5, 6.5, 11)
                                .addPixelBox(1, 2, 10, 15, 3.5, 11)
                                .addPixelBox(5.5, 3.5, 10, 10.5, 6.5, 11)
                                .addPixelBox(13.5, 3.5, 10, 15, 6.5, 11)
                                .build()
                )
                .addShape(
                        ShapeUtil.builder()
                                .addPixelBox(8, 0, 6, 10, 16, 8)
                                .addPixelBox(6, 0, 8, 8, 16, 10)
                                .addPixelBox(8, 0, 8, 10, 16, 10)
                                .addPixelBox(6, 0, 6, 8, 16, 8)
                                .build()
                )
                .build();
        EnumMap<SeatPart, Map<Direction, VoxelShape>> shapes = new EnumMap<>(SeatPart.class);
        shapes.put(SeatPart.FOOT, ShapeUtil.createUniformDirectionMap(foot));
        shapes.put(SeatPart.HEAD, ShapeUtil.createUniformDirectionMap(head));
        setPartMap(shapes);
    }

    @Override
    public WoodType getWoodType() {
        return woodType;
    }

    @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;
    }
}
