/*
 * 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.math.Axis;
import com.remains_cave.block.EmblemBlock;
import com.remains_cave.block.entity.EmblemBlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;

public class EmblemRenderer implements BlockEntityRenderer<EmblemBlockEntity> {
    private static final float ITEM_HEIGHT = 1.5F;
    private static final float FLOAT_AMPLITUDE = 0.05F;
    private static final float ROTATION_SPEED = 5.0F;
    private static final float AURA_SCALE = 1.5F; // 根据需要调整大小

    private BakedModel auraModel;

    public EmblemRenderer(BlockEntityRendererProvider.Context context) {
        ModelManager modelManager = Minecraft.getInstance().getModelManager();
        ResourceLocation modelLocation = ResourceLocation.parse("remains_cave:block/kings_aura");
        ModelResourceLocation modelResourceLocation = ModelResourceLocation.standalone(modelLocation);
        auraModel = modelManager.getModel(modelResourceLocation);
    }

    @Override
    public void render(EmblemBlockEntity emblem, float partialTick, PoseStack poseStack,
                       MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
        if (auraModel == null) return;

        BlockState state = emblem.getBlockState();
        boolean active = state.getValue(EmblemBlock.ACTIVE);

        poseStack.pushPose();
        poseStack.translate(0.5, ITEM_HEIGHT, 0.5);

        long gameTime = emblem.getLevel() != null ? emblem.getLevel().getGameTime() : 0;

        if (active) {
            // 启动状态：上下浮动 + 旋转
            float yOffset = (float) Math.sin((gameTime + partialTick) * 0.1) * FLOAT_AMPLITUDE;
            poseStack.translate(0, yOffset, 0);
            float angle = (gameTime + partialTick) * ROTATION_SPEED;
            poseStack.mulPose(Axis.YP.rotationDegrees(angle));
        } else {
            // 未启动状态：向下平移 8 个单位
            poseStack.translate(0, -8.0 / 16.0, 0); // 8 个像素 = 0.5 格
        }

        poseStack.scale(AURA_SCALE, AURA_SCALE, AURA_SCALE);

        var dispatcher = Minecraft.getInstance().getBlockRenderer();
        var consumer = bufferSource.getBuffer(RenderType.cutout());
        dispatcher.getModelRenderer().renderModel(
                poseStack.last(), consumer, null, auraModel, 1.0F, 1.0F, 1.0F, packedLight, packedOverlay
        );

        poseStack.popPose();
    }
}