package net.minecraft.client.gui.screens;

import com.google.common.util.concurrent.Runnables;
import java.util.Iterator;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.SharedConstants;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.util.Mth;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.Util;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.world.level.storage.LevelData;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.client.gui.screens.multiplayer.SafetyScreen;
import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
import net.minecraft.client.gui.screens.worldselection.SelectWorldScreen;
import net.minecraft.realms.RealmsBridge;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.resources.language.I18n;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import net.minecraft.client.renderer.texture.TextureManager;
import java.util.Random;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.renderer.PanoramaRenderer;
import net.minecraft.client.gui.components.Button;
import javax.annotation.Nullable;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.renderer.CubeMap;

public class TitleScreen extends Screen {
    public static final CubeMap CUBE_MAP;
    private static final ResourceLocation PANORAMA_OVERLAY;
    private static final ResourceLocation ACCESSIBILITY_TEXTURE;
    private final boolean minceraftEasterEgg;
    @Nullable
    private String splash;
    private Button resetDemoButton;
    private static final ResourceLocation MINECRAFT_LOGO;
    private static final ResourceLocation MINECRAFT_EDITION;
    private boolean realmsNotificationsInitialized;
    private Screen realmsNotificationsScreen;
    private int copyrightWidth;
    private int copyrightX;
    private final PanoramaRenderer panorama;
    private final boolean fading;
    private long fadeInStart;
    
    public TitleScreen() {
        this(false);
    }
    
    public TitleScreen(final boolean boolean1) {
        super(new TranslatableComponent("narrator.screen.title", new Object[0]));
        this.panorama = new PanoramaRenderer(TitleScreen.CUBE_MAP);
        this.fading = boolean1;
        this.minceraftEasterEgg = (new Random().nextFloat() < 1.0E-4);
    }
    
    private boolean realmsNotificationsEnabled() {
        return this.minecraft.options.realmsNotifications && this.realmsNotificationsScreen != null;
    }
    
    @Override
    public void tick() {
        if (this.realmsNotificationsEnabled()) {
            this.realmsNotificationsScreen.tick();
        }
    }
    
    public static CompletableFuture<Void> preloadResources(final TextureManager eak, final Executor executor) {
        return CompletableFuture.allOf(eak.preload(TitleScreen.MINECRAFT_LOGO, executor), eak.preload(TitleScreen.MINECRAFT_EDITION, executor), eak.preload(TitleScreen.PANORAMA_OVERLAY, executor), TitleScreen.CUBE_MAP.preload(eak, executor));
    }
    
    @Override
    public boolean isPauseScreen() {
        return false;
    }
    
    @Override
    public boolean shouldCloseOnEsc() {
        return false;
    }
    
    @Override
    protected void init() {
        if (this.splash == null) {
            this.splash = this.minecraft.getSplashManager().getSplash();
        }
        this.copyrightWidth = this.font.width("Copyright Mojang AB. Do not distribute!");
        this.copyrightX = this.width - this.copyrightWidth - 2;
        final int integer2 = 24;
        final int integer3 = this.height / 4 + 48;
        if (this.minecraft.isDemo()) {
            this.createDemoMenuOptions(integer3, 24);
        }
        else {
            this.createNormalMenuOptions(integer3, 24);
        }
        this.<ImageButton>addButton(new ImageButton(this.width / 2 - 124, integer3 + 72 + 12, 20, 20, 0, 106, 20, Button.WIDGETS_LOCATION, 256, 256, dcv -> this.minecraft.setScreen(new LanguageSelectScreen(this, this.minecraft.options, this.minecraft.getLanguageManager())), I18n.get("narrator.button.language")));
        this.<Button>addButton(new Button(this.width / 2 - 100, integer3 + 72 + 12, 98, 20, I18n.get("menu.options"), dcv -> this.minecraft.setScreen(new OptionsScreen(this, this.minecraft.options))));
        this.<Button>addButton(new Button(this.width / 2 + 2, integer3 + 72 + 12, 98, 20, I18n.get("menu.quit"), dcv -> this.minecraft.stop()));
        this.<ImageButton>addButton(new ImageButton(this.width / 2 + 104, integer3 + 72 + 12, 20, 20, 0, 0, 20, TitleScreen.ACCESSIBILITY_TEXTURE, 32, 64, dcv -> this.minecraft.setScreen(new AccessibilityOptionsScreen(this, this.minecraft.options)), I18n.get("narrator.button.accessibility")));
        this.minecraft.setConnectedToRealms(false);
        if (this.minecraft.options.realmsNotifications && !this.realmsNotificationsInitialized) {
            final RealmsBridge realmsBridge4 = new RealmsBridge();
            this.realmsNotificationsScreen = realmsBridge4.getNotificationScreen(this);
            this.realmsNotificationsInitialized = true;
        }
        if (this.realmsNotificationsEnabled()) {
            this.realmsNotificationsScreen.init(this.minecraft, this.width, this.height);
        }
    }
    
    private void createNormalMenuOptions(final int integer1, final int integer2) {
        this.<Button>addButton(new Button(this.width / 2 - 100, integer1, 200, 20, I18n.get("menu.singleplayer"), dcv -> this.minecraft.setScreen(new SelectWorldScreen(this))));
        this.<Button>addButton(new Button(this.width / 2 - 100, integer1 + integer2 * 1, 200, 20, I18n.get("menu.multiplayer"), dcv -> {
            if (this.minecraft.options.skipMultiplayerWarning) {
                this.minecraft.setScreen(new JoinMultiplayerScreen(this));
            }
            else {
                this.minecraft.setScreen(new SafetyScreen(this));
            }
            return;
        }));
        this.<Button>addButton(new Button(this.width / 2 - 100, integer1 + integer2 * 2, 200, 20, I18n.get("menu.online"), dcv -> this.realmsButtonClicked()));
    }
    
    private void createDemoMenuOptions(final int integer1, final int integer2) {
        this.<Button>addButton(new Button(this.width / 2 - 100, integer1, 200, 20, I18n.get("menu.playdemo"), dcv -> this.minecraft.selectLevel("Demo_World", "Demo_World", MinecraftServer.DEMO_SETTINGS)));
        final LevelStorageSource crp3;
        final LevelData crm4;
        Minecraft minecraft;
        TranslatableComponent lf2;
        final TranslatableComponent lf3;
        final Screen screen;
        final BooleanConsumer booleanConsumer;
        this.resetDemoButton = this.<Button>addButton(new Button(this.width / 2 - 100, integer1 + integer2 * 1, 200, 20, I18n.get("menu.resetdemo"), dcv -> {
            crp3 = this.minecraft.getLevelSource();
            crm4 = crp3.getDataTagFor("Demo_World");
            if (crm4 != null) {
                minecraft = this.minecraft;
                // new(net.minecraft.client.gui.screens.ConfirmScreen.class)
                this::confirmDemo;
                lf2 = new TranslatableComponent("selectWorld.deleteQuestion", new Object[0]);
                new TranslatableComponent("selectWorld.deleteWarning", new Object[] { crm4.getLevelName() });
                new ConfirmScreen(booleanConsumer, lf2, lf3, I18n.get("selectWorld.deleteButton"), I18n.get("gui.cancel"));
                minecraft.setScreen(screen);
            }
            return;
        }));
        final LevelStorageSource crp4 = this.minecraft.getLevelSource();
        final LevelData crm5 = crp4.getDataTagFor("Demo_World");
        if (crm5 == null) {
            this.resetDemoButton.active = false;
        }
    }
    
    private void realmsButtonClicked() {
        final RealmsBridge realmsBridge2 = new RealmsBridge();
        realmsBridge2.switchToRealms(this);
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        if (this.fadeInStart == 0L && this.fading) {
            this.fadeInStart = Util.getMillis();
        }
        final float float4 = this.fading ? ((Util.getMillis() - this.fadeInStart) / 1000.0f) : 1.0f;
        GuiComponent.fill(0, 0, this.width, this.height, -1);
        this.panorama.render(float3, Mth.clamp(float4, 0.0f, 1.0f));
        final int integer3 = 274;
        final int integer4 = this.width / 2 - 137;
        final int integer5 = 30;
        this.minecraft.getTextureManager().bind(TitleScreen.PANORAMA_OVERLAY);
        RenderSystem.enableBlend();
        RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, this.fading ? ((float)Mth.ceil(Mth.clamp(float4, 0.0f, 1.0f))) : 1.0f);
        GuiComponent.blit(0, 0, this.width, this.height, 0.0f, 0.0f, 16, 128, 16, 128);
        final float float5 = this.fading ? Mth.clamp(float4 - 1.0f, 0.0f, 1.0f) : 1.0f;
        final int integer6 = Mth.ceil(float5 * 255.0f) << 24;
        if ((integer6 & 0xFC000000) == 0x0) {
            return;
        }
        this.minecraft.getTextureManager().bind(TitleScreen.MINECRAFT_LOGO);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, float5);
        if (this.minceraftEasterEgg) {
            this.blit(integer4 + 0, 30, 0, 0, 99, 44);
            this.blit(integer4 + 99, 30, 129, 0, 27, 44);
            this.blit(integer4 + 99 + 26, 30, 126, 0, 3, 44);
            this.blit(integer4 + 99 + 26 + 3, 30, 99, 0, 26, 44);
            this.blit(integer4 + 155, 30, 0, 45, 155, 44);
        }
        else {
            this.blit(integer4 + 0, 30, 0, 0, 155, 44);
            this.blit(integer4 + 155, 30, 0, 45, 155, 44);
        }
        this.minecraft.getTextureManager().bind(TitleScreen.MINECRAFT_EDITION);
        GuiComponent.blit(integer4 + 88, 67, 0.0f, 0.0f, 98, 14, 128, 16);
        if (this.splash != null) {
            RenderSystem.pushMatrix();
            RenderSystem.translatef((float)(this.width / 2 + 90), 70.0f, 0.0f);
            RenderSystem.rotatef(-20.0f, 0.0f, 0.0f, 1.0f);
            float float6 = 1.8f - Mth.abs(Mth.sin(Util.getMillis() % 1000L / 1000.0f * 6.2831855f) * 0.1f);
            float6 = float6 * 100.0f / (this.font.width(this.splash) + 32);
            RenderSystem.scalef(float6, float6, float6);
            this.drawCenteredString(this.font, this.splash, 0, -8, 0xFFFF00 | integer6);
            RenderSystem.popMatrix();
        }
        String string11 = "Minecraft " + SharedConstants.getCurrentVersion().getName();
        if (this.minecraft.isDemo()) {
            string11 += " Demo";
        }
        else {
            string11 += ("release".equalsIgnoreCase(this.minecraft.getVersionType()) ? "" : ("/" + this.minecraft.getVersionType()));
        }
        if (this.minecraft.isProbablyModded()) {
            string11 += I18n.get("menu.modded");
        }
        this.drawString(this.font, string11, 2, this.height - 10, 0xFFFFFF | integer6);
        this.drawString(this.font, "Copyright Mojang AB. Do not distribute!", this.copyrightX, this.height - 10, 0xFFFFFF | integer6);
        if (integer1 > this.copyrightX && integer1 < this.copyrightX + this.copyrightWidth && integer2 > this.height - 10 && integer2 < this.height) {
            GuiComponent.fill(this.copyrightX, this.height - 1, this.copyrightX + this.copyrightWidth, this.height, 0xFFFFFF | integer6);
        }
        for (final AbstractWidget dct13 : this.buttons) {
            dct13.setAlpha(float5);
        }
        super.render(integer1, integer2, float3);
        if (this.realmsNotificationsEnabled() && float5 >= 1.0f) {
            this.realmsNotificationsScreen.render(integer1, integer2, float3);
        }
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        if (super.mouseClicked(double1, double2, integer)) {
            return true;
        }
        if (this.realmsNotificationsEnabled() && this.realmsNotificationsScreen.mouseClicked(double1, double2, integer)) {
            return true;
        }
        if (double1 > this.copyrightX && double1 < this.copyrightX + this.copyrightWidth && double2 > this.height - 10 && double2 < this.height) {
            this.minecraft.setScreen(new WinScreen(false, Runnables.doNothing()));
        }
        return false;
    }
    
    @Override
    public void removed() {
        if (this.realmsNotificationsScreen != null) {
            this.realmsNotificationsScreen.removed();
        }
    }
    
    private void confirmDemo(final boolean boolean1) {
        if (boolean1) {
            final LevelStorageSource crp3 = this.minecraft.getLevelSource();
            crp3.deleteLevel("Demo_World");
        }
        this.minecraft.setScreen(this);
    }
    
    static {
        CUBE_MAP = new CubeMap(new ResourceLocation("textures/gui/title/background/panorama"));
        PANORAMA_OVERLAY = new ResourceLocation("textures/gui/title/background/panorama_overlay.png");
        ACCESSIBILITY_TEXTURE = new ResourceLocation("textures/gui/accessibility.png");
        MINECRAFT_LOGO = new ResourceLocation("textures/gui/title/minecraft.png");
        MINECRAFT_EDITION = new ResourceLocation("textures/gui/title/edition.png");
    }
}
