package me.km.networking;

import com.mojang.blaze3d.systems.RenderSystem;
import java.util.TreeMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.network.play.NetworkPlayerInfo;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class PlayerHeadGui extends AbstractGui {
    public final static PlayerHeadGui INSTANCE = new PlayerHeadGui(Minecraft.getInstance());

    private class HeadData {
        private final int x;
        private final int y;
        private final int scale;
        private final ResourceLocation rl;

        public HeadData(int x, int y, int scale, ResourceLocation rl) {
            this.x = x;
            this.y = y;
            this.scale = scale;
            this.rl = rl;
        }

        public void paint(Minecraft mc) {
            RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
            mc.getTextureManager().bindTexture(rl);
            AbstractGui.blit(x, y, 8 * scale, 8 * scale, 8.0f, 8.0f, 8, 8, 64, 64);
            AbstractGui.blit(x, y, 8 * scale, 8 * scale, 40.0f, 8.0f, 8, 8, 64, 64);
        }
    }

    private final TreeMap<Integer, HeadData> data;
    private final Minecraft mc;

    public PlayerHeadGui(Minecraft mc) {
        this.mc = mc;
        this.data = new TreeMap<>();
    }

    public void add(int i, int x, int y, int scale, String name) {
        NetworkPlayerInfo info = this.mc.player.connection.getPlayerInfo(name);
        if(info != null) {
            data.put(i, new HeadData(x, y, scale, info.getLocationSkin()));
        }
    }

    public void remove(int i) {
        data.remove(i);
    }

    public void clear() {
        data.clear();
    }

    public void paint() {
        data.values().forEach(head -> head.paint(mc));
    }
}
