package de.winniepat.citrus.mixins;

import de.winniepat.citrus.managers.*;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.*;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;

import java.util.UUID;

@Mixin(EntityRenderer.class)
public abstract class PlayerNametagMixin {

    @ModifyVariable(method = "renderLabelIfPresent", at = @At("HEAD"), argsOnly = true)
    private Text addRankIcon(Text text, Entity entity) {
        if (entity instanceof PlayerEntity player) {
            UUID uuid = player.getUuid();

            if (!ClientRegistrationManager.isPlayerOnlineWithCitrus(uuid)) {
                return text;
            }

            String iconChar = RankManager.getRankIcon(uuid);

            if (!iconChar.trim().isEmpty()) {
                MutableText icon = Text.literal(iconChar).setStyle(Style.EMPTY.withFont(Identifier.of("citrus", "icon_font")));

                MutableText nameWithSpace = Text.literal(" ")
                        .append(text)
                        .setStyle(Style.EMPTY.withFont(Identifier.of("minecraft", "default")));

                return icon.append(nameWithSpace);
            }
        }
        return text;
    }
}
