/*
 *  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.Direction;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.Vec3;
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.horizontal.HorizontalDoubleSeatBlock;
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 CuffBedBlock extends HorizontalDoubleSeatBlock implements IColorBlock {
    public final ColorContent color;
    private static final BlockBehaviour.Properties PROPERTIES = BlockBehaviour.Properties.of()
            .strength(2.0f)
            .explosionResistance(3.0f)
            .sound(SoundType.WOOD)
            .noOcclusion();
    public CuffBedBlock(ColorContent color) {
        super(PROPERTIES);
        this.color = color;
    }

    public ColorContent getColor() {
        return color;
    }

    @Override
    public void setupShape() {
        // 第一部分：头部分（HEAD）的碰撞箱
        VoxelShape group1 = ShapeUtil.builder()
                .addPixelBox(0.0, 3.0, 0.0, 16.0, 5.0, 16.0)   // 主体平台
                .addPixelBox(13.0, 0.0, 0.0, 16.0, 3.0, 3.0)   // 前右支柱
                .addPixelBox(0.0, 0.0, 0.0, 3.0, 3.0, 3.0)     // 前左支柱
                .addPixelBox(0.0, 5.0, 0.0, 16.0, 9.0, 9.0)    // 前半上层平台
                .addPixelBox(0.0, 5.0, 9.0, 16.0, 9.0, 16.0)   // 后半上层平台
                .build();

        // 第二部分：脚部分（FOOT）的碰撞箱
        VoxelShape group2 = ShapeUtil.builder()
                .addPixelBox(0.0, 3.0, 0.0, 16.0, 5.0, 16.0)   // 主体平台
                .addPixelBox(0.0, 0.0, 13.0, 3.0, 3.0, 16.0)   // 后左支柱
                .addPixelBox(13.0, 0.0, 13.0, 16.0, 3.0, 16.0) // 后右支柱
                .addPixelBox(0.0, 5.0, 0.0, 16.0, 9.0, 16.0)   // 上层平台
                .build();

        // 创建双部分方块的形状映射
        EnumMap<SeatPart, Map<Direction, VoxelShape>> shapeMap = new EnumMap<>(SeatPart.class);
        shapeMap.put(SeatPart.HEAD, ShapeUtil.createUniformDirectionMap(group1));
        shapeMap.put(SeatPart.FOOT, ShapeUtil.createUniformDirectionMap(group2));
        setPartMap(shapeMap);
    }

    public void updateEntityAfterFallOn(@NotNull BlockGetter level, @NotNull Entity entity) {
        if (entity.isSuppressingBounce()) {
            super.updateEntityAfterFallOn(level, entity);
        } else {
            this.bounceUp(entity);
        }

    }

    private void bounceUp(@NotNull Entity entity) {
        Vec3 vec3 = entity.getDeltaMovement();
        if (vec3.y < 0.0) {
            double d0 = entity instanceof LivingEntity ? 1.0 : 0.8;
            entity.setDeltaMovement(vec3.x, -vec3.y * 0.6600000262260437 * d0, vec3.z);
        }

    }
}
