package com.mojang.realmsclient.util.task;

import com.mojang.realmsclient.exception.RealmsServiceException;
import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
import com.mojang.realmsclient.exception.RetryCallException;
import net.minecraft.client.gui.screens.Screen;
import com.mojang.realmsclient.client.RealmsClient;
import net.minecraft.client.resources.language.I18n;
import com.mojang.realmsclient.gui.screens.RealmsConfigureWorldScreen;
import com.mojang.realmsclient.dto.Backup;

public class RestoreTask extends LongRunningTask {
    private final Backup backup;
    private final long worldId;
    private final RealmsConfigureWorldScreen lastScreen;
    
    public RestoreTask(final Backup dif, final long long2, final RealmsConfigureWorldScreen djr) {
        this.backup = dif;
        this.worldId = long2;
        this.lastScreen = djr;
    }
    
    @Override
    public void run() {
        this.setTitle(I18n.get("mco.backup.restoring"));
        final RealmsClient dia2 = RealmsClient.create();
        for (int integer3 = 0; integer3 < 25; ++integer3) {
            try {
                if (this.aborted()) {
                    return;
                }
                dia2.restoreWorld(this.worldId, this.backup.backupId);
                LongRunningTask.pause(1);
                if (this.aborted()) {
                    return;
                }
                LongRunningTask.setScreen(this.lastScreen.getNewScreen());
                return;
            }
            catch (RetryCallException dji4) {
                if (this.aborted()) {
                    return;
                }
                LongRunningTask.pause(dji4.delaySeconds);
            }
            catch (RealmsServiceException djh4) {
                if (this.aborted()) {
                    return;
                }
                RestoreTask.LOGGER.error("Couldn't restore backup", (Throwable)djh4);
                LongRunningTask.setScreen(new RealmsGenericErrorScreen(djh4, this.lastScreen));
                return;
            }
            catch (Exception exception4) {
                if (this.aborted()) {
                    return;
                }
                RestoreTask.LOGGER.error("Couldn't restore backup", (Throwable)exception4);
                this.error(exception4.getLocalizedMessage());
                return;
            }
        }
    }
}
