package top.r3944realms.lib39.client.renderer.item;

import com.mojang.authlib.GameProfile;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.resources.DefaultPlayerSkin;
import net.minecraft.client.resources.SkinManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.lib39.client.model.DollModel;
import top.r3944realms.lib39.content.item.DollItem;
import top.r3944realms.lib39.util.GameProfileHelper;
import top.r3944realms.lib39.util.lang.Pair;

/**
 * The type Doll item renderer.
 */
public class DollItemRenderer extends BlockEntityWithoutLevelRenderer {
    private static DollItemRenderer instance;
    private final DollModel dollModel;
    private DollItemRenderer() {
        super(
                Minecraft.getInstance().getBlockEntityRenderDispatcher(),
                Minecraft.getInstance().getEntityModels()
        );
        this.dollModel = new DollModel(
                Minecraft.getInstance().getEntityModels().bakeLayer(DollModel.LAYER_LOCATION)
        );
    }

    /**
     * Gets instance.
     *
     * @return the instance
     */
    public static DollItemRenderer getInstance() {
        if (instance == null) {
            instance = new DollItemRenderer();
        }
        return instance;
    }

    @Override
    public void renderByItem(@NotNull ItemStack stack, @NotNull ItemDisplayContext displayContext, @NotNull PoseStack poseStack, @NotNull MultiBufferSource buffer, int packedLight, int packedOverlay) {
        if (!(stack.getItem() instanceof DollItem)) {
            return;
        }
        GameProfile profile = GameProfileHelper.getProfileFromItemStack(stack);
        Pair<ResourceLocation, Boolean> resourceLocationBooleanPair = loadSkin(profile);
        ResourceLocation playerSkin = resourceLocationBooleanPair.first;
        boolean isSlim = resourceLocationBooleanPair.second;
        poseStack.pushPose();
        VertexConsumer vertexConsumer = buffer.getBuffer(
                RenderType.entityTranslucent(playerSkin)
        );

        poseStack.translate(0.5, 2.6, 0.8);
        poseStack.scale(1.8F, -1.8F, -1.8F);
        poseStack.mulPose(Axis.YP.rotationDegrees(180));
        this.dollModel.slim = isSlim;
        this.dollModel.renderToBuffer(
                poseStack,
                vertexConsumer,
                packedLight,
                packedOverlay,
                1.0F, 1.0F, 1.0F, 1.0F
        );

        poseStack.popPose();
    }

    /**
     * Load skin pair.
     *
     * @param profile the profile
     * @return the pair
     */
    public static @NotNull Pair<ResourceLocation,Boolean> loadSkin(GameProfile profile) {
        SkinManager skinManager = Minecraft.getInstance().getSkinManager();
        ResourceLocation playerSkin;
        boolean isSlim;
        if (profile != null) {
            playerSkin = skinManager.getInsecureSkinLocation(profile);
            isSlim = GameProfileHelper.isSlimArms(profile);
        } else {
            playerSkin = DefaultPlayerSkin.getDefaultSkin(); //6  new SkinType("textures/entity/player/slim/steve.png", DefaultPlayerSkin.ModelType.SLIM),
            isSlim = true;
        }
        return Pair.of(playerSkin, isSlim);
    }
}
