package com.mojang.realmsclient.gui.screens;

import net.minecraft.network.chat.FormattedText;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.Button;
import net.minecraft.realms.NarrationHelper;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.TextComponent;
import com.mojang.realmsclient.exception.RealmsServiceException;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.realms.RealmsScreen;

public class RealmsGenericErrorScreen extends RealmsScreen {
    private final Screen nextScreen;
    private Component line1;
    private Component line2;
    
    public RealmsGenericErrorScreen(final RealmsServiceException djh, final Screen dqs) {
        this.nextScreen = dqs;
        this.errorMessage(djh);
    }
    
    public RealmsGenericErrorScreen(final Component mr, final Screen dqs) {
        this.nextScreen = dqs;
        this.errorMessage(mr);
    }
    
    public RealmsGenericErrorScreen(final Component mr1, final Component mr2, final Screen dqs) {
        this.nextScreen = dqs;
        this.errorMessage(mr1, mr2);
    }
    
    private void errorMessage(final RealmsServiceException djh) {
        if (djh.errorCode == -1) {
            this.line1 = new TextComponent("An error occurred (" + djh.httpResultCode + "):");
            this.line2 = new TextComponent(djh.httpResponseContent);
        }
        else {
            this.line1 = new TextComponent("Realms (" + djh.errorCode + "):");
            final String string3 = "mco.errorMessage." + djh.errorCode;
            this.line2 = (I18n.exists(string3) ? new TranslatableComponent(string3) : Component.nullToEmpty(djh.errorMsg));
        }
    }
    
    private void errorMessage(final Component mr) {
        this.line1 = new TextComponent("An error occurred: ");
        this.line2 = mr;
    }
    
    private void errorMessage(final Component mr1, final Component mr2) {
        this.line1 = mr1;
        this.line2 = mr2;
    }
    
    public void init() {
        NarrationHelper.now(this.line1.getString() + ": " + this.line2.getString());
        this.<Button>addButton(new Button(this.width / 2 - 100, this.height - 52, 200, 20, new TextComponent("Ok"), dni -> this.minecraft.setScreen(this.nextScreen)));
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        this.drawCenteredString(dhl, this.font, this.line1, this.width / 2, 80, 16777215);
        this.drawCenteredString(dhl, this.font, this.line2, this.width / 2, 100, 16711680);
        super.render(dhl, integer2, integer3, float4);
    }
}
