package com.mojang.realmsclient.gui.screens;

import org.apache.logging.log4j.LogManager;
import net.minecraft.client.Minecraft;
import com.mojang.realmsclient.util.task.ResettingWorldTask;
import com.mojang.realmsclient.util.task.LongRunningTask;
import com.mojang.realmsclient.util.task.SwitchSlotTask;
import javax.annotation.Nullable;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.client.gui.GuiComponent;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.realmsclient.exception.RealmsServiceException;
import com.mojang.realmsclient.client.RealmsClient;
import net.minecraft.client.gui.components.Button;
import net.minecraft.realms.RealmsScreen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.realmsclient.dto.WorldTemplate;
import com.mojang.realmsclient.dto.WorldTemplatePaginatedList;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.Component;
import net.minecraft.realms.RealmsLabel;
import com.mojang.realmsclient.dto.RealmsServer;
import net.minecraft.client.gui.screens.Screen;
import org.apache.logging.log4j.Logger;

public class RealmsResetWorldScreen extends RealmsScreenWithCallback {
    private static final Logger LOGGER;
    private final Screen lastScreen;
    private final RealmsServer serverData;
    private RealmsLabel titleLabel;
    private RealmsLabel subtitleLabel;
    private Component title;
    private Component subtitle;
    private Component buttonTitle;
    private int subtitleColor;
    private static final ResourceLocation SLOT_FRAME_LOCATION;
    private static final ResourceLocation UPLOAD_LOCATION;
    private static final ResourceLocation ADVENTURE_MAP_LOCATION;
    private static final ResourceLocation SURVIVAL_SPAWN_LOCATION;
    private static final ResourceLocation NEW_WORLD_LOCATION;
    private static final ResourceLocation EXPERIENCE_LOCATION;
    private static final ResourceLocation INSPIRATION_LOCATION;
    private WorldTemplatePaginatedList templates;
    private WorldTemplatePaginatedList adventuremaps;
    private WorldTemplatePaginatedList experiences;
    private WorldTemplatePaginatedList inspirations;
    public int slot;
    private ResetType typeToReset;
    private ResetWorldInfo worldInfoToReset;
    private WorldTemplate worldTemplateToReset;
    private String resetTitle;
    private final Runnable resetWorldRunnable;
    private final Runnable callback;
    
    public RealmsResetWorldScreen(final Screen dqs, final RealmsServer dip, final Runnable runnable3, final Runnable runnable4) {
        this.title = new TranslatableComponent("mco.reset.world.title");
        this.subtitle = new TranslatableComponent("mco.reset.world.warning");
        this.buttonTitle = CommonComponents.GUI_CANCEL;
        this.subtitleColor = 16711680;
        this.slot = -1;
        this.typeToReset = ResetType.NONE;
        this.lastScreen = dqs;
        this.serverData = dip;
        this.resetWorldRunnable = runnable3;
        this.callback = runnable4;
    }
    
    public RealmsResetWorldScreen(final Screen dqs, final RealmsServer dip, final Component mr3, final Component mr4, final int integer, final Component mr6, final Runnable runnable7, final Runnable runnable8) {
        this(dqs, dip, runnable7, runnable8);
        this.title = mr3;
        this.subtitle = mr4;
        this.subtitleColor = integer;
        this.buttonTitle = mr6;
    }
    
    public void setSlot(final int integer) {
        this.slot = integer;
    }
    
    public void setResetTitle(final String string) {
        this.resetTitle = string;
    }
    
    public void init() {
        this.<Button>addButton(new Button(this.width / 2 - 40, RealmsScreen.row(14) - 10, 80, 20, this.buttonTitle, dni -> this.minecraft.setScreen(this.lastScreen)));
        new Thread("Realms-reset-world-fetcher") {
            @Override
            public void run() {
                final RealmsClient dia2 = RealmsClient.create();
                try {
                    final WorldTemplatePaginatedList dje3 = dia2.fetchWorldTemplates(1, 10, RealmsServer.WorldType.NORMAL);
                    final WorldTemplatePaginatedList dje4 = dia2.fetchWorldTemplates(1, 10, RealmsServer.WorldType.ADVENTUREMAP);
                    final WorldTemplatePaginatedList dje5 = dia2.fetchWorldTemplates(1, 10, RealmsServer.WorldType.EXPERIENCE);
                    final WorldTemplatePaginatedList dje6 = dia2.fetchWorldTemplates(1, 10, RealmsServer.WorldType.INSPIRATION);
                    final WorldTemplatePaginatedList dje7;
                    final WorldTemplatePaginatedList dje8;
                    final WorldTemplatePaginatedList dje9;
                    final WorldTemplatePaginatedList dje10;
                    RealmsResetWorldScreen.this.minecraft.execute(() -> {
                        RealmsResetWorldScreen.this.templates = dje7;
                        RealmsResetWorldScreen.this.adventuremaps = dje8;
                        RealmsResetWorldScreen.this.experiences = dje9;
                        RealmsResetWorldScreen.this.inspirations = dje10;
                    });
                }
                catch (RealmsServiceException djh3) {
                    RealmsResetWorldScreen.LOGGER.error("Couldn't fetch templates in reset world", (Throwable)djh3);
                }
            }
        }.start();
        this.titleLabel = this.<RealmsLabel>addWidget(new RealmsLabel(this.title, this.width / 2, 7, 16777215));
        this.subtitleLabel = this.<RealmsLabel>addWidget(new RealmsLabel(this.subtitle, this.width / 2, 22, this.subtitleColor));
        this.<FrameButton>addButton(new FrameButton(this.frame(1), RealmsScreen.row(0) + 10, new TranslatableComponent("mco.reset.world.generate"), RealmsResetWorldScreen.NEW_WORLD_LOCATION, dni -> this.minecraft.setScreen(new RealmsResetNormalWorldScreen(this, this.title))));
        final RealmsSelectFileToUploadScreen realmsSelectFileToUploadScreen;
        final Screen dqs3;
        this.<FrameButton>addButton(new FrameButton(this.frame(2), RealmsScreen.row(0) + 10, new TranslatableComponent("mco.reset.world.upload"), RealmsResetWorldScreen.UPLOAD_LOCATION, dni -> {
            new RealmsSelectFileToUploadScreen(this.serverData.id, (this.slot != -1) ? this.slot : this.serverData.activeSlot, this, this.callback);
            dqs3 = realmsSelectFileToUploadScreen;
            this.minecraft.setScreen(dqs3);
            return;
        }));
        final RealmsSelectWorldTemplateScreen dkh3;
        this.<FrameButton>addButton(new FrameButton(this.frame(3), RealmsScreen.row(0) + 10, new TranslatableComponent("mco.reset.world.template"), RealmsResetWorldScreen.SURVIVAL_SPAWN_LOCATION, dni -> {
            dkh3 = new RealmsSelectWorldTemplateScreen(this, RealmsServer.WorldType.NORMAL, this.templates);
            dkh3.setTitle(new TranslatableComponent("mco.reset.world.template"));
            this.minecraft.setScreen(dkh3);
            return;
        }));
        final RealmsSelectWorldTemplateScreen dkh4;
        this.<FrameButton>addButton(new FrameButton(this.frame(1), RealmsScreen.row(6) + 20, new TranslatableComponent("mco.reset.world.adventure"), RealmsResetWorldScreen.ADVENTURE_MAP_LOCATION, dni -> {
            dkh4 = new RealmsSelectWorldTemplateScreen(this, RealmsServer.WorldType.ADVENTUREMAP, this.adventuremaps);
            dkh4.setTitle(new TranslatableComponent("mco.reset.world.adventure"));
            this.minecraft.setScreen(dkh4);
            return;
        }));
        final RealmsSelectWorldTemplateScreen dkh5;
        this.<FrameButton>addButton(new FrameButton(this.frame(2), RealmsScreen.row(6) + 20, new TranslatableComponent("mco.reset.world.experience"), RealmsResetWorldScreen.EXPERIENCE_LOCATION, dni -> {
            dkh5 = new RealmsSelectWorldTemplateScreen(this, RealmsServer.WorldType.EXPERIENCE, this.experiences);
            dkh5.setTitle(new TranslatableComponent("mco.reset.world.experience"));
            this.minecraft.setScreen(dkh5);
            return;
        }));
        final RealmsSelectWorldTemplateScreen dkh6;
        this.<FrameButton>addButton(new FrameButton(this.frame(3), RealmsScreen.row(6) + 20, new TranslatableComponent("mco.reset.world.inspiration"), RealmsResetWorldScreen.INSPIRATION_LOCATION, dni -> {
            dkh6 = new RealmsSelectWorldTemplateScreen(this, RealmsServer.WorldType.INSPIRATION, this.inspirations);
            dkh6.setTitle(new TranslatableComponent("mco.reset.world.inspiration"));
            this.minecraft.setScreen(dkh6);
            return;
        }));
        this.narrateLabels();
    }
    
    @Override
    public void removed() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
    }
    
    @Override
    public boolean keyPressed(final int integer1, final int integer2, final int integer3) {
        if (integer1 == 256) {
            this.minecraft.setScreen(this.lastScreen);
            return true;
        }
        return super.keyPressed(integer1, integer2, integer3);
    }
    
    private int frame(final int integer) {
        return this.width / 2 - 130 + (integer - 1) * 100;
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        this.titleLabel.render(this, dhl);
        this.subtitleLabel.render(this, dhl);
        super.render(dhl, integer2, integer3, float4);
    }
    
    private void drawFrame(final PoseStack dhl, final int integer2, final int integer3, final Component mr, final ResourceLocation uh, final boolean boolean6, final boolean boolean7) {
        this.minecraft.getTextureManager().bind(uh);
        if (boolean6) {
            RenderSystem.color4f(0.56f, 0.56f, 0.56f, 1.0f);
        }
        else {
            RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        }
        GuiComponent.blit(dhl, integer2 + 2, integer3 + 14, 0.0f, 0.0f, 56, 56, 56, 56);
        this.minecraft.getTextureManager().bind(RealmsResetWorldScreen.SLOT_FRAME_LOCATION);
        if (boolean6) {
            RenderSystem.color4f(0.56f, 0.56f, 0.56f, 1.0f);
        }
        else {
            RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        }
        GuiComponent.blit(dhl, integer2, integer3 + 12, 0.0f, 0.0f, 60, 60, 60, 60);
        final int integer4 = boolean6 ? 10526880 : 16777215;
        this.drawCenteredString(dhl, this.font, mr, integer2 + 30, integer3, integer4);
    }
    
    @Override
    protected void callback(@Nullable final WorldTemplate djd) {
        if (djd == null) {
            return;
        }
        if (this.slot == -1) {
            this.resetWorldWithTemplate(djd);
        }
        else {
            switch (djd.type) {
                case WORLD_TEMPLATE: {
                    this.typeToReset = ResetType.SURVIVAL_SPAWN;
                    break;
                }
                case ADVENTUREMAP: {
                    this.typeToReset = ResetType.ADVENTURE;
                    break;
                }
                case EXPERIENCE: {
                    this.typeToReset = ResetType.EXPERIENCE;
                    break;
                }
                case INSPIRATION: {
                    this.typeToReset = ResetType.INSPIRATION;
                    break;
                }
            }
            this.worldTemplateToReset = djd;
            this.switchSlot();
        }
    }
    
    private void switchSlot() {
        this.switchSlot(() -> {
            switch (this.typeToReset) {
                case ADVENTURE:
                case SURVIVAL_SPAWN:
                case EXPERIENCE:
                case INSPIRATION: {
                    if (this.worldTemplateToReset != null) {
                        this.resetWorldWithTemplate(this.worldTemplateToReset);
                        break;
                    }
                    else {
                        break;
                    }
                    break;
                }
                case GENERATE: {
                    if (this.worldInfoToReset != null) {
                        this.triggerResetWorld(this.worldInfoToReset);
                        break;
                    }
                    else {
                        break;
                    }
                    break;
                }
            }
        });
    }
    
    public void switchSlot(final Runnable runnable) {
        this.minecraft.setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, new SwitchSlotTask(this.serverData.id, this.slot, runnable)));
    }
    
    public void resetWorldWithTemplate(final WorldTemplate djd) {
        this.resetWorld(null, djd, -1, true);
    }
    
    private void triggerResetWorld(final ResetWorldInfo c) {
        this.resetWorld(c.seed, null, c.levelType, c.generateStructures);
    }
    
    private void resetWorld(@Nullable final String string, @Nullable final WorldTemplate djd, final int integer, final boolean boolean4) {
        this.minecraft.setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, new ResettingWorldTask(string, djd, integer, boolean4, this.serverData.id, this.resetTitle, this.resetWorldRunnable)));
    }
    
    public void resetWorld(final ResetWorldInfo c) {
        if (this.slot == -1) {
            this.triggerResetWorld(c);
        }
        else {
            this.typeToReset = ResetType.GENERATE;
            this.worldInfoToReset = c;
            this.switchSlot();
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
        SLOT_FRAME_LOCATION = new ResourceLocation("realms", "textures/gui/realms/slot_frame.png");
        UPLOAD_LOCATION = new ResourceLocation("realms", "textures/gui/realms/upload.png");
        ADVENTURE_MAP_LOCATION = new ResourceLocation("realms", "textures/gui/realms/adventure.png");
        SURVIVAL_SPAWN_LOCATION = new ResourceLocation("realms", "textures/gui/realms/survival_spawn.png");
        NEW_WORLD_LOCATION = new ResourceLocation("realms", "textures/gui/realms/new_world.png");
        EXPERIENCE_LOCATION = new ResourceLocation("realms", "textures/gui/realms/experience.png");
        INSPIRATION_LOCATION = new ResourceLocation("realms", "textures/gui/realms/inspiration.png");
    }
    
    enum ResetType {
        NONE, 
        GENERATE, 
        UPLOAD, 
        ADVENTURE, 
        SURVIVAL_SPAWN, 
        EXPERIENCE, 
        INSPIRATION;
    }
    
    public static class ResetWorldInfo {
        private final String seed;
        private final int levelType;
        private final boolean generateStructures;
        
        public ResetWorldInfo(final String string, final int integer, final boolean boolean3) {
            this.seed = string;
            this.levelType = integer;
            this.generateStructures = boolean3;
        }
    }
    
    class FrameButton extends Button {
        private final ResourceLocation image;
        
        public FrameButton(final int integer2, final int integer3, final Component mr, final ResourceLocation uh, final OnPress a) {
            super(integer2, integer3, 60, 72, mr, a);
            this.image = uh;
        }
        
        @Override
        public void renderButton(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
            RealmsResetWorldScreen.this.drawFrame(dhl, this.x, this.y, this.getMessage(), this.image, this.isHovered(), this.isMouseOver(integer2, integer3));
        }
    }
}
