package org.implab.gradle.common.dsl;

import java.io.File;
import java.io.OutputStream;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.gradle.api.provider.Provider;
import org.implab.gradle.common.exec.RedirectTo;

public class RedirectToSpec {
    private Supplier<RedirectTo> streamRedirect;
    
    public boolean isRedirected() {
        return streamRedirect != null;
    }

    public Optional<RedirectTo> getRedirection() {
        return streamRedirect != null ? Optional.ofNullable(streamRedirect.get()) : Optional.empty();
    }

    public void toFile(File file) {
        this.streamRedirect = () -> RedirectTo.file(file);
    }

    public void toFile(Provider<File> file) {
        this.streamRedirect = file.map(RedirectTo::file)::get;
    }

    public void toStream(OutputStream stream) {
        this.streamRedirect = () -> RedirectTo.stream(stream);
    }

    public void toStream(Provider<OutputStream> stream) {
        this.streamRedirect = stream.map(RedirectTo::stream)::get;
    }

    public void toConsumer(Consumer<String> consumer) {
        this.streamRedirect = () -> RedirectTo.consumer(consumer);
    }
}
