package org.xbib.files.webdav.fs;

import org.xbib.io.webdav.client.WebDavClient;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.WatchService;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.Set;

public class WebdavFileSystem extends FileSystem {

    private final FileSystemProvider provider;

    private final URI uri;

    private final String password;

    private final String username;

    /**
     * @param provider  an instance of a WebdavFileSystemProvided. This can be a shared instance.
     * @param serverUri URI for the WEBDAV server, the scheme is ignored.
     */
    public WebdavFileSystem(WebdavFileSystemProvider provider, URI serverUri) {
        this.provider = provider;
        this.uri = serverUri;
        String[] ui = serverUri.getUserInfo().split(":");
        this.username = ui[0];
        this.password = ui[1];
    }

    @Override
    public FileSystemProvider provider() {
        return provider;
    }

    @Override
    public void close() {
    }

    @Override
    public Iterable<FileStore> getFileStores() {
        return null;
    }

    @Override
    public Path getPath(String first, String... more) {
        String path;
        if (more.length == 0) {
            path = first;
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(first);
            for (String segment : more) {
                if (segment.length() > 0) {
                    if (sb.length() > 0) {
                        sb.append(getSeparator());
                    }
                    sb.append(segment);
                }
            }
            path = sb.toString();
        }
        return new WebdavPath(this, path);
    }

    @Override
    public PathMatcher getPathMatcher(String syntaxAndPattern) {
        return null;
    }

    @Override
    public Iterable<Path> getRootDirectories() {
        return null;
    }

    @Override
    public String getSeparator() {
        return "/";
    }

    @Override
    public UserPrincipalLookupService getUserPrincipalLookupService() {
        return null;
    }

    @Override
    public boolean isOpen() {
        return false;
    }

    @Override
    public boolean isReadOnly() {
        return false;
    }

    @Override
    public WatchService newWatchService() throws IOException {
        return null;
    }

    @Override
    public Set<String> supportedFileAttributeViews() {
        return null;
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof WebdavFileSystem)) {
            return false;
        }
        WebdavFileSystem fileSystem = (WebdavFileSystem) other;
        return this.uri.equals(fileSystem.uri);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    public URI getUri() {
        return uri;
    }

    public String getUserName() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    WebDavClient getClient() {
        WebDavClient client = new WebDavClient();
        client.init(uri, username, password);
        return client;
    }
}
