package org.goblin.commander.impl;

import org.apache.commons.lang3.BooleanUtils;
import org.goblin.commander.GoblinCommander;
import org.goblin.dto.Executable;
import org.goblin.dto.ProcessContext;
import org.goblin.dto.Result;
import org.goblin.exception.CommandExecuteException;
import org.goblin.exception.CommandNotFoundException;
import org.goblin.executor.CommandExecutor;
import org.goblin.parser.CommandParser;
import org.jmotor.util.StringUtilities;

import java.nio.file.FileSystems;
import java.nio.file.Files;

/**
 * Component:
 * Description:
 * Date: 13-12-26
 *
 * @author Andy Ai
 */
public class GoblinCommanderImpl implements GoblinCommander {
    private CommandParser commandParser;
    private CommandExecutor commandExecutor;


    @Override
    public Result execute(ProcessContext processContext, String command) {
        Result result = new Result();
        try {
            if (StringUtilities.isNotBlank(processContext.getSearch())) {
                boolean confirm = BooleanUtils.toBoolean(command);
                if (confirm) {
                    result.setSearch(processContext.getSearch());
                } else {
                    result.setSpeech("Canceled");
                }
                processContext.setSearch(null);
                return result;
            }
            Executable executable = commandParser.parse(command);
            String context = executable.getContext();
            if (StringUtilities.isNotBlank(context)) {
                boolean passed = validateContext(executable);
                if (!passed) {
                    result.setException("Can not found context");
                    result.setSpeech("Can not found" + context);
                }
            }
            Process process = commandExecutor.execute(processContext, executable);
            result.setProcess(process);
            if (executable.getCommand().equals("cd")) {
                processContext.setDirectory(context);
            }
            return result;
        } catch (CommandNotFoundException | CommandExecuteException e) {
            processContext.setSearch(command);
            result.setSpeech("Do you want search on website?");
            return result;
        }
    }

    public boolean validateContext(Executable executable) {
        if (null == executable.getContextType()) {
            return Files.exists(FileSystems.getDefault().getPath(executable.getContext()));
        }
        switch (executable.getContextType()) {
            case "file":
                return Files.exists(FileSystems.getDefault().getPath(executable.getContext()));
            default:
                return Files.exists(FileSystems.getDefault().getPath(executable.getContext()));
        }
    }

    public void setCommandParser(CommandParser commandParser) {
        this.commandParser = commandParser;
    }

    public void setCommandExecutor(CommandExecutor commandExecutor) {
        this.commandExecutor = commandExecutor;
    }
}

@@ -454,16 +454,37 @@
 ities;%0A%0A
+import java.io.File;%0A
 import j
@@ -678,16 +678,70 @@
 ander %7B%0A
+    private static final String PARENT_SYMBOL = %22..%22;%0A
     priv
@@ -1991,24 +1991,446 @@
 ls(%22cd%22)) %7B%0A
+                if (PARENT_SYMBOL.equals(context)) %7B%0A                    String directory = processContext.getDirectory();%0A                    int endIndex = directory.lastIndexOf(File.separator);%0A                    if (endIndex %3E 0) %7B%0A                        directory = directory.substring(0, endIndex);%0A                        processContext.setDirectory(directory);%0A                    %7D%0A                %7D else %7B%0A    
             
@@ -2463,32 +2463,50 @@
 ctory(context);%0A
+                %7D%0A
             %7D%0A  

Change process context directory