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

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.core.Direction;
import net.minecraft.world.phys.Vec3;
import software.bernie.geckolib.cache.object.BakedGeoModel;
import software.bernie.geckolib.renderer.GeoBlockRenderer;
import top.r3944realms.eroticdungeongame.client.model.block.LoveMachineBlockModel;
import top.r3944realms.eroticdungeongame.content.block.blockentity.LoveMachineBlockEntity;
import top.r3944realms.eroticdungeongame.content.block.type.LoveMachineBlock;

public class LoveMachineBlockRenderer extends GeoBlockRenderer<LoveMachineBlockEntity> {

    public LoveMachineBlockRenderer(String type) {
        super(new LoveMachineBlockModel(type));
    }
    /**
     * 重写 preRender 方法，在渲染前应用偏移
     */
    @Override
    public void preRender(PoseStack poseStack, LoveMachineBlockEntity animatable, BakedGeoModel model,
                          MultiBufferSource bufferSource, VertexConsumer buffer, boolean isReRender,
                          float partialTick, int packedLight, int packedOverlay, float red,
                          float green, float blue, float alpha) {

        // 先调用父类的preRender
        super.preRender(poseStack, animatable, model, bufferSource, buffer, isReRender,
                partialTick, packedLight, packedOverlay, red, green, blue, alpha);

        // 应用偏移变换
        if (animatable != null && animatable.isOffsetEnabled()) {
            float offsetX = animatable.getRenderOffsetX();
            float offsetY = animatable.getRenderOffsetY();
            float offsetZ = animatable.getRenderOffsetZ();
            // 在PoseStack中应用偏移
            Direction direction = animatable.getBlockState().getValue(LoveMachineBlock.FACING);
            Vec3 vec3 = LoveMachineBlock.adjustOffsetByFacing(offsetX, offsetY, offsetZ, direction);

            poseStack.translate(vec3.x, vec3.y, vec3.z);
        }
    }

    protected void rotateBlock(Direction facing, PoseStack poseStack) {
        switch (facing) {
            case SOUTH, NORTH, WEST, EAST -> super.rotateBlock(facing, poseStack);
            case UP -> {
                poseStack.mulPose(Axis.XP.rotationDegrees(90));
                poseStack.translate(0, -0.31, -0.32);
            }
            case DOWN -> {
                poseStack.mulPose(Axis.XN.rotationDegrees(90));
                poseStack.translate(0, -0.32, 0.68);
            }
        }
    }


}
