/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.client.render.block;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import com.remains_cave.block.TrophyBlock;
import com.remains_cave.block.entity.TrophyBlockEntity;
import com.remains_cave.client.model.TrophyModel;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib.cache.object.BakedGeoModel;
import software.bernie.geckolib.renderer.GeoBlockRenderer;

public class TrophyRenderer extends GeoBlockRenderer<TrophyBlockEntity> {
    public TrophyRenderer(BlockEntityRendererProvider.Context context) {
        super(new TrophyModel());
    }

    @Override
    public RenderType getRenderType(TrophyBlockEntity animatable, ResourceLocation texture,
                                    MultiBufferSource bufferSource, float partialTick) {
        return RenderType.entityCutout(texture);
    }

    @Override
    public void render(@NotNull TrophyBlockEntity blockEntity, float partialTick, @NotNull PoseStack poseStack,
                       @NotNull MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
        super.render(blockEntity, partialTick, poseStack, bufferSource, packedLight, packedOverlay);
    }

    @Override
    protected void rotateBlock(Direction facing, PoseStack poseStack) {
        TrophyBlockEntity blockEntity = getAnimatable();
        BlockState state = blockEntity.getBlockState();
        int rotation = state.getValue(TrophyBlock.ROTATION);

        if (facing == Direction.UP) {
            // 地面放置：直接用 rotation 决定绕 Y 轴的旋转角度
            poseStack.mulPose(Axis.YP.rotationDegrees(rotation * 22.5f));
        } else if (facing.getAxis().isHorizontal()) {
            // 墙上放置：根据墙面方向旋转模型，使正面朝外
            float yRot = switch (facing) {
                case NORTH -> 180f;
                case SOUTH -> 0f;
                case WEST -> 270f;
                case EAST -> 90f;
                default -> 0f;
            };
            poseStack.mulPose(Axis.YP.rotationDegrees(yRot));
        }
    }

    @Override
    public void preRender(PoseStack poseStack, TrophyBlockEntity animatable, BakedGeoModel model,
                          MultiBufferSource bufferSource, VertexConsumer buffer, boolean isReRender,
                          float partialTick, int packedLight, int packedOverlay, int colour) {
        super.preRender(poseStack, animatable, model, bufferSource, buffer, isReRender,
                partialTick, packedLight, packedOverlay, colour);

        BlockState state = animatable.getBlockState();
        Direction facing = state.getValue(TrophyBlock.FACING);

        if (facing.getAxis().isHorizontal()) {
            // 根据墙面方向移动模型，使其贴合墙面中央
            float offset = 0.2875f; // 移动半格，使模型中心贴到墙面
            switch (facing) {
                case NORTH -> poseStack.translate(0, 0, -offset);
                case SOUTH -> poseStack.translate(0, 0, offset);
                case WEST -> poseStack.translate(-offset, 0, 0);
                case EAST -> poseStack.translate(offset, 0, 0);
                default -> {}
            }
            poseStack.translate(0, 0.25, 0);
        }
    }
}