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

package com.hypixel.hytale.server.core.command.commands.utility.git;

import java.nio.file.Path;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.io.BufferedReader;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.common.util.PathUtil;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import com.hypixel.hytale.server.core.prefab.PrefabStore;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractAsyncCommand;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractCommandCollection;

public class UpdatePrefabsCommand extends AbstractCommandCollection
{
    public UpdatePrefabsCommand() {
        super("prefabs", "server.commands.git.prefabs.desc");
        this.addSubCommand(new UpdatePrefabsStatusCommand());
        this.addSubCommand(new UpdatePrefabsCommitCommand());
        this.addSubCommand(new UpdatePrefabsPullCommand());
        this.addSubCommand(new UpdatePrefabsPushCommand());
        this.addSubCommand(new UpdatePrefabsAllCommand());
    }
    
    private abstract static class UpdatePrefabsGitCommand extends AbstractAsyncCommand
    {
        protected UpdatePrefabsGitCommand(@Nonnull final String name, @Nonnull final String description) {
            super(name, description);
        }
        
        @Nonnull
        protected abstract String[][] getCommands(@Nonnull final String p0);
        
        @Nonnull
        @Override
        protected CompletableFuture<Void> executeAsync(@Nonnull final CommandContext context) {
            return CompletableFuture.runAsync(() -> {
                final Path prefabsPath = PrefabStore.get().getServerPrefabsPath();
                Path gitPath = null;
                if (Files.isDirectory(prefabsPath.resolve(".git"), new LinkOption[0])) {
                    gitPath = prefabsPath;
                }
                else {
                    final Path parent = PathUtil.getParent(prefabsPath);
                    if (Files.isDirectory(parent.resolve(".git"), new LinkOption[0])) {
                        gitPath = parent;
                    }
                }
                if (gitPath == null) {
                    context.sendMessage(Message.translation("server.general.pathNotGitRepo").param("path", prefabsPath.toString()));
                }
                else {
                    String senderDisplayName = context.sender().getDisplayName().replaceAll("[^a-zA-Z0-9 ._-]", "");
                    if (senderDisplayName.isEmpty()) {
                        senderDisplayName = "Unknown";
                    }
                    final String finalSenderDisplayName = senderDisplayName;
                    final String[][] arr$;
                    final String[][] cmds = arr$ = this.getCommands(finalSenderDisplayName);
                    for (final String[] processCommand : arr$) {
                        try {
                            final String commandDisplay = String.join(" ", (CharSequence[])processCommand);
                            context.sendMessage(Message.translation("server.commands.git.runningCmd").param("cmd", commandDisplay));
                            final Process process = new ProcessBuilder(processCommand).directory(gitPath.toFile()).start();
                            try {
                                process.waitFor();
                                new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
                                final BufferedReader bufferedReader;
                                final BufferedReader reader = bufferedReader;
                                while (true) {
                                    final String line = reader.readLine();
                                    final Object o;
                                    if (o != null) {
                                        context.sendMessage(Message.translation("server.commands.git.runningStdOut").param("cmd", commandDisplay).param("line", line));
                                    }
                                    else {
                                        break;
                                    }
                                }
                                new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8));
                                final BufferedReader bufferedReader2;
                                final BufferedReader reader2 = bufferedReader2;
                                while (true) {
                                    final String line2 = reader2.readLine();
                                    final Object o2;
                                    if (o2 != null) {
                                        context.sendMessage(Message.translation("server.commands.git.runningStdErr").param("cmd", commandDisplay).param("line", line2));
                                    }
                                    else {
                                        break;
                                    }
                                }
                                context.sendMessage(Message.translation("server.commands.git.done").param("cmd", commandDisplay));
                            }
                            catch (final InterruptedException e) {
                                Thread.currentThread().interrupt();
                                break;
                            }
                        }
                        catch (final IOException e2) {
                            context.sendMessage(Message.translation("server.commands.git.failed").param("cmd", String.join(" ", (CharSequence[])processCommand)).param("msg", e2.getMessage()));
                            break;
                        }
                    }
                }
            });
        }
    }
    
    private static class UpdatePrefabsStatusCommand extends UpdatePrefabsGitCommand
    {
        public UpdatePrefabsStatusCommand() {
            super("status", "server.commands.git.prefabs.status.desc");
        }
        
        @Nonnull
        @Override
        protected String[][] getCommands(@Nonnull final String senderDisplayName) {
            return new String[][] { { "git", "status" }, { "git", "submodule", "foreach", "git", "status" } };
        }
    }
    
    private static class UpdatePrefabsCommitCommand extends UpdatePrefabsGitCommand
    {
        public UpdatePrefabsCommitCommand() {
            super("commit", "server.commands.git.prefabs.commit.desc");
        }
        
        @Nonnull
        @Override
        protected String[][] getCommands(@Nonnull final String senderDisplayName) {
            return new String[][] { { "git", "add", "--all", "." }, { "git", "commit", "-am", "Update prefabs by " + senderDisplayName }, { "git", "submodule", "foreach", "git", "add", "--all", "." }, { "git", "submodule", "foreach", "git", "commit", "-am", "\"Update prefabs by " + senderDisplayName } };
        }
    }
    
    private static class UpdatePrefabsPullCommand extends UpdatePrefabsGitCommand
    {
        public UpdatePrefabsPullCommand() {
            super("pull", "server.commands.git.prefabs.pull.desc");
        }
        
        @Nonnull
        @Override
        protected String[][] getCommands(@Nonnull final String senderDisplayName) {
            return new String[][] { { "git", "pull" }, { "git", "submodule", "foreach", "git", "pull" } };
        }
    }
    
    private static class UpdatePrefabsPushCommand extends UpdatePrefabsGitCommand
    {
        public UpdatePrefabsPushCommand() {
            super("push", "server.commands.git.prefabs.push.desc");
        }
        
        @Nonnull
        @Override
        protected String[][] getCommands(@Nonnull final String senderDisplayName) {
            return new String[][] { { "git", "push", "origin", "master" }, { "git", "submodule", "foreach", "git", "push" } };
        }
    }
    
    private static class UpdatePrefabsAllCommand extends UpdatePrefabsGitCommand
    {
        public UpdatePrefabsAllCommand() {
            super("all", "server.commands.git.prefabs.all.desc");
        }
        
        @Nonnull
        @Override
        protected String[][] getCommands(@Nonnull final String senderDisplayName) {
            return new String[][] { { "git", "submodule", "foreach", "git", "add", "--all", "." }, { "git", "submodule", "foreach", "git", "commit", "-am", "\"Update prefabs by " + senderDisplayName }, { "git", "submodule", "foreach", "git", "pull" }, { "git", "submodule", "foreach", "git", "push" }, { "git", "add", "--all", "." }, { "git", "commit", "-am", "Update prefabs by " + senderDisplayName }, { "git", "pull" }, { "git", "push" } };
        }
    }
}
