package net.minecraft.client.gui.screens;

import net.minecraft.network.chat.ClickEvent;
import net.minecraft.client.gui.components.ComponentRenderUtils;
import net.minecraft.ChatFormatting;
import com.mojang.blaze3d.systems.RenderSystem;
import java.util.Iterator;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.TranslatableComponent;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;

public class DeathScreen extends Screen {
    private int delayTicker;
    private final Component causeOfDeath;
    private final boolean hardcore;
    
    public DeathScreen(@Nullable final Component lf, final boolean boolean2) {
        super(new TranslatableComponent(boolean2 ? "deathScreen.title.hardcore" : "deathScreen.title", new Object[0]));
        this.causeOfDeath = lf;
        this.hardcore = boolean2;
    }
    
    @Override
    protected void init() {
        this.delayTicker = 0;
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 72, 200, 20, this.hardcore ? I18n.get("deathScreen.spectate") : I18n.get("deathScreen.respawn"), dcv -> {
            this.minecraft.player.respawn();
            this.minecraft.setScreen(null);
            return;
        }));
        final ConfirmScreen confirmScreen;
        ConfirmScreen dfc3;
        final Button dcv2 = this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 96, 200, 20, I18n.get("deathScreen.titleScreen"), dcv -> {
            if (this.hardcore) {
                this.exitToTitleScreen();
                return;
            }
            else {
                new ConfirmScreen(this::confirmResult, new TranslatableComponent("deathScreen.quit.confirm", new Object[0]), new TextComponent(""), I18n.get("deathScreen.titleScreen"), I18n.get("deathScreen.respawn"));
                dfc3 = confirmScreen;
                this.minecraft.setScreen(dfc3);
                dfc3.setDelay(20);
                return;
            }
        }));
        if (!this.hardcore && this.minecraft.getUser() == null) {
            dcv2.active = false;
        }
        for (final AbstractWidget dct4 : this.buttons) {
            dct4.active = false;
        }
    }
    
    @Override
    public boolean shouldCloseOnEsc() {
        return false;
    }
    
    private void confirmResult(final boolean boolean1) {
        if (boolean1) {
            this.exitToTitleScreen();
        }
        else {
            this.minecraft.player.respawn();
            this.minecraft.setScreen(null);
        }
    }
    
    private void exitToTitleScreen() {
        if (this.minecraft.level != null) {
            this.minecraft.level.disconnect();
        }
        this.minecraft.clearLevel(new GenericDirtMessageScreen(new TranslatableComponent("menu.savingLevel", new Object[0])));
        this.minecraft.setScreen(new TitleScreen());
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        this.fillGradient(0, 0, this.width, this.height, 1615855616, -1602211792);
        RenderSystem.pushMatrix();
        RenderSystem.scalef(2.0f, 2.0f, 2.0f);
        this.drawCenteredString(this.font, this.title.getColoredString(), this.width / 2 / 2, 30, 16777215);
        RenderSystem.popMatrix();
        if (this.causeOfDeath != null) {
            this.drawCenteredString(this.font, this.causeOfDeath.getColoredString(), this.width / 2, 85, 16777215);
        }
        this.drawCenteredString(this.font, I18n.get("deathScreen.score") + ": " + ChatFormatting.YELLOW + this.minecraft.player.getScore(), this.width / 2, 100, 16777215);
        if (this.causeOfDeath != null && integer2 > 85) {
            final int n = 85;
            this.font.getClass();
            if (integer2 < n + 9) {
                final Component lf5 = this.getClickedComponentAt(integer1);
                if (lf5 != null && lf5.getStyle().getHoverEvent() != null) {
                    this.renderComponentHoverEffect(lf5, integer1, integer2);
                }
            }
        }
        super.render(integer1, integer2, float3);
    }
    
    @Nullable
    public Component getClickedComponentAt(final int integer) {
        if (this.causeOfDeath == null) {
            return null;
        }
        final int integer2 = this.minecraft.font.width(this.causeOfDeath.getColoredString());
        final int integer3 = this.width / 2 - integer2 / 2;
        final int integer4 = this.width / 2 + integer2 / 2;
        int integer5 = integer3;
        if (integer < integer3 || integer > integer4) {
            return null;
        }
        for (final Component lf8 : this.causeOfDeath) {
            integer5 += this.minecraft.font.width(ComponentRenderUtils.stripColor(lf8.getContents(), false));
            if (integer5 > integer) {
                return lf8;
            }
        }
        return null;
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        if (this.causeOfDeath != null && double2 > 85.0) {
            final int n = 85;
            this.font.getClass();
            if (double2 < n + 9) {
                final Component lf7 = this.getClickedComponentAt((int)double1);
                if (lf7 != null && lf7.getStyle().getClickEvent() != null && lf7.getStyle().getClickEvent().getAction() == ClickEvent.Action.OPEN_URL) {
                    this.handleComponentClicked(lf7);
                    return false;
                }
            }
        }
        return super.mouseClicked(double1, double2, integer);
    }
    
    @Override
    public boolean isPauseScreen() {
        return false;
    }
    
    @Override
    public void tick() {
        super.tick();
        ++this.delayTicker;
        if (this.delayTicker == 20) {
            for (final AbstractWidget dct3 : this.buttons) {
                dct3.active = true;
            }
        }
    }
}
