package xyz.illuc.neuralgiacore.entities;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.SkullBlockEntity;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class OfflinePlayerRenderer extends LivingEntityRenderer<OfflinePlayerEntity, PlayerModel<OfflinePlayerEntity>> {

    public OfflinePlayerRenderer(EntityRendererProvider.Context context) {
        super(context, new PlayerModel<>(context.bakeLayer(ModelLayers.PLAYER), false), 0.5F);
    }

    private static HashMap<String, GameProfile> players = new HashMap<>();

    @Override
    public ResourceLocation getTextureLocation(OfflinePlayerEntity offlinePlayerEntity) {
        UUID uuid = offlinePlayerEntity.getPlayerUUID().orElse(new UUID(0L, 0L));
        String name = offlinePlayerEntity.getPlayerName();

        GameProfile gameProfile;
        if (players.containsKey(uuid.toString())) {
            gameProfile = players.get(uuid.toString());
        } else {
            gameProfile = new GameProfile(uuid, name);

            players.put(uuid.toString(), gameProfile);
            SkullBlockEntity.updateGameprofile(gameProfile, profile -> {
                players.put(uuid.toString(), profile);
            });
        }


        ResourceLocation skinLocation = Minecraft.getInstance().getSkinManager().getInsecureSkinLocation(gameProfile);

        //System.out.println("UUID: " + uuid + " NAME: " + name + " SKIN: " + skinLocation);

        return skinLocation;
    }

    @Override
    public void render(OfflinePlayerEntity entity, float entityYaw, float partialTicks,
                       PoseStack matrixStack, MultiBufferSource buffer, int packedLightIn) {
        super.render(entity, entityYaw, partialTicks, matrixStack, buffer, packedLightIn);
    }
}