package com.tyron.builder.compiler.aab;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class TreeCopyFileVisitor extends SimpleFileVisitor<Path> {

  private final Path source;
  private final Path target;

  public TreeCopyFileVisitor(String source, String target) {
    this.source = Paths.get(source);
    this.target = Paths.get(target);
  }

  @Override
  public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

    Path resolve = target.resolve(source.relativize(dir));
    if (Files.notExists(resolve)) {
      Files.createDirectories(resolve);
      System.out.println("Create directories : " + resolve);
    }
    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

    Path resolve = target.resolve(source.relativize(file));
    Files.copy(file, resolve);
    System.out.printf("Copy File from \t'%s' to \t'%s'%n", file, resolve);

    return FileVisitResult.CONTINUE;
  }

  @Override
  public FileVisitResult visitFileFailed(Path file, IOException exc) {
    System.err.format("Unable to copy: %s: %s%n", file, exc);
    return FileVisitResult.CONTINUE;
  }
}
