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.BaseSpliterator;
import net.ranides.assira.functional.Consumers;
import net.ranides.assira.functional.Predicates;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

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

    protected boolean parallel;

    protected Supplier<? extends RuntimeException> message = null;

    /**
     * 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<Spliterator<T>> source) {
        return from(false, source);
    }

    /**
     * Creates new stream which supports almost fast "stream" and "iterator" operations, if generator is fast.
     *
     * @param fast fast
     * @param source source
     * @param <T> T
     * @return stream
     */public static <T> CQuery<T> from(boolean fast, Supplier<Spliterator<T>> source) {
        return new CQSpliterator<T>() {
            @Override
            public Spliterator<T> spliterator() {
                return source.get();
            }

            @Override
            public boolean hasFastSpliterator() {
                return fast;
            }
        };
    }

    @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 false;
    }

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

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

    @Override
    public boolean hasFastLength() {
        return hasFastSpliterator() && spliterator().hasCharacteristics(Spliterator.SIZED);
    }

    @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;
    }

    protected abstract boolean hasFastSpliterator();

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

    @Override
    public Stream<T> stream() {
        return StreamSupport.stream(spliterator(), isParallel());
    }

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

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

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

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

    @Override
    public int size() {
        return BaseSpliterator.size(spliterator(), isParallel());
    }
}


