// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.teleport.commands.warp;

import java.util.Iterator;
import com.hypixel.hytale.server.core.universe.world.World;
import java.util.List;
import com.hypixel.hytale.common.util.ListUtil;
import java.util.Collection;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import com.hypixel.hytale.server.core.entity.entities.player.pages.CustomUIPage;
import com.hypixel.hytale.builtin.teleport.Warp;
import java.util.Map;
import com.hypixel.hytale.builtin.teleport.WarpListPage;
import com.hypixel.hytale.sneakythrow.SneakyThrow;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.builtin.teleport.TeleportPlugin;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.permissions.HytalePermissions;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;

public class WarpListCommand extends CommandBase
{
    private static final int WARPS_PER_LIST_PAGE = 8;
    private static final Message MESSAGE_COMMANDS_TELEPORT_WARP_NOT_LOADED;
    private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
    private static final Message MESSAGE_COMMANDS_TELEPORT_WARP_NO_WARPS;
    private static final Message MESSAGE_COMMANDS_TELEPORT_WARP_PAGE_NUM_TOO_HIGH;
    @Nonnull
    private final OptionalArg<Integer> pageArg;
    static final /* synthetic */ boolean $assertionsDisabled;
    
    public WarpListCommand() {
        super("list", "server.commands.warp.list.desc");
        this.pageArg = this.withOptionalArg("page", "server.commands.warp.list.page.desc", ArgTypes.INTEGER);
        this.requirePermission(HytalePermissions.fromCommand("warp.list"));
    }
    
    @Override
    protected void executeSync(@Nonnull final CommandContext context) {
        if (!TeleportPlugin.get().isWarpsLoaded()) {
            context.sendMessage(WarpListCommand.MESSAGE_COMMANDS_TELEPORT_WARP_NOT_LOADED);
            return;
        }
        final Map<String, Warp> warps = TeleportPlugin.get().getWarps();
        if (context.isPlayer() && !this.pageArg.provided(context)) {
            final Ref<EntityStore> ref = context.senderAsPlayerRef();
            if (ref == null || !ref.isValid()) {
                context.sendMessage(WarpListCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                return;
            }
            final Store<EntityStore> store = ref.getStore();
            final World playerWorld = store.getExternalData().getWorld();
            playerWorld.execute(() -> {
                final Player playerComponent = store.getComponent(ref, Player.getComponentType());
                if (!WarpListCommand.$assertionsDisabled && playerComponent == null) {
                    throw new AssertionError();
                }
                else {
                    final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
                    if (!WarpListCommand.$assertionsDisabled && playerRefComponent == null) {
                        throw new AssertionError();
                    }
                    else {
                        playerComponent.getPageManager().openCustomPage(ref, store, new WarpListPage(playerRefComponent, warps, warp -> {
                            try {
                                WarpCommand.tryGo(context, warp, ref, store);
                            }
                            catch (final Exception e) {
                                throw SneakyThrow.sneakyThrow(e);
                            }
                        }));
                    }
                }
            });
        }
        else {
            if (warps.isEmpty()) {
                context.sendMessage(WarpListCommand.MESSAGE_COMMANDS_TELEPORT_WARP_NO_WARPS);
                return;
            }
            final int pageNumber = this.pageArg.provided(context) ? this.pageArg.get(context) : 1;
            if (pageNumber < 1) {
                context.sendMessage(Message.translation("server.commands.teleport.warp.pageNumTooLow").param("page", pageNumber));
                return;
            }
            final List<Warp> innerWarps = new ObjectArrayList<Warp>(warps.values());
            innerWarps.sort((o1, o2) -> o2.getCreationDate().compareTo(o1.getCreationDate()));
            final List<List<Warp>> paginated = ListUtil.partition(innerWarps, 8);
            if (paginated.size() < pageNumber) {
                context.sendMessage(WarpListCommand.MESSAGE_COMMANDS_TELEPORT_WARP_PAGE_NUM_TOO_HIGH);
            }
            else {
                context.sendMessage(Message.translation("server.commands.teleport.warp.listHeader").param("page", pageNumber).param("pages", paginated.size()));
                final int startIndex = (pageNumber - 1) * 8;
                final List<Warp> page = paginated.get(pageNumber - 1);
                int i = 1;
                for (final Warp w : page) {
                    context.sendMessage(Message.translation("server.commands.teleport.warp.listEntry").param("index", startIndex + i).param("name", w.getId()).param("creator", w.getCreator()));
                    ++i;
                }
            }
        }
    }
    
    static {
        MESSAGE_COMMANDS_TELEPORT_WARP_NOT_LOADED = Message.translation("server.commands.teleport.warp.notLoaded");
        MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        MESSAGE_COMMANDS_TELEPORT_WARP_NO_WARPS = Message.translation("server.commands.teleport.warp.noWarps");
        MESSAGE_COMMANDS_TELEPORT_WARP_PAGE_NUM_TOO_HIGH = Message.translation("server.commands.teleport.warp.pageNumTooHigh");
    }
}
