package ch.deletescape.jterm.commandcontexts;

import ch.deletescape.jterm.CommandUtils;
import ch.deletescape.jterm.JTerm;
import ch.deletescape.jterm.Util;
import ch.deletescape.jterm.config.Resources;
import ch.deletescape.jterm.io.Printer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.file.*;
import java.util.stream.Stream;

public class Directories extends CommandContext {
  private static final Logger LOGGER = LogManager.getLogger();

  @Override
  protected void init() {
    CommandUtils.addListener("cd", this::cd);
    CommandUtils.addListener("pwd", o -> pwd());
    CommandUtils.addListener("ls", this::ls);
    CommandUtils.addListener("mkdir", this::mkdir);
  }

  private String pwd() {
    return Printer.out.println(JTerm.getCurrPath());
  }

  private boolean cd(String cmd) throws IOException {
    String command = CommandUtils.parseInlineCommands(cmd);
    Path path = JTerm.getCurrPath().resolve(Util.makePathString(command)).toRealPath();
    if (Files.isDirectory(path)) {
      JTerm.setCurrPath(path);
      pwd();
      return true;
    }
    Printer.err.println(Resources.getString("Directories.PathIsNotDirectory"), path);
    return false;
  }

  private String ls(String cmd) throws IOException {
    String command = CommandUtils.parseInlineCommands(cmd);
    StringBuilder out = new StringBuilder();
    Path path = JTerm.getCurrPath().resolve(Util.makePathString(command));
    try (Stream<Path> stream = Files.list(path)) {
      stream.forEach(p -> out.append(p.getFileName().toString() + "\n"));
    } catch (NoSuchFileException e) {
      Printer.err.println(Resources.getString("PathNotFound"), path);
      LOGGER.error(e.toString(), e);
    } catch (NotDirectoryException e) {
      out.append(path);
      out.append('\n');
    }
    return Printer.out.println(out);
  }

  private boolean mkdir(String cmd) throws IOException {
    String command = CommandUtils.parseInlineCommands(cmd);
    Path path = JTerm.getCurrPath().resolve(Util.makePathString(command));
    try {
      Files.createDirectory(path);
      return true;
    } catch (FileAlreadyExistsException e) {
      Printer.err.println(Resources.getString("Directories.DirectoryAlreadyExists"), path);
      LOGGER.error(e.toString(), e);
    }
    return false;
  }
}

@@ -40,16 +40,352 @@
 texts;%0A%0A
+import java.io.IOException;%0Aimport java.nio.file.FileAlreadyExistsException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.NoSuchFileException;%0Aimport java.nio.file.NotDirectoryException;%0Aimport java.nio.file.Path;%0Aimport java.util.stream.Stream;%0A%0Aimport org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A%0A
 import c
@@ -576,177 +576,8 @@
 ter;
-%0Aimport org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A%0Aimport java.io.IOException;%0Aimport java.nio.file.*;%0Aimport java.util.stream.Stream;
 %0A%0Apu
@@ -1539,53 +1539,8 @@
 d);%0A
-    StringBuilder out = new StringBuilder();%0A
     
@@ -1657,24 +1657,71 @@
 st(path)) %7B%0A
+      StringBuilder out = new StringBuilder();%0A
       stream
@@ -1778,24 +1778,63 @@
 ) + %22%5Cn%22));%0A
+      return Printer.out.println(out);%0A
     %7D catch 
@@ -1857,32 +1857,76 @@
 tion e) %7B%0A      
+LOGGER.error(e.toString(), e);%0A      return 
 Printer.err.prin
@@ -1977,45 +1977,8 @@
 h);%0A
-      LOGGER.error(e.toString(), e);%0A
     
@@ -2019,60 +2019,8 @@
 %7B%0A  
-    out.append(path);%0A      out.append('%5Cn');%0A    %7D%0A
     
@@ -2046,21 +2046,28 @@
 println(
-out);
+path);%0A    %7D
 %0A  %7D%0A%0A  

refactor Directories