package com.mojang.realmsclient.gui.screens;

import org.apache.logging.log4j.LogManager;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.realms.NarrationHelper;
import net.minecraft.client.resources.language.I18n;
import com.mojang.realmsclient.client.RealmsClient;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.gui.screens.Screen;
import com.mojang.realmsclient.dto.RealmsServer;
import net.minecraft.client.gui.components.EditBox;
import org.apache.logging.log4j.Logger;
import net.minecraft.realms.RealmsScreen;

public class RealmsInviteScreen extends RealmsScreen {
    private static final Logger LOGGER;
    private EditBox profileName;
    private final RealmsServer serverData;
    private final RealmsConfigureWorldScreen configureScreen;
    private final Screen lastScreen;
    private String errorMsg;
    private boolean showError;
    
    public RealmsInviteScreen(final RealmsConfigureWorldScreen djr, final Screen dqs, final RealmsServer dip) {
        this.configureScreen = djr;
        this.lastScreen = dqs;
        this.serverData = dip;
    }
    
    @Override
    public void tick() {
        this.profileName.tick();
    }
    
    public void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        this.<EditBox>addWidget(this.profileName = new EditBox(this.minecraft.font, this.width / 2 - 100, RealmsScreen.row(2), 200, 20, null, new TranslatableComponent("mco.configure.world.invite.profile.name")));
        this.setInitialFocus(this.profileName);
        this.<Button>addButton(new Button(this.width / 2 - 100, RealmsScreen.row(10), 200, 20, new TranslatableComponent("mco.configure.world.buttons.invite"), dni -> this.onInvite()));
        this.<Button>addButton(new Button(this.width / 2 - 100, RealmsScreen.row(12), 200, 20, CommonComponents.GUI_CANCEL, dni -> this.minecraft.setScreen(this.lastScreen)));
    }
    
    @Override
    public void removed() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
    }
    
    private void onInvite() {
        final RealmsClient dia2 = RealmsClient.create();
        if (this.profileName.getValue() == null || this.profileName.getValue().isEmpty()) {
            this.showError(I18n.get("mco.configure.world.players.error"));
            return;
        }
        try {
            final RealmsServer dip3 = dia2.invite(this.serverData.id, this.profileName.getValue().trim());
            if (dip3 != null) {
                this.serverData.players = dip3.players;
                this.minecraft.setScreen(new RealmsPlayerScreen(this.configureScreen, this.serverData));
            }
            else {
                this.showError(I18n.get("mco.configure.world.players.error"));
            }
        }
        catch (Exception exception3) {
            RealmsInviteScreen.LOGGER.error("Couldn't invite user");
            this.showError(I18n.get("mco.configure.world.players.error"));
        }
    }
    
    private void showError(final String string) {
        this.showError = true;
        NarrationHelper.now(this.errorMsg = string);
    }
    
    @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);
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        this.font.draw(dhl, I18n.get("mco.configure.world.invite.profile.name"), (float)(this.width / 2 - 100), (float)RealmsScreen.row(1), 10526880);
        if (this.showError) {
            this.drawCenteredString(dhl, this.font, this.errorMsg, this.width / 2, RealmsScreen.row(5), 16711680);
        }
        this.profileName.render(dhl, integer2, integer3, float4);
        super.render(dhl, integer2, integer3, float4);
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
