package com.mojang.realmsclient.gui.screens;

import org.apache.logging.log4j.LogManager;
import net.minecraft.client.Minecraft;
import java.util.Locale;
import com.mojang.blaze3d.vertex.PoseStack;
import java.util.Calendar;
import java.text.DateFormat;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import com.mojang.realmsclient.exception.RealmsServiceException;
import com.mojang.realmsclient.client.RealmsClient;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.components.Button;
import net.minecraft.Util;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.realms.NarrationHelper;
import net.minecraft.client.resources.language.I18n;
import com.mojang.realmsclient.dto.Subscription;
import com.mojang.realmsclient.dto.RealmsServer;
import net.minecraft.client.gui.screens.Screen;
import org.apache.logging.log4j.Logger;
import net.minecraft.realms.RealmsScreen;

public class RealmsSubscriptionInfoScreen extends RealmsScreen {
    private static final Logger LOGGER;
    private final Screen lastScreen;
    private final RealmsServer serverData;
    private final Screen mainScreen;
    private final String subscriptionTitle;
    private final String subscriptionStartLabelText;
    private final String timeLeftLabelText;
    private final String daysLeftLabelText;
    private int daysLeft;
    private String startDate;
    private Subscription.SubscriptionType type;
    
    public RealmsSubscriptionInfoScreen(final Screen dqs1, final RealmsServer dip, final Screen dqs3) {
        this.lastScreen = dqs1;
        this.serverData = dip;
        this.mainScreen = dqs3;
        this.subscriptionTitle = I18n.get("mco.configure.world.subscription.title");
        this.subscriptionStartLabelText = I18n.get("mco.configure.world.subscription.start");
        this.timeLeftLabelText = I18n.get("mco.configure.world.subscription.timeleft");
        this.daysLeftLabelText = I18n.get("mco.configure.world.subscription.recurring.daysleft");
    }
    
    public void init() {
        this.getSubscription(this.serverData.id);
        NarrationHelper.now(this.subscriptionTitle, this.subscriptionStartLabelText, this.startDate, this.timeLeftLabelText, this.daysLeftPresentation(this.daysLeft));
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        final String string3;
        this.<Button>addButton(new Button(this.width / 2 - 100, RealmsScreen.row(6), 200, 20, new TranslatableComponent("mco.configure.world.subscription.extend"), dni -> {
            string3 = "https://aka.ms/ExtendJavaRealms?subscriptionId=" + this.serverData.remoteSubscriptionId + "&profileId=" + this.minecraft.getUser().getUuid();
            this.minecraft.keyboardHandler.setClipboard(string3);
            Util.getPlatform().openUri(string3);
            return;
        }));
        this.<Button>addButton(new Button(this.width / 2 - 100, RealmsScreen.row(12), 200, 20, CommonComponents.GUI_BACK, dni -> this.minecraft.setScreen(this.lastScreen)));
        if (this.serverData.expired) {
            final Component mr3;
            final Component mr4;
            this.<Button>addButton(new Button(this.width / 2 - 100, RealmsScreen.row(10), 200, 20, new TranslatableComponent("mco.configure.world.delete.button"), dni -> {
                mr3 = new TranslatableComponent("mco.configure.world.delete.question.line1");
                mr4 = new TranslatableComponent("mco.configure.world.delete.question.line2");
                this.minecraft.setScreen(new RealmsLongConfirmationScreen(this::deleteRealm, RealmsLongConfirmationScreen.Type.Warning, mr3, mr4, true));
            }));
        }
    }
    
    private void deleteRealm(final boolean boolean1) {
        if (boolean1) {
            new Thread("Realms-delete-realm") {
                @Override
                public void run() {
                    try {
                        final RealmsClient dia2 = RealmsClient.create();
                        dia2.deleteWorld(RealmsSubscriptionInfoScreen.this.serverData.id);
                    }
                    catch (RealmsServiceException djh2) {
                        RealmsSubscriptionInfoScreen.LOGGER.error("Couldn't delete world");
                        RealmsSubscriptionInfoScreen.LOGGER.error(djh2);
                    }
                    RealmsSubscriptionInfoScreen.this.minecraft.execute(() -> RealmsSubscriptionInfoScreen.this.minecraft.setScreen(RealmsSubscriptionInfoScreen.this.mainScreen));
                }
            }.start();
        }
        this.minecraft.setScreen(this);
    }
    
    private void getSubscription(final long long1) {
        final RealmsClient dia4 = RealmsClient.create();
        try {
            final Subscription diz5 = dia4.subscriptionFor(long1);
            this.daysLeft = diz5.daysLeft;
            this.startDate = this.localPresentation(diz5.startDate);
            this.type = diz5.type;
        }
        catch (RealmsServiceException djh5) {
            RealmsSubscriptionInfoScreen.LOGGER.error("Couldn't get subscription");
            this.minecraft.setScreen(new RealmsGenericErrorScreen(djh5, this.lastScreen));
        }
    }
    
    private String localPresentation(final long long1) {
        final Calendar calendar4 = new GregorianCalendar(TimeZone.getDefault());
        calendar4.setTimeInMillis(long1);
        return DateFormat.getDateTimeInstance().format(calendar4.getTime());
    }
    
    @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);
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        final int integer4 = this.width / 2 - 100;
        this.drawCenteredString(dhl, this.font, this.subscriptionTitle, this.width / 2, 17, 16777215);
        this.font.draw(dhl, this.subscriptionStartLabelText, (float)integer4, (float)RealmsScreen.row(0), 10526880);
        this.font.draw(dhl, this.startDate, (float)integer4, (float)RealmsScreen.row(1), 16777215);
        if (this.type == Subscription.SubscriptionType.NORMAL) {
            this.font.draw(dhl, this.timeLeftLabelText, (float)integer4, (float)RealmsScreen.row(3), 10526880);
        }
        else if (this.type == Subscription.SubscriptionType.RECURRING) {
            this.font.draw(dhl, this.daysLeftLabelText, (float)integer4, (float)RealmsScreen.row(3), 10526880);
        }
        this.font.draw(dhl, this.daysLeftPresentation(this.daysLeft), (float)integer4, (float)RealmsScreen.row(4), 16777215);
        super.render(dhl, integer2, integer3, float4);
    }
    
    private String daysLeftPresentation(final int integer) {
        if (integer == -1 && this.serverData.expired) {
            return I18n.get("mco.configure.world.subscription.expired");
        }
        if (integer <= 1) {
            return I18n.get("mco.configure.world.subscription.less_than_a_day");
        }
        final int integer2 = integer / 30;
        final int integer3 = integer % 30;
        final StringBuilder stringBuilder5 = new StringBuilder();
        if (integer2 > 0) {
            stringBuilder5.append(integer2).append(" ");
            if (integer2 == 1) {
                stringBuilder5.append(I18n.get("mco.configure.world.subscription.month").toLowerCase(Locale.ROOT));
            }
            else {
                stringBuilder5.append(I18n.get("mco.configure.world.subscription.months").toLowerCase(Locale.ROOT));
            }
        }
        if (integer3 > 0) {
            if (stringBuilder5.length() > 0) {
                stringBuilder5.append(", ");
            }
            stringBuilder5.append(integer3).append(" ");
            if (integer3 == 1) {
                stringBuilder5.append(I18n.get("mco.configure.world.subscription.day").toLowerCase(Locale.ROOT));
            }
            else {
                stringBuilder5.append(I18n.get("mco.configure.world.subscription.days").toLowerCase(Locale.ROOT));
            }
        }
        return stringBuilder5.toString();
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
