package eloquent.adapters;

import eloquent.exceptions.EloquentException;
import eloquent.exceptions.FileNotFoundException;
import eloquent.models.File;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Date;

/**
 * @author Kunal
 */
public class LocalAdapter extends AbstractAdapter {

    /**
     * Default Character Set
     */
    private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

    /**
     * Local Filesystem Root
     */
    protected String root = "";

    public LocalAdapter(String root) {
        this.root = root;
    }

    public String getRoot() {
        return this.root;
    }

    public void setRoot(String root) {
        this.root = root;
    }

    @Override
    public File read(String path) throws EloquentException {
        java.io.File file = new java.io.File(path);

        // File doesn't exist
        if (!file.exists()) {
            throw new FileNotFoundException(path);
        }

        String contents;
        String size;
        String name;
        Date timestamp;

        try {
            // Read the file into a string
            contents = new String(Files.readAllBytes(file.toPath()), DEFAULT_CHARSET);
            // Fetch the file size
            size = Long.toString(file.length());
            // Fetch the file name
            name = file.getName();
            // Fetch the file timestamp (last modified)
            timestamp = new Date(file.lastModified());
        } catch (Exception e) {
            throw new EloquentException(e.getMessage());
        }

        // Make the file model
        File fileModel = new File();
        fileModel.setPath(file.getAbsolutePath())
                .setContents(contents)
                .setSize(size)
                .setName(name)
                .setTimestamp(timestamp);

        // Return the file model
        return fileModel;
    }

    @Override
    public File write(String path, String contents) throws EloquentException {
        return null;
    }

    @Override
    public File update(String path, String contents) throws EloquentException {
        return null;
    }

    @Override
    public boolean has(String path) throws EloquentException {
        return false;
    }

    @Override
    public boolean rename(String path, String newPath) throws EloquentException {
        return false;
    }

    @Override
    public boolean copy(String path, String newPath) throws EloquentException {
        return false;
    }

    @Override
    public boolean delete(String path) throws EloquentException {
        return false;
    }

    @Override
    public boolean deleteDir(String path) throws EloquentException {
        return false;
    }

    @Override
    public boolean createDir(String path) throws EloquentException {
        return false;
    }
}

@@ -599,35 +599,37 @@
        this.
-root = 
+setRoot(
 root
+)
 ;%0A    %7D%0A%0A   
@@ -723,32 +723,33 @@
 ing root) %7B%0A
+%0A
         
 this.root = 
@@ -740,24 +740,583 @@
     
-this.root = root
+// Add '/' at the end of%0A        // the root if not present.%0A        if (!root.endsWith(%22/%22)) %7B%0A            root = root + %22/%22;%0A        %7D%0A%0A        // Set the root%0A        this.root = root;%0A    %7D%0A%0A    /**%0A     * Build the path%0A     *%0A     * @param path Path to a file or directory%0A     *%0A     * @return String%0A     */%0A    protected String buildPath(String path) %7B%0A        String root = this.getRoot();%0A%0A        // The path to file/folder%0A        // must start with a '/'.%0A        if (!path.startsWith(%22/%22)) %7B%0A            path = %22/%22 + path;%0A        %7D%0A%0A        return root + path
 ;%0A  
@@ -1387,32 +1387,69 @@
 uentException %7B%0A
+        path = this.buildPath(path);%0A
         java.io.

Build local path