/*
 *  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.util;

import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.core.device.ISeatType;

public class BoundingBoxCalculator {

    /**
     * 将预设AABB移动到玩家当前位置
     *
     * @param presetBB 预设的碰撞箱（相对于原点）
     * @param playerPos 玩家的当前位置
     * @return 移动到玩家位置的碰撞箱
     */
    public static @NotNull AABB positionBoundingBox(@NotNull AABB presetBB, @NotNull Vec3 playerPos) {
        // 计算预设碰撞箱的中心点
        Vec3 center = presetBB.getCenter();

        // 将碰撞箱移动到玩家位置，保持相对形状
        return presetBB.move(
                playerPos.x - center.x,
                playerPos.y - presetBB.minY, // Y轴以底部为基准
                playerPos.z - center.z
        );
    }

    /**
     * 更精确的位置计算（考虑玩家脚部位置）
     *
     * @param presetBB 预设碰撞箱
     * @param playerPos 玩家位置（脚部坐标）
     * @param eyeHeight 视线高度（用于调整）
     * @return 定位后的碰撞箱
     */
    public static @NotNull AABB positionBoundingBoxWithEyeHeight(
            @NotNull AABB presetBB,
            @NotNull Vec3 playerPos,
            float eyeHeight) {

        // 获取预设碰撞箱的高度
        double bbHeight = presetBB.getYsize();

        // 计算需要调整的Y轴偏移
        // 确保玩家的眼睛在正确高度
        double eyeOffset = eyeHeight - (bbHeight * 0.9); // 90%高度处为眼睛位置

        return presetBB.move(
                playerPos.x - presetBB.getCenter().x,
                playerPos.y + eyeOffset,
                playerPos.z - presetBB.getCenter().z
        );
    }

    /**
     * 根据玩家位置和朝向调整碰撞箱
     */
    public static @NotNull AABB adjustBoundingBoxForPosition(
            @NotNull AABB presetBB,
            @NotNull Vec3 playerPos,
            ISeatType ISeatType) {

        AABB positionedBB = positionBoundingBox(presetBB, playerPos);

        // 对于某些座位类型，可能需要根据朝向微调
        if (ISeatType.isOppositeDirection()) {
            // 如果需要反向，调整Z轴
            positionedBB = positionedBB.move(0, 0, 0.2); // 微调
        }

        return positionedBB;
    }

    /**
     * 简化方法：直接计算
     */
    public static @NotNull AABB getPlayerBoundingBoxForSeat(
            @NotNull Vec3 playerPos,
            @NotNull ISeatType ISeatType) {

        AABB presetBB = ISeatType.getPlayerBB();
        if (presetBB == null) {
            // 返回默认站立碰撞箱
            return new AABB(
                    playerPos.x - 0.3, playerPos.y,
                    playerPos.z - 0.3,
                    playerPos.x + 0.3, playerPos.y + 1.8,
                    playerPos.z + 0.3
            );
        }

        return positionBoundingBox(presetBB, playerPos);
    }
}