package net.minecraft.client.gui.screens.worldselection;

import net.minecraft.client.gui.components.toasts.ToastComponent;
import net.minecraft.client.gui.components.toasts.Toast;
import net.minecraft.client.gui.components.toasts.SystemToast;
import net.minecraft.util.Mth;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.level.storage.LevelData;
import net.minecraft.client.Minecraft;
import java.nio.file.Path;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.BackupConfirmScreen;
import java.io.IOException;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import net.minecraft.Util;
import org.apache.commons.io.FileUtils;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.gui.components.EditBox;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;

public class EditWorldScreen extends Screen {
    private Button renameButton;
    private final BooleanConsumer callback;
    private EditBox nameEdit;
    private final String levelId;
    
    public EditWorldScreen(final BooleanConsumer booleanConsumer, final String string) {
        super(new TranslatableComponent("selectWorld.edit.title", new Object[0]));
        this.callback = booleanConsumer;
        this.levelId = string;
    }
    
    @Override
    public void tick() {
        this.nameEdit.tick();
    }
    
    @Override
    protected void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        final LevelStorageSource crp3;
        final Button dcv2 = this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 24 + 5, 200, 20, I18n.get("selectWorld.edit.resetIcon"), dcv -> {
            crp3 = this.minecraft.getLevelSource();
            FileUtils.deleteQuietly(crp3.getFile(this.levelId, "icon.png"));
            dcv.active = false;
            return;
        }));
        final LevelStorageSource crp4;
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 48 + 5, 200, 20, I18n.get("selectWorld.edit.openFolder"), dcv -> {
            crp4 = this.minecraft.getLevelSource();
            Util.getPlatform().openFile(crp4.getFile(this.levelId, "icon.png").getParentFile());
            return;
        }));
        final LevelStorageSource crp5;
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 72 + 5, 200, 20, I18n.get("selectWorld.edit.backup"), dcv -> {
            crp5 = this.minecraft.getLevelSource();
            makeBackupAndShowToast(crp5, this.levelId);
            this.callback.accept(false);
            return;
        }));
        final LevelStorageSource crp6;
        final Path path4;
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 96 + 5, 200, 20, I18n.get("selectWorld.edit.backupFolder"), dcv -> {
            crp6 = this.minecraft.getLevelSource();
            path4 = crp6.getBackupPath();
            try {
                Files.createDirectories(Files.exists(path4) ? path4.toRealPath() : path4, new FileAttribute[0]);
            }
            catch (IOException iOException5) {
                throw new RuntimeException(iOException5);
            }
            Util.getPlatform().openFile(path4.toFile());
            return;
        }));
        final Minecraft minecraft;
        final BackupConfirmScreen screen;
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 120 + 5, 200, 20, I18n.get("selectWorld.edit.optimize"), dcv -> {
            minecraft = this.minecraft;
            new BackupConfirmScreen(this, (boolean1, boolean2) -> {
                if (boolean1) {
                    makeBackupAndShowToast(this.minecraft.getLevelSource(), this.levelId);
                }
                this.minecraft.setScreen(new OptimizeWorldScreen(this.callback, this.levelId, this.minecraft.getLevelSource(), boolean2));
                return;
            }, new TranslatableComponent("optimizeWorld.confirm.title", new Object[0]), new TranslatableComponent("optimizeWorld.confirm.description", new Object[0]), true);
            minecraft.setScreen(screen);
            return;
        }));
        this.renameButton = this.<Button>addButton(new Button(this.width / 2 - 100, this.height / 4 + 144 + 5, 98, 20, I18n.get("selectWorld.edit.save"), dcv -> this.onRename()));
        this.<Button>addButton(new Button(this.width / 2 + 2, this.height / 4 + 144 + 5, 98, 20, I18n.get("gui.cancel"), dcv -> this.callback.accept(false)));
        dcv2.active = this.minecraft.getLevelSource().getFile(this.levelId, "icon.png").isFile();
        final LevelStorageSource crp7 = this.minecraft.getLevelSource();
        final LevelData crm4 = crp7.getDataTagFor(this.levelId);
        final String string2 = (crm4 == null) ? "" : crm4.getLevelName();
        (this.nameEdit = new EditBox(this.font, this.width / 2 - 100, 53, 200, 20, I18n.get("selectWorld.enterName"))).setValue(string2);
        this.nameEdit.setResponder(string -> this.renameButton.active = !string.trim().isEmpty());
        this.children.add(this.nameEdit);
        this.setInitialFocus(this.nameEdit);
    }
    
    @Override
    public void resize(final Minecraft dbn, final int integer2, final int integer3) {
        final String string5 = this.nameEdit.getValue();
        this.init(dbn, integer2, integer3);
        this.nameEdit.setValue(string5);
    }
    
    @Override
    public void removed() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
    }
    
    private void onRename() {
        final LevelStorageSource crp2 = this.minecraft.getLevelSource();
        crp2.renameLevel(this.levelId, this.nameEdit.getValue().trim());
        this.callback.accept(true);
    }
    
    public static void makeBackupAndShowToast(final LevelStorageSource crp, final String string) {
        final ToastComponent deb3 = Minecraft.getInstance().getToasts();
        long long4 = 0L;
        IOException iOException6 = null;
        try {
            long4 = crp.makeWorldBackup(string);
        }
        catch (IOException iOException7) {
            iOException6 = iOException7;
        }
        Component lf7;
        Component lf8;
        if (iOException6 != null) {
            lf7 = new TranslatableComponent("selectWorld.edit.backupFailed", new Object[0]);
            lf8 = new TextComponent(iOException6.getMessage());
        }
        else {
            lf7 = new TranslatableComponent("selectWorld.edit.backupCreated", new Object[] { string });
            lf8 = new TranslatableComponent("selectWorld.edit.backupSize", new Object[] { Mth.ceil(long4 / 1048576.0) });
        }
        deb3.addToast(new SystemToast(SystemToast.SystemToastIds.WORLD_BACKUP, lf7, lf8));
    }
    
    @Override
    public void render(final int integer1, final int integer2, final float float3) {
        this.renderBackground();
        this.drawCenteredString(this.font, this.title.getColoredString(), this.width / 2, 20, 16777215);
        this.drawString(this.font, I18n.get("selectWorld.enterName"), this.width / 2 - 100, 40, 10526880);
        this.nameEdit.render(integer1, integer2, float3);
        super.render(integer1, integer2, float3);
    }
}
