package com.mojang.realmsclient.gui.screens;

import org.apache.logging.log4j.LogManager;
import net.minecraft.client.resources.language.I18n;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.Util;
import com.mojang.realmsclient.exception.RealmsServiceException;
import com.mojang.realmsclient.util.task.LongRunningTask;
import com.mojang.realmsclient.util.task.GetServerDetailsTask;
import java.util.concurrent.locks.ReentrantLock;
import com.mojang.realmsclient.client.RealmsClient;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.realmsclient.dto.RealmsServer;
import com.mojang.realmsclient.RealmsMainScreen;
import net.minecraft.client.gui.screens.Screen;
import org.apache.logging.log4j.Logger;
import net.minecraft.realms.RealmsScreen;

public class RealmsTermsScreen extends RealmsScreen {
    private static final Logger LOGGER;
    private final Screen lastScreen;
    private final RealmsMainScreen mainScreen;
    private final RealmsServer realmsServer;
    private boolean onLink;
    private final String realmsToSUrl = "https://minecraft.net/realms/terms";
    
    public RealmsTermsScreen(final Screen dqs, final RealmsMainScreen dhv, final RealmsServer dip) {
        this.lastScreen = dqs;
        this.mainScreen = dhv;
        this.realmsServer = dip;
    }
    
    public void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        final int integer2 = this.width / 4 - 2;
        this.<Button>addButton(new Button(this.width / 4, RealmsScreen.row(12), integer2, 20, new TranslatableComponent("mco.terms.buttons.agree"), dni -> this.agreedToTos()));
        this.<Button>addButton(new Button(this.width / 2 + 4, RealmsScreen.row(12), integer2, 20, new TranslatableComponent("mco.terms.buttons.disagree"), dni -> this.minecraft.setScreen(this.lastScreen)));
    }
    
    @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 void agreedToTos() {
        final RealmsClient dia2 = RealmsClient.create();
        try {
            dia2.agreeToTos();
            this.minecraft.setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, new GetServerDetailsTask(this.mainScreen, this.lastScreen, this.realmsServer, new ReentrantLock())));
        }
        catch (RealmsServiceException djh3) {
            RealmsTermsScreen.LOGGER.error("Couldn't agree to TOS");
        }
    }
    
    @Override
    public boolean mouseClicked(final double double1, final double double2, final int integer) {
        if (this.onLink) {
            this.minecraft.keyboardHandler.setClipboard("https://minecraft.net/realms/terms");
            Util.getPlatform().openUri("https://minecraft.net/realms/terms");
            return true;
        }
        return super.mouseClicked(double1, double2, integer);
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        this.drawCenteredString(dhl, this.font, I18n.get("mco.terms.title"), this.width / 2, 17, 16777215);
        this.font.draw(dhl, I18n.get("mco.terms.sentence.1"), (float)(this.width / 2 - 120), (float)RealmsScreen.row(5), 16777215);
        final int integer4 = this.font.width(I18n.get("mco.terms.sentence.1"));
        final int integer5 = this.width / 2 - 121 + integer4;
        final int integer6 = RealmsScreen.row(5);
        final int integer7 = integer5 + this.font.width("mco.terms.sentence.2") + 1;
        final int n = integer6 + 1;
        this.font.getClass();
        final int integer8 = n + 9;
        if (integer5 <= integer2 && integer2 <= integer7 && integer6 <= integer3 && integer3 <= integer8) {
            this.onLink = true;
            this.font.draw(dhl, " " + I18n.get("mco.terms.sentence.2"), (float)(this.width / 2 - 120 + integer4), (float)RealmsScreen.row(5), 7107012);
        }
        else {
            this.onLink = false;
            this.font.draw(dhl, " " + I18n.get("mco.terms.sentence.2"), (float)(this.width / 2 - 120 + integer4), (float)RealmsScreen.row(5), 3368635);
        }
        super.render(dhl, integer2, integer3, float4);
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
