package net.ranides.assira.collection.query.base;

import net.ranides.assira.collection.iterators.IteratorUtils;
import net.ranides.assira.collection.query.CQuery;
import net.ranides.assira.collection.query.CQueryAbstract;
import net.ranides.assira.collection.query.support.BaseCollection;
import net.ranides.assira.collection.query.support.BaseIterable;
import net.ranides.assira.functional.Consumers;
import net.ranides.assira.functional.Predicates;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Stream implementation which uses collection generator as source of data.
 *
 * If provided generator creates collection fast, then  it implements fast:
 * stream, iterator and length.
 *
 * @param <T>
 */
public abstract class CQCollectionSupplier<T> extends CQueryAbstract<T> {

    protected boolean parallel;

    protected Supplier<? extends RuntimeException> message;

    /**
     * Creates new stream which does not support fast operations at all, because
     * it assummes generator call has very high cost.
     *
     * @param source source
     * @param <T> T
     * @return stream
     */
    public static <T> CQuery<T> from(Supplier<Collection<T>> source) {
        return from(false, source);
    }

    /**
     * Creates new stream which supports fast operations, if generator is fast.
     *
     * @param fast fast
     * @param source source
     * @param <T> T
     * @return query
     */
    public static <T> CQuery<T> from(boolean fast, Supplier<Collection<T>> source) {
        return new CQCollectionSupplier<T>() {
            @Override
            protected Collection<T> collection() {
                return source.get();
            }

            @Override
            protected boolean hasFastCollection() {
                return fast;
            }
        };
    }

    protected abstract Collection<T> collection();

    protected abstract boolean hasFastCollection();

    @Override
    public Supplier<? extends RuntimeException> message() {
        return message;
    }

    @Override
    public CQuery<T> message(Supplier<? extends RuntimeException> message) {
        this.message = message;
        return this;
    }
    
    @Override
    public boolean hasFastEach() {
        return hasFastCollection();
    }

    @Override
    public boolean hasFastStream() {
        return hasFastCollection();
    }

    @Override
    public boolean hasFastIterator() {
        return hasFastCollection();
    }

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

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

    @Override
    public boolean isParallel() {
        return parallel;
    }

    @Override
    public CQuery<T> parallel() {
        this.parallel = true;
        return this;
    }

    @Override
    public CQuery<T> sequential() {
        this.parallel = false;
        return this;
    }

    @Override
    public Stream<T> stream() {
        return isParallel() ? collection().parallelStream() : collection().stream();
    }

    @Override
    public Iterator<T> iterator() {
        return IteratorUtils.withCause(collection().iterator(), message);
    }

    @Override
    public Spliterator<T> spliterator() {
        return collection().spliterator();
    }

    @Override
    public List<T> list() {
        return new ArrayList<>(collection());
    }

    @Override
    public int size() {
        return collection().size();
    }

    @Override
    public void forEach(Consumer<? super T> consumer) {
        BaseCollection.forEach(collection(), isParallel(), consumer);
    }

    @Override
    public void forEach(Consumers.EachConsumer<? super T> consumer) {
        BaseCollection.forEach(collection(), isParallel(), consumer);
    }

    @Override
    public boolean whileEach(Predicate<? super T> consumer) {
        return BaseCollection.whileEach(collection(), isParallel(), consumer);
    }

    @Override
    public boolean whileEach(Predicates.EachPredicate<? super T> consumer) {
        return BaseCollection.whileEach(collection(), isParallel(), consumer);
    }

}
