/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.io.uri.impl;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import net.ranides.assira.io.uri.URIHandle;
import net.ranides.assira.io.uri.URIHandler;
import net.ranides.assira.io.uri.URIResolver;
import net.ranides.assira.io.uri.URIUtils;

// @todo (assira #1) uri: class-path Handler
public class CClassPathHandler implements URIHandler {
    
    private final URIResolver resolver;
    
    public CClassPathHandler() {
        this.resolver = URIResolver.DEFAULT;
    }

    public CClassPathHandler(URIResolver resolver) {
        this.resolver = resolver;
    }

    @Override
    public URIHandle resolve(URI uri) throws IOException {
        return new CClassPathHandle(uri);
    }

    @Override
    public Collection<String> schemes() {
        return Arrays.asList("classpath");
    }
    
    private class CClassPathHandle extends CHandle {
        
        public final URI uri;

        public CClassPathHandle(URI uri) {
            this(uri, URIUtils.getFile(uri).toPath(), URIUtils.getParam(uri, "charset"));
        }
        
        public CClassPathHandle(Path path) {
            this(path.toUri(), path, Optional.empty());
        }
        
        public CClassPathHandle(URI uri, Path path, Optional<String> charset) {
            this.uri = uri;
//            this.file = path.toFile();
//            this.path = path;
//            this.charset = Charset.forName(charset.orElse("UTF-8"));
//            this.text = charset.isPresent();
//            this.istream = new IOHandle<>(() -> Files.newInputStream(path));
//            this.ostream = new IOHandle<>(() -> Files.newOutputStream(path));
        }

        @Override
        public URIResolver resolver() {
            return CClassPathHandler.this.resolver;
        }

        @Override
        public URI uri() throws IOException {
            return uri;
        }
        
    }
    
}
