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

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.player.PlayerRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.block.ISeatBlock;
import top.r3944realms.eroticdungeongame.content.util.FurnitureHelper;
import top.r3944realms.eroticdungeongame.core.device.ISeatType;

public class RendererUtil {
    public static boolean enabled = true;
    public static <T extends LivingEntity> boolean warpRenderer(BlockPos seatPos, PlayerRenderer instance, @NotNull T entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight, Operation<Void> original) {
        if (enabled) {
            BlockState blockState = entity.level().getBlockState(seatPos);
            if (blockState.getBlock() instanceof ISeatBlock seatBlock) {
                Direction direction = blockState.getValue(ISeatBlock.FACING);
                ISeatType ISeatType = FurnitureHelper.getSeatType(blockState.getBlock());
                if (ISeatType != null) {
                    translate(ISeatType, direction, poseStack);
                }
            }
            original.call(instance, entity, entityYaw, partialTicks, poseStack, buffer, packedLight);
        }
        return enabled;
    }

    /**
     * 根据方块朝向旋转向量
     */
    public static Vec3 rotateVectorByFacing(Vec3 vector, @NotNull Direction facing) {
        return switch (facing) {
            case SOUTH -> new Vec3(-vector.x, vector.y, -vector.z);
            case WEST -> new Vec3(-vector.z, vector.y, vector.x);
            case EAST -> new Vec3(vector.z, vector.y, -vector.x);
            case UP -> new Vec3(vector.x, vector.z, -vector.y);
            case DOWN -> new Vec3(vector.x, -vector.z, vector.y);
            default -> vector; // NORTH
        };
    }

//    switch (seatType) {
//        case CRUX, PETRINE_CROSS, X_CROSS, WALL_RACK, CUFF_POLE, PRONE_BENCH -> new Vec3(0, 0, -0.2);
//        case SPANISH_DONKEY -> new Vec3(-0.25, 0, 0);
//        case HANGING_POST -> new Vec3(0, 0, -0.1);
//        default -> new Vec3(0, 0, 0);
//    };
    private static void translate(ISeatType iSeatType, Direction direction, PoseStack poseStack) {
        Vec3 vec3 = iSeatType.getFirstModelOffset();
        Vec3 translate = rotateVectorByFacing(vec3, direction);
        poseStack.translate(direction.get3DDataValue() >= 4 ? -translate.x : translate.x, translate.y, direction.get3DDataValue() >= 4 ? -translate.z : translate.z);
    }

    public static boolean shouldIgnoreBlock(@NotNull BlockState blockState) {
        return blockState.getBlock() instanceof ISeatBlock;
    }

}
