package net.minecraft.client.gui.screens;

import org.apache.logging.log4j.LogManager;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import net.minecraft.client.gui.GuiComponent;
import java.util.Iterator;
import java.io.InputStream;
import net.minecraft.server.packs.resources.Resource;
import java.io.Closeable;
import org.apache.commons.io.IOUtils;
import java.util.Collection;
import net.minecraft.network.chat.Style;
import net.minecraft.ChatFormatting;
import java.util.Random;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import com.google.common.collect.Lists;
import net.minecraft.client.gui.chat.NarratorChatListener;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.network.chat.FormattedText;
import java.util.List;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.Logger;

public class WinScreen extends Screen {
    private static final Logger LOGGER;
    private static final ResourceLocation LOGO_LOCATION;
    private static final ResourceLocation EDITION_LOCATION;
    private static final ResourceLocation VIGNETTE_LOCATION;
    private static final String OBFUSCATE_TOKEN;
    private final boolean poem;
    private final Runnable onFinished;
    private float time;
    private List<FormattedText> lines;
    private IntSet centeredLines;
    private int totalScrollLength;
    private float scrollSpeed;
    
    public WinScreen(final boolean boolean1, final Runnable runnable) {
        super(NarratorChatListener.NO_TITLE);
        this.scrollSpeed = 0.5f;
        this.poem = boolean1;
        this.onFinished = runnable;
        if (!boolean1) {
            this.scrollSpeed = 0.75f;
        }
    }
    
    @Override
    public void tick() {
        this.minecraft.getMusicManager().tick();
        this.minecraft.getSoundManager().tick(false);
        final float float2 = (this.totalScrollLength + this.height + this.height + 24) / this.scrollSpeed;
        if (this.time > float2) {
            this.respawn();
        }
    }
    
    @Override
    public void onClose() {
        this.respawn();
    }
    
    private void respawn() {
        this.onFinished.run();
        this.minecraft.setScreen(null);
    }
    
    @Override
    protected void init() {
        if (this.lines != null) {
            return;
        }
        this.lines = Lists.newArrayList();
        this.centeredLines = (IntSet)new IntOpenHashSet();
        Resource abb2 = null;
        try {
            final int integer3 = 274;
            if (this.poem) {
                abb2 = this.minecraft.getResourceManager().getResource(new ResourceLocation("texts/end.txt"));
                final InputStream inputStream4 = abb2.getInputStream();
                final BufferedReader bufferedReader5 = new BufferedReader(new InputStreamReader(inputStream4, StandardCharsets.UTF_8));
                final Random random6 = new Random(8124371L);
                String string7;
                while ((string7 = bufferedReader5.readLine()) != null) {
                    int integer4;
                    String string8;
                    String string9;
                    for (string7 = string7.replaceAll("PLAYERNAME", this.minecraft.getUser().getName()); (integer4 = string7.indexOf(WinScreen.OBFUSCATE_TOKEN)) != -1; string7 = string8 + ChatFormatting.WHITE + ChatFormatting.OBFUSCATED + "XXXXXXXX".substring(0, random6.nextInt(4) + 3) + string9) {
                        string8 = string7.substring(0, integer4);
                        string9 = string7.substring(integer4 + WinScreen.OBFUSCATE_TOKEN.length());
                    }
                    this.lines.addAll(this.minecraft.font.getSplitter().splitLines(string7, 274, Style.EMPTY));
                    this.lines.add(FormattedText.EMPTY);
                }
                inputStream4.close();
                for (int integer4 = 0; integer4 < 8; ++integer4) {
                    this.lines.add(FormattedText.EMPTY);
                }
            }
            final InputStream inputStream4 = this.minecraft.getResourceManager().getResource(new ResourceLocation("texts/credits.txt")).getInputStream();
            final BufferedReader bufferedReader5 = new BufferedReader(new InputStreamReader(inputStream4, StandardCharsets.UTF_8));
            String string10;
            while ((string10 = bufferedReader5.readLine()) != null) {
                string10 = string10.replaceAll("PLAYERNAME", this.minecraft.getUser().getName());
                string10 = string10.replaceAll("\t", "    ");
                boolean boolean7;
                if (string10.startsWith("[C]")) {
                    string10 = string10.substring(3);
                    boolean7 = true;
                }
                else {
                    boolean7 = false;
                }
                final List<FormattedText> list8 = this.minecraft.font.getSplitter().splitLines(string10, 274, Style.EMPTY);
                for (final FormattedText mu10 : list8) {
                    if (boolean7) {
                        this.centeredLines.add(this.lines.size());
                    }
                    this.lines.add(mu10);
                }
                this.lines.add(FormattedText.EMPTY);
            }
            inputStream4.close();
            this.totalScrollLength = this.lines.size() * 12;
        }
        catch (Exception exception3) {
            WinScreen.LOGGER.error("Couldn't load credits", (Throwable)exception3);
        }
        finally {
            IOUtils.closeQuietly((Closeable)abb2);
        }
    }
    
    private void renderBg(final int integer1, final int integer2, final float float3) {
        this.minecraft.getTextureManager().bind(GuiComponent.BACKGROUND_LOCATION);
        final int integer3 = this.width;
        final float float4 = -this.time * 0.5f * this.scrollSpeed;
        final float float5 = this.height - this.time * 0.5f * this.scrollSpeed;
        final float float6 = 0.015625f;
        float float7 = this.time * 0.02f;
        final float float8 = (this.totalScrollLength + this.height + this.height + 24) / this.scrollSpeed;
        final float float9 = (float8 - 20.0f - this.time) * 0.005f;
        if (float9 < float7) {
            float7 = float9;
        }
        if (float7 > 1.0f) {
            float7 = 1.0f;
        }
        float7 *= float7;
        float7 = float7 * 96.0f / 255.0f;
        final Tesselator dhn12 = Tesselator.getInstance();
        final BufferBuilder dhg13 = dhn12.getBuilder();
        dhg13.begin(7, DefaultVertexFormat.POSITION_TEX_COLOR);
        dhg13.vertex(0.0, this.height, this.getBlitOffset()).uv(0.0f, float4 * 0.015625f).color(float7, float7, float7, 1.0f).endVertex();
        dhg13.vertex(integer3, this.height, this.getBlitOffset()).uv(integer3 * 0.015625f, float4 * 0.015625f).color(float7, float7, float7, 1.0f).endVertex();
        dhg13.vertex(integer3, 0.0, this.getBlitOffset()).uv(integer3 * 0.015625f, float5 * 0.015625f).color(float7, float7, float7, 1.0f).endVertex();
        dhg13.vertex(0.0, 0.0, this.getBlitOffset()).uv(0.0f, float5 * 0.015625f).color(float7, float7, float7, 1.0f).endVertex();
        dhn12.end();
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBg(integer2, integer3, float4);
        final int integer4 = 274;
        final int integer5 = this.width / 2 - 137;
        final int integer6 = this.height + 50;
        this.time += float4;
        final float float5 = -this.time * this.scrollSpeed;
        RenderSystem.pushMatrix();
        RenderSystem.translatef(0.0f, float5, 0.0f);
        this.minecraft.getTextureManager().bind(WinScreen.LOGO_LOCATION);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.enableAlphaTest();
        RenderSystem.enableBlend();
        this.blitOutlineBlack(integer5, integer6, (integer2, integer3) -> {
            this.blit(dhl, integer2 + 0, integer3, 0, 0, 155, 44);
            this.blit(dhl, integer2 + 155, integer3, 0, 45, 155, 44);
            return;
        });
        RenderSystem.disableBlend();
        this.minecraft.getTextureManager().bind(WinScreen.EDITION_LOCATION);
        GuiComponent.blit(dhl, integer5 + 88, integer6 + 37, 0.0f, 0.0f, 98, 14, 128, 16);
        RenderSystem.disableAlphaTest();
        int integer7 = integer6 + 100;
        for (int integer8 = 0; integer8 < this.lines.size(); ++integer8) {
            if (integer8 == this.lines.size() - 1) {
                final float float6 = integer7 + float5 - (this.height / 2 - 6);
                if (float6 < 0.0f) {
                    RenderSystem.translatef(0.0f, -float6, 0.0f);
                }
            }
            if (integer7 + float5 + 12.0f + 8.0f > 0.0f && integer7 + float5 < this.height) {
                final FormattedText mu12 = this.lines.get(integer8);
                if (this.centeredLines.contains(integer8)) {
                    this.font.drawShadow(dhl, mu12, (float)(integer5 + (274 - this.font.width(mu12)) / 2), (float)integer7, 16777215);
                }
                else {
                    this.font.random.setSeed((long)(integer8 * 4238972211L + this.time / 4.0f));
                    this.font.drawShadow(dhl, mu12, (float)integer5, (float)integer7, 16777215);
                }
            }
            integer7 += 12;
        }
        RenderSystem.popMatrix();
        this.minecraft.getTextureManager().bind(WinScreen.VIGNETTE_LOCATION);
        RenderSystem.enableBlend();
        RenderSystem.blendFunc(GlStateManager.SourceFactor.ZERO, GlStateManager.DestFactor.ONE_MINUS_SRC_COLOR);
        int integer8 = this.width;
        final int integer9 = this.height;
        final Tesselator dhn13 = Tesselator.getInstance();
        final BufferBuilder dhg14 = dhn13.getBuilder();
        dhg14.begin(7, DefaultVertexFormat.POSITION_TEX_COLOR);
        dhg14.vertex(0.0, integer9, this.getBlitOffset()).uv(0.0f, 1.0f).color(1.0f, 1.0f, 1.0f, 1.0f).endVertex();
        dhg14.vertex(integer8, integer9, this.getBlitOffset()).uv(1.0f, 1.0f).color(1.0f, 1.0f, 1.0f, 1.0f).endVertex();
        dhg14.vertex(integer8, 0.0, this.getBlitOffset()).uv(1.0f, 0.0f).color(1.0f, 1.0f, 1.0f, 1.0f).endVertex();
        dhg14.vertex(0.0, 0.0, this.getBlitOffset()).uv(0.0f, 0.0f).color(1.0f, 1.0f, 1.0f, 1.0f).endVertex();
        dhn13.end();
        RenderSystem.disableBlend();
        super.render(dhl, integer2, integer3, float4);
    }
    
    static {
        LOGGER = LogManager.getLogger();
        LOGO_LOCATION = new ResourceLocation("textures/gui/title/minecraft.png");
        EDITION_LOCATION = new ResourceLocation("textures/gui/title/edition.png");
        VIGNETTE_LOCATION = new ResourceLocation("textures/misc/vignette.png");
        OBFUSCATE_TOKEN = "" + ChatFormatting.WHITE + ChatFormatting.OBFUSCATED + ChatFormatting.GREEN + ChatFormatting.AQUA;
    }
}
