package com.mojang.realmsclient.gui.screens;

import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.components.AbstractSelectionList;
import javax.annotation.Nullable;
import net.minecraft.realms.NarrationHelper;
import java.util.Arrays;
import net.minecraft.realms.RealmsObjectSelectionList;
import java.text.SimpleDateFormat;
import org.apache.logging.log4j.LogManager;
import net.minecraft.client.gui.Font;
import java.util.Date;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import java.util.Iterator;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import net.minecraft.realms.RealmsLabel;
import net.minecraft.world.level.storage.LevelSummary;
import java.util.List;
import net.minecraft.client.gui.components.Button;
import java.text.DateFormat;
import org.apache.logging.log4j.Logger;
import net.minecraft.realms.RealmsScreen;

public class RealmsSelectFileToUploadScreen extends RealmsScreen {
    private static final Logger LOGGER;
    private static final DateFormat DATE_FORMAT;
    private final RealmsResetWorldScreen lastScreen;
    private final long worldId;
    private final int slotId;
    private Button uploadButton;
    private List<LevelSummary> levelList;
    private int selectedWorld;
    private WorldSelectionList worldSelectionList;
    private String worldLang;
    private String conversionLang;
    private RealmsLabel titleLabel;
    private RealmsLabel subtitleLabel;
    private RealmsLabel noWorldsLabel;
    private final Runnable callback;
    
    public RealmsSelectFileToUploadScreen(final long long1, final int integer, final RealmsResetWorldScreen dke, final Runnable runnable) {
        this.levelList = Lists.newArrayList();
        this.selectedWorld = -1;
        this.lastScreen = dke;
        this.worldId = long1;
        this.slotId = integer;
        this.callback = runnable;
    }
    
    private void loadLevelList() throws Exception {
        this.levelList = this.minecraft.getLevelSource().getLevelList().stream().sorted((daf1, daf2) -> {
            if (daf1.getLastPlayed() < daf2.getLastPlayed()) {
                return 1;
            }
            else if (daf1.getLastPlayed() > daf2.getLastPlayed()) {
                return -1;
            }
            else {
                return daf1.getLevelId().compareTo(daf2.getLevelId());
            }
        }).collect(Collectors.toList());
        for (final LevelSummary daf3 : this.levelList) {
            this.worldSelectionList.addEntry(daf3);
        }
    }
    
    public void init() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
        this.worldSelectionList = new WorldSelectionList();
        try {
            this.loadLevelList();
        }
        catch (Exception exception2) {
            RealmsSelectFileToUploadScreen.LOGGER.error("Couldn't load level list", (Throwable)exception2);
            this.minecraft.setScreen(new RealmsGenericErrorScreen(new TextComponent("Unable to load worlds"), Component.nullToEmpty(exception2.getMessage()), this.lastScreen));
            return;
        }
        this.worldLang = I18n.get("selectWorld.world");
        this.conversionLang = I18n.get("selectWorld.conversion");
        this.<WorldSelectionList>addWidget(this.worldSelectionList);
        this.uploadButton = this.<Button>addButton(new Button(this.width / 2 - 154, this.height - 32, 153, 20, new TranslatableComponent("mco.upload.button.name"), dni -> this.upload()));
        this.uploadButton.active = (this.selectedWorld >= 0 && this.selectedWorld < this.levelList.size());
        this.<Button>addButton(new Button(this.width / 2 + 6, this.height - 32, 153, 20, CommonComponents.GUI_BACK, dni -> this.minecraft.setScreen(this.lastScreen)));
        this.titleLabel = this.<RealmsLabel>addWidget(new RealmsLabel(new TranslatableComponent("mco.upload.select.world.title"), this.width / 2, 13, 16777215));
        this.subtitleLabel = this.<RealmsLabel>addWidget(new RealmsLabel(new TranslatableComponent("mco.upload.select.world.subtitle"), this.width / 2, RealmsScreen.row(-1), 10526880));
        if (this.levelList.isEmpty()) {
            this.noWorldsLabel = this.<RealmsLabel>addWidget(new RealmsLabel(new TranslatableComponent("mco.upload.select.world.none"), this.width / 2, this.height / 2 - 20, 16777215));
        }
        else {
            this.noWorldsLabel = null;
        }
        this.narrateLabels();
    }
    
    @Override
    public void removed() {
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
    }
    
    private void upload() {
        if (this.selectedWorld != -1 && !this.levelList.get(this.selectedWorld).isHardcore()) {
            final LevelSummary daf2 = this.levelList.get(this.selectedWorld);
            this.minecraft.setScreen(new RealmsUploadScreen(this.worldId, this.slotId, this.lastScreen, daf2, this.callback));
        }
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        this.renderBackground(dhl);
        this.worldSelectionList.render(dhl, integer2, integer3, float4);
        this.titleLabel.render(this, dhl);
        this.subtitleLabel.render(this, dhl);
        if (this.noWorldsLabel != null) {
            this.noWorldsLabel.render(this, dhl);
        }
        super.render(dhl, integer2, integer3, float4);
    }
    
    @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 static String gameModeName(final LevelSummary daf) {
        return daf.getGameMode().getDisplayName().getString();
    }
    
    private static String formatLastPlayed(final LevelSummary daf) {
        return RealmsSelectFileToUploadScreen.DATE_FORMAT.format(new Date(daf.getLastPlayed()));
    }
    
    static {
        LOGGER = LogManager.getLogger();
        DATE_FORMAT = new SimpleDateFormat();
    }
    
    class WorldSelectionList extends RealmsObjectSelectionList<RealmsSelectFileToUploadScreen.Entry> {
        public WorldSelectionList() {
            super(RealmsSelectFileToUploadScreen.this.width, RealmsSelectFileToUploadScreen.this.height, RealmsScreen.row(0), RealmsSelectFileToUploadScreen.this.height - 40, 36);
        }
        
        public void addEntry(final LevelSummary daf) {
            this.addEntry(new RealmsSelectFileToUploadScreen.Entry(daf));
        }
        
        @Override
        public int getMaxPosition() {
            return RealmsSelectFileToUploadScreen.this.levelList.size() * 36;
        }
        
        public boolean isFocused() {
            return RealmsSelectFileToUploadScreen.this.getFocused() == this;
        }
        
        public void renderBackground(final PoseStack dhl) {
            RealmsSelectFileToUploadScreen.this.renderBackground(dhl);
        }
        
        @Override
        public void selectItem(final int integer) {
            this.setSelectedItem(integer);
            if (integer != -1) {
                final LevelSummary daf3 = RealmsSelectFileToUploadScreen.this.levelList.get(integer);
                final String string4 = I18n.get("narrator.select.list.position", integer + 1, RealmsSelectFileToUploadScreen.this.levelList.size());
                final String string5 = NarrationHelper.join(Arrays.<String>asList(daf3.getLevelName(), formatLastPlayed(daf3), gameModeName(daf3), string4));
                NarrationHelper.now(I18n.get("narrator.select", string5));
            }
        }
        
        @Override
        public void setSelected(@Nullable final RealmsSelectFileToUploadScreen.Entry a) {
            super.setSelected(a);
            RealmsSelectFileToUploadScreen.this.selectedWorld = this.children().indexOf(a);
            RealmsSelectFileToUploadScreen.this.uploadButton.active = (RealmsSelectFileToUploadScreen.this.selectedWorld >= 0 && RealmsSelectFileToUploadScreen.this.selectedWorld < this.getItemCount() && !RealmsSelectFileToUploadScreen.this.levelList.get(RealmsSelectFileToUploadScreen.this.selectedWorld).isHardcore());
        }
    }
    
    class Entry extends ObjectSelectionList.Entry<Entry> {
        private final LevelSummary levelSummary;
        
        public Entry(final LevelSummary daf) {
            this.levelSummary = daf;
        }
        
        @Override
        public void render(final PoseStack dhl, final int integer2, final int integer3, final int integer4, final int integer5, final int integer6, final int integer7, final int integer8, final boolean boolean9, final float float10) {
            this.renderItem(dhl, this.levelSummary, integer2, integer4, integer3);
        }
        
        @Override
        public boolean mouseClicked(final double double1, final double double2, final int integer) {
            RealmsSelectFileToUploadScreen.this.worldSelectionList.selectItem(RealmsSelectFileToUploadScreen.this.levelList.indexOf(this.levelSummary));
            return true;
        }
        
        protected void renderItem(final PoseStack dhl, final LevelSummary daf, final int integer3, final int integer4, final int integer5) {
            String string7 = daf.getLevelName();
            if (string7 == null || string7.isEmpty()) {
                string7 = RealmsSelectFileToUploadScreen.this.worldLang + " " + (integer3 + 1);
            }
            String string8 = daf.getLevelId();
            string8 = string8 + " (" + formatLastPlayed(daf);
            string8 += ")";
            String string9 = "";
            if (daf.isRequiresConversion()) {
                string9 = RealmsSelectFileToUploadScreen.this.conversionLang + " " + string9;
            }
            else {
                string9 = gameModeName(daf);
                if (daf.isHardcore()) {
                    string9 = ChatFormatting.DARK_RED + I18n.get("mco.upload.hardcore") + ChatFormatting.RESET;
                }
                if (daf.hasCheats()) {
                    string9 = string9 + ", " + I18n.get("selectWorld.cheats");
                }
            }
            RealmsSelectFileToUploadScreen.this.font.draw(dhl, string7, (float)(integer4 + 2), (float)(integer5 + 1), 16777215);
            RealmsSelectFileToUploadScreen.this.font.draw(dhl, string8, (float)(integer4 + 2), (float)(integer5 + 12), 8421504);
            RealmsSelectFileToUploadScreen.this.font.draw(dhl, string9, (float)(integer4 + 2), (float)(integer5 + 12 + 10), 8421504);
        }
    }
}
