package com.mojang.realmsclient.util.task;

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

public class DownloadTask extends LongRunningTask {
    private final long worldId;
    private final int slot;
    private final Screen lastScreen;
    private final String downloadName;
    
    public DownloadTask(final long long1, final int integer, final String string, final Screen dqs) {
        this.worldId = long1;
        this.slot = integer;
        this.lastScreen = dqs;
        this.downloadName = string;
    }
    
    @Override
    public void run() {
        this.setTitle(I18n.get("mco.download.preparing"));
        final RealmsClient dia2 = RealmsClient.create();
        for (int integer3 = 0; integer3 < 25; ++integer3) {
            try {
                if (this.aborted()) {
                    return;
                }
                final WorldDownload djc4 = dia2.download(this.worldId, this.slot);
                LongRunningTask.pause(1);
                if (this.aborted()) {
                    return;
                }
                LongRunningTask.setScreen(new RealmsDownloadLatestWorldScreen(this.lastScreen, djc4, this.downloadName, boolean1 -> {}));
                return;
            }
            catch (RetryCallException dji4) {
                if (this.aborted()) {
                    return;
                }
                LongRunningTask.pause(dji4.delaySeconds);
            }
            catch (RealmsServiceException djh4) {
                if (this.aborted()) {
                    return;
                }
                DownloadTask.LOGGER.error("Couldn't download world data");
                LongRunningTask.setScreen(new RealmsGenericErrorScreen(djh4, this.lastScreen));
                return;
            }
            catch (Exception exception4) {
                if (this.aborted()) {
                    return;
                }
                DownloadTask.LOGGER.error("Couldn't download world data", (Throwable)exception4);
                this.error(exception4.getLocalizedMessage());
                return;
            }
        }
    }
}
