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

package com.hypixel.hytale.builtin.buildertools.prefabeditor.commands;

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.server.core.prefab.selection.standard.BlockSelection;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditingMetadata;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditSession;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.PrefabEditSessionManager;
import java.util.UUID;
import com.hypixel.hytale.server.core.command.system.CommandSender;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.saving.PrefabSaver;
import java.nio.file.Path;
import com.hypixel.hytale.server.core.modules.singleplayer.SingleplayerModule;
import com.hypixel.hytale.common.util.PathUtil;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.saving.PrefabSaverSettings;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.server.core.entity.entities.Player;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.builtin.buildertools.prefabeditor.enums.PrefabRootDirectory;
import com.hypixel.hytale.server.core.command.system.arguments.system.DefaultArg;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractAsyncPlayerCommand;

public class PrefabEditSaveAsCommand extends AbstractAsyncPlayerCommand
{
    private final RequiredArg<String> fileNameArg;
    private final DefaultArg<PrefabRootDirectory> prefabPathArg;
    private final FlagArg noEntitiesArg;
    private final FlagArg overwriteArg;
    private final FlagArg emptyArg;
    private final FlagArg noUpdateArg;
    
    public PrefabEditSaveAsCommand() {
        super("saveas", "server.commands.editprefab.saveAs.desc");
        this.fileNameArg = this.withRequiredArg("fileNameArg", "server.commands.editprefab.save.saveAs.desc", ArgTypes.STRING);
        this.prefabPathArg = this.withDefaultArg("prefabPath", "server.commands.editprefab.save.path.desc", ArgTypes.forEnum("PrefabPath", PrefabRootDirectory.class), PrefabRootDirectory.SERVER, "server.commands.editprefab.save.path.default.desc");
        this.noEntitiesArg = this.withFlagArg("noEntities", "server.commands.editprefab.save.noEntities.desc");
        this.overwriteArg = this.withFlagArg("overwrite", "server.commands.editprefab.save.overwrite.desc");
        this.emptyArg = this.withFlagArg("empty", "server.commands.editprefab.save.empty.desc");
        this.noUpdateArg = this.withFlagArg("noUpdate", "server.commands.editprefab.saveAs.noUpdate.desc");
    }
    
    @Nonnull
    @Override
    protected CompletableFuture<Void> executeAsync(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final UUID uuid = playerRef.getUuid();
        final PrefabEditSessionManager prefabEditSessionManager = BuilderToolsPlugin.get().getPrefabEditSessionManager();
        final PrefabEditSession prefabEditSession = prefabEditSessionManager.getPrefabEditSession(uuid);
        if (prefabEditSession == null) {
            context.sendMessage(Message.translation("server.commands.editprefab.notInEditSession"));
            return CompletableFuture.completedFuture((Void)null);
        }
        final PrefabSaverSettings prefabSaverSettings = new PrefabSaverSettings();
        prefabSaverSettings.setBlocks(true);
        prefabSaverSettings.setEntities(!this.noEntitiesArg.provided(context));
        prefabSaverSettings.setOverwriteExisting(((Argument<Arg, Boolean>)this.overwriteArg).get(context));
        prefabSaverSettings.setEmpty(((Argument<Arg, Boolean>)this.emptyArg).get(context));
        final Path prefabRootPath = this.prefabPathArg.get(context).getPrefabPath();
        if (!PathUtil.isChildOf(prefabRootPath, prefabRootPath.resolve(this.fileNameArg.get(context))) && !SingleplayerModule.isOwner(playerRef)) {
            context.sendMessage(Message.translation("server.builderTools.attemptedToSaveOutsidePrefabsDir"));
            return CompletableFuture.completedFuture((Void)null);
        }
        Path prefabSavePath = prefabRootPath.resolve(this.fileNameArg.get(context));
        if (prefabSavePath.toString().endsWith("/")) {
            context.sendMessage(Message.translation("server.commands.editprefab.saveAs.errors.notAFile"));
            return CompletableFuture.completedFuture((Void)null);
        }
        if (!prefabEditSession.toString().endsWith(".prefab.json")) {
            prefabSavePath = Path.of(String.valueOf(prefabSavePath) + ".prefab.json", new String[0]);
        }
        final PrefabEditingMetadata selectedPrefab = prefabEditSession.getSelectedPrefab(uuid);
        if (selectedPrefab == null) {
            context.sendMessage(Message.translation("server.commands.editprefab.noPrefabSelected"));
            return CompletableFuture.completedFuture((Void)null);
        }
        final BlockSelection selection = BuilderToolsPlugin.getState(playerComponent, playerRef).getSelection();
        if (!selectedPrefab.getMinPoint().equals(selection.getSelectionMin()) || !selectedPrefab.getMaxPoint().equals(selection.getSelectionMax())) {
            context.sendMessage(Message.translation("server.commands.editprefab.save.selectionMismatch"));
            return CompletableFuture.completedFuture((Void)null);
        }
        if (!this.noUpdateArg.provided(context)) {
            prefabEditSessionManager.updatePathOfLoadedPrefab(selectedPrefab.getPrefabPath(), prefabSavePath);
            selectedPrefab.setPrefabPath(prefabSavePath);
        }
        return PrefabSaver.savePrefab(playerComponent, world, prefabSavePath, selectedPrefab.getAnchorPoint(), selectedPrefab.getMinPoint(), selectedPrefab.getMaxPoint(), selectedPrefab.getPastePosition(), selectedPrefab.getOriginalFileAnchor(), prefabSaverSettings).thenAccept(success -> context.sendMessage(Message.translation("server.commands.editprefab.save." + (((boolean)success) ? "success" : "failure")).param("name", selectedPrefab.getPrefabPath().toString())));
    }
}
