package com.mojang.realmsclient.util.task;

import java.util.function.Function;
import net.minecraft.network.chat.TextComponent;
import com.mojang.realmsclient.dto.RealmsServerAddress;
import com.mojang.realmsclient.gui.screens.RealmsLongRunningMcoTaskScreen;
import com.mojang.realmsclient.gui.screens.RealmsLongConfirmationScreen;
import net.minecraft.network.chat.Component;
import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.realmsclient.gui.screens.RealmsBrokenWorldScreen;
import net.minecraft.client.Minecraft;
import com.mojang.realmsclient.gui.screens.RealmsTermsScreen;
import com.mojang.realmsclient.exception.RealmsServiceException;
import com.mojang.realmsclient.exception.RetryCallException;
import com.mojang.realmsclient.client.RealmsClient;
import net.minecraft.client.resources.language.I18n;
import java.util.concurrent.locks.ReentrantLock;
import com.mojang.realmsclient.RealmsMainScreen;
import net.minecraft.client.gui.screens.Screen;
import com.mojang.realmsclient.dto.RealmsServer;

public class GetServerDetailsTask extends LongRunningTask {
    private final RealmsServer server;
    private final Screen lastScreen;
    private final RealmsMainScreen mainScreen;
    private final ReentrantLock connectLock;
    
    public GetServerDetailsTask(final RealmsMainScreen dhv, final Screen dqs, final RealmsServer dip, final ReentrantLock reentrantLock) {
        this.lastScreen = dqs;
        this.mainScreen = dhv;
        this.server = dip;
        this.connectLock = reentrantLock;
    }
    
    @Override
    public void run() {
        this.setTitle(I18n.get("mco.connect.connecting"));
        final RealmsClient dia2 = RealmsClient.create();
        boolean boolean3 = false;
        boolean boolean4 = false;
        int integer5 = 5;
        RealmsServerAddress diq6 = null;
        boolean boolean5 = false;
        boolean boolean6 = false;
        for (int integer6 = 0; integer6 < 40; ++integer6) {
            if (this.aborted()) {
                break;
            }
            try {
                diq6 = dia2.join(this.server.id);
                boolean3 = true;
            }
            catch (RetryCallException dji10) {
                integer5 = dji10.delaySeconds;
            }
            catch (RealmsServiceException djh10) {
                if (djh10.errorCode == 6002) {
                    boolean5 = true;
                    break;
                }
                if (djh10.errorCode == 6006) {
                    boolean6 = true;
                    break;
                }
                boolean4 = true;
                this.error(djh10.toString());
                GetServerDetailsTask.LOGGER.error("Couldn't connect to world", (Throwable)djh10);
                break;
            }
            catch (Exception exception10) {
                boolean4 = true;
                GetServerDetailsTask.LOGGER.error("Couldn't connect to world", (Throwable)exception10);
                this.error(exception10.getLocalizedMessage());
                break;
            }
            if (boolean3) {
                break;
            }
            this.sleep(integer5);
        }
        if (boolean5) {
            LongRunningTask.setScreen(new RealmsTermsScreen(this.lastScreen, this.mainScreen, this.server));
        }
        else if (boolean6) {
            if (this.server.ownerUUID.equals(Minecraft.getInstance().getUser().getUuid())) {
                LongRunningTask.setScreen(new RealmsBrokenWorldScreen(this.lastScreen, this.mainScreen, this.server.id, this.server.worldType == RealmsServer.WorldType.MINIGAME));
            }
            else {
                LongRunningTask.setScreen(new RealmsGenericErrorScreen(new TranslatableComponent("mco.brokenworld.nonowner.title"), new TranslatableComponent("mco.brokenworld.nonowner.error"), this.lastScreen));
            }
        }
        else if (!this.aborted() && !boolean4) {
            if (boolean3) {
                final RealmsServerAddress diq7 = diq6;
                if (diq7.resourcePackUrl != null && diq7.resourcePackHash != null) {
                    final Component mr10 = new TranslatableComponent("mco.configure.world.resourcepack.question.line1");
                    final Component mr11 = new TranslatableComponent("mco.configure.world.resourcepack.question.line2");
                    LongRunningTask.setScreen(new RealmsLongConfirmationScreen(boolean2 -> {
                        try {
                            if (boolean2) {
                                final RealmsGenericErrorScreen screen;
                                final Function<Throwable, Void> function4 = (Function<Throwable, Void>)(throwable -> {
                                    Minecraft.getInstance().getClientPackSource().clearServerPack();
                                    GetServerDetailsTask.LOGGER.error(throwable);
                                    new RealmsGenericErrorScreen(new TextComponent("Failed to download resource pack!"), this.lastScreen);
                                    LongRunningTask.setScreen(screen);
                                    return null;
                                });
                                try {
                                    final RealmsLongRunningMcoTaskScreen screen2;
                                    Minecraft.getInstance().getClientPackSource().downloadAndSelectResourcePack(diq7.resourcePackUrl, diq7.resourcePackHash).thenRun(() -> {
                                        new RealmsLongRunningMcoTaskScreen(this.lastScreen, new ConnectTask(this.lastScreen, diq7));
                                        this.setScreen(screen2);
                                    }).exceptionally(function4);
                                }
                                catch (Exception exception5) {
                                    function4.apply(exception5);
                                }
                            }
                            else {
                                LongRunningTask.setScreen(this.lastScreen);
                            }
                        }
                        finally {
                            if (this.connectLock != null && this.connectLock.isHeldByCurrentThread()) {
                                this.connectLock.unlock();
                            }
                        }
                    }, RealmsLongConfirmationScreen.Type.Info, mr10, mr11, true));
                }
                else {
                    this.setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, new ConnectTask(this.lastScreen, diq7)));
                }
            }
            else {
                this.error(new TranslatableComponent("mco.errorMessage.connectionFailure"));
            }
        }
    }
    
    private void sleep(final int integer) {
        try {
            Thread.sleep(integer * 1000);
        }
        catch (InterruptedException interruptedException3) {
            GetServerDetailsTask.LOGGER.warn(interruptedException3.getLocalizedMessage());
        }
    }
}
