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

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

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.util.AssetUtil;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import java.nio.file.Path;
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 UpdateAssetsCommand extends AbstractCommandCollection
{
    public UpdateAssetsCommand() {
        super("assets", "server.commands.git.assets.desc");
        this.addSubCommand(new UpdateAssetsStatusCommand());
        this.addSubCommand(new UpdateAssetsResetCommand());
        this.addSubCommand(new UpdateAssetsPullCommand());
    }
    
    private abstract static class UpdateAssetsGitCommand extends AbstractAsyncCommand
    {
        protected UpdateAssetsGitCommand(@Nonnull final String name, @Nonnull final String description) {
            super(name, description);
        }
        
        @Nonnull
        protected abstract String[] getCommand(@Nonnull final Path p0);
        
        @Nonnull
        @Override
        protected CompletableFuture<Void> executeAsync(@Nonnull final CommandContext context) {
            return CompletableFuture.runAsync(() -> {
                final Path assetPath = AssetUtil.getHytaleAssetsPath();
                Path gitPath = null;
                if (Files.exists(assetPath.resolve(".git"), new LinkOption[0])) {
                    gitPath = assetPath;
                }
                else {
                    final Path parent = PathUtil.getParent(assetPath.toAbsolutePath());
                    if (Files.exists(parent.resolve(".git"), new LinkOption[0])) {
                        gitPath = parent;
                    }
                }
                if (gitPath == null) {
                    context.sendMessage(Message.translation("server.general.pathNotGitRepo").param("path", assetPath.toString()));
                }
                else {
                    final String[] processCommand = this.getCommand(gitPath);
                    final String commandDisplay = String.join(" ", (CharSequence[])processCommand);
                    try {
                        context.sendMessage(Message.translation("server.commands.git.running").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();
                        }
                    }
                    catch (final IOException e2) {
                        context.sendMessage(Message.translation("server.commands.git.failed").param("cmd", commandDisplay).param("msg", e2.getMessage()));
                    }
                }
            });
        }
    }
    
    private static class UpdateAssetsStatusCommand extends UpdateAssetsGitCommand
    {
        public UpdateAssetsStatusCommand() {
            super("status", "server.commands.git.assets.status.desc");
        }
        
        @Nonnull
        @Override
        protected String[] getCommand(@Nonnull final Path gitPath) {
            return new String[] { "git", "status" };
        }
    }
    
    private static class UpdateAssetsResetCommand extends UpdateAssetsGitCommand
    {
        public UpdateAssetsResetCommand() {
            super("reset", "server.commands.git.assets.reset.desc");
        }
        
        @Nonnull
        @Override
        protected String[] getCommand(@Nonnull final Path gitPath) {
            return new String[] { "git", "reset", "--hard", "head" };
        }
    }
    
    private static class UpdateAssetsPullCommand extends UpdateAssetsGitCommand
    {
        public UpdateAssetsPullCommand() {
            super("pull", "server.commands.git.assets.pull.desc");
        }
        
        @Nonnull
        @Override
        protected String[] getCommand(@Nonnull final Path gitPath) {
            final Path script = gitPath.resolve("../../updateAssets.sh");
            if (Files.exists(script, new LinkOption[0])) {
                final Path relative = gitPath.relativize(script);
                return new String[] { "sh", relative.toString() };
            }
            return new String[] { "git", "pull" };
        }
    }
}
