/*
 *  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.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.block.IColorBlock;
import top.r3944realms.eroticdungeongame.content.block.multiply.vertical.VerticalDoubleSeatBlock;
import top.r3944realms.eroticdungeongame.content.block.part.SeatPart;
import top.r3944realms.eroticdungeongame.util.ColorContent;
import top.r3944realms.lib39.util.shape.ShapeUtil;

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

public class DisplayRackBlock extends VerticalDoubleSeatBlock implements IColorBlock {
    public final ColorContent color;

    private static final BlockBehaviour.Properties PROPERTIES = BlockBehaviour.Properties.of()
            .strength(2.0f)
            .explosionResistance(3.0f)
            .sound(SoundType.ANVIL)
            .noOcclusion();

    public DisplayRackBlock(ColorContent color) {
        super(PROPERTIES);
        this.color = color;
    }

    @Override
    public ColorContent getColor() {
        return color;
    }

    @Override
    public void setupShape() {
        VoxelShape head = ShapeUtil.builder()
                .addPixelBox(7, 0, 11, 9, 7, 13)
                .addPixelBox(4, 7, 11.05, 12, 8, 13.05)
                .build();
        VoxelShape foot = ShapeUtil.builder()
                .addShape(
                        ShapeUtil.builder()
                                .addPixelBox(1, 0, 1, 15, 2, 15)
                                .addPixelBox(7, 2, 11, 9, 16, 13)
                                .addShape(
                                        ShapeUtil.builder()
                                                .addPixelBox(4, 10, 10, 12, 11, 11)
                                                .addPixelBox(4, 10, 5, 12, 11, 6)
                                                .addPixelBox(12, 10, 5, 13, 11, 11)
                                                .addPixelBox(3, 10, 5, 4, 11, 11)
                                                .build()
                                )
                                .build()
                )
                .addShape(
                        ShapeUtil.builder()
                                .addPixelBox(4, 6, 10, 12, 7, 11)
                                .addPixelBox(4, 6, 5, 12, 7, 6)
                                .addPixelBox(12, 6, 5, 13, 7, 11)
                                .addPixelBox(3, 6, 5, 4, 7, 11)
                                .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 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;
    }
}
