package net.minecraft.client.gui.components;

import net.minecraft.network.protocol.game.ClientboundBossEventPacket;
import net.minecraft.network.chat.Component;
import java.util.Iterator;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.world.BossEvent;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.google.common.collect.Maps;
import java.util.UUID;
import java.util.Map;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.gui.GuiComponent;

public class BossHealthOverlay extends GuiComponent {
    private static final ResourceLocation GUI_BARS_LOCATION;
    private final Minecraft minecraft;
    private final Map<UUID, LerpingBossEvent> events;
    
    public BossHealthOverlay(final Minecraft dlx) {
        this.events = Maps.newLinkedHashMap();
        this.minecraft = dlx;
    }
    
    public void render(final PoseStack dhl) {
        if (this.events.isEmpty()) {
            return;
        }
        final int integer3 = this.minecraft.getWindow().getGuiScaledWidth();
        int integer4 = 12;
        for (final LerpingBossEvent dnr6 : this.events.values()) {
            final int integer5 = integer3 / 2 - 91;
            final int integer6 = integer4;
            RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
            this.minecraft.getTextureManager().bind(BossHealthOverlay.GUI_BARS_LOCATION);
            this.drawBar(dhl, integer5, integer6, dnr6);
            final Component mr9 = dnr6.getName();
            final int integer7 = this.minecraft.font.width(mr9);
            final int integer8 = integer3 / 2 - integer7 / 2;
            final int integer9 = integer6 - 9;
            this.minecraft.font.drawShadow(dhl, mr9, (float)integer8, (float)integer9, 16777215);
            final int n = integer4;
            final int n2 = 10;
            this.minecraft.font.getClass();
            integer4 = n + (n2 + 9);
            if (integer4 >= this.minecraft.getWindow().getGuiScaledHeight() / 3) {
                break;
            }
        }
    }
    
    private void drawBar(final PoseStack dhl, final int integer2, final int integer3, final BossEvent amw) {
        this.blit(dhl, integer2, integer3, 0, amw.getColor().ordinal() * 5 * 2, 182, 5);
        if (amw.getOverlay() != BossEvent.BossBarOverlay.PROGRESS) {
            this.blit(dhl, integer2, integer3, 0, 80 + (amw.getOverlay().ordinal() - 1) * 5 * 2, 182, 5);
        }
        final int integer4 = (int)(amw.getPercent() * 183.0f);
        if (integer4 > 0) {
            this.blit(dhl, integer2, integer3, 0, amw.getColor().ordinal() * 5 * 2 + 5, integer4, 5);
            if (amw.getOverlay() != BossEvent.BossBarOverlay.PROGRESS) {
                this.blit(dhl, integer2, integer3, 0, 80 + (amw.getOverlay().ordinal() - 1) * 5 * 2 + 5, integer4, 5);
            }
        }
    }
    
    public void update(final ClientboundBossEventPacket ny) {
        if (ny.getOperation() == ClientboundBossEventPacket.Operation.ADD) {
            this.events.put(ny.getId(), new LerpingBossEvent(ny));
        }
        else if (ny.getOperation() == ClientboundBossEventPacket.Operation.REMOVE) {
            this.events.remove(ny.getId());
        }
        else {
            this.events.get(ny.getId()).update(ny);
        }
    }
    
    public void reset() {
        this.events.clear();
    }
    
    public boolean shouldPlayMusic() {
        if (!this.events.isEmpty()) {
            for (final BossEvent amw3 : this.events.values()) {
                if (amw3.shouldPlayBossMusic()) {
                    return true;
                }
            }
        }
        return false;
    }
    
    public boolean shouldDarkenScreen() {
        if (!this.events.isEmpty()) {
            for (final BossEvent amw3 : this.events.values()) {
                if (amw3.shouldDarkenScreen()) {
                    return true;
                }
            }
        }
        return false;
    }
    
    public boolean shouldCreateWorldFog() {
        if (!this.events.isEmpty()) {
            for (final BossEvent amw3 : this.events.values()) {
                if (amw3.shouldCreateWorldFog()) {
                    return true;
                }
            }
        }
        return false;
    }
    
    static {
        GUI_BARS_LOCATION = new ResourceLocation("textures/gui/bars.png");
    }
}
