package de.winniepat.citrus.mixins;

import de.winniepat.citrus.managers.*;
import net.minecraft.client.gui.hud.PlayerListHud;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.text.*;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.UUID;

@Mixin(PlayerListHud.class)
public abstract class TabListMixin {

    @Inject(method = "getPlayerName", at = @At("RETURN"), cancellable = true)
    private void addRankIconToTab(PlayerListEntry entry, CallbackInfoReturnable<Text> cir) {
        UUID uuid = entry.getProfile().getId();

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

        String iconChar = RankManager.getRankIcon(uuid);

        if (!iconChar.trim().isEmpty()) {
            Text currentName = cir.getReturnValue() != null ? cir.getReturnValue() : Text.literal(entry.getProfile().getName());

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

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

            cir.setReturnValue(icon.append(nameWithSpace));
        }
    }
}
