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

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Stream implementation which uses array generator as source of data.
 *
 * If provided generator creates array fast, then  it implements fast:
 * list, stream, iterator and length.
 *
 * @param <T>
 */
public abstract class CQArraySupplier<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<T[]> source) {
        return from(false, source);
    }

    /**
     * Creates new stream which supports almost all fast 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<T[]> source) {
        return new CQArraySupplier<T>(){
            @Override
            public T[] array() {
                return source.get();
            }

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

    @Override
    public abstract T[] array();

    protected abstract boolean hasFastArray();

    @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 hasFastArray();
    }

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

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

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

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

    @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 BaseArray.stream(array(), isParallel());
    }

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

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

    @Override
    public List<T> list() {
        return BaseArray.list(array());
    }

    @Override
    public Set<T> set() {
        return BaseArray.set(this);
    }

    @Override
    public Ref<T> at(int index) {
        return BaseArray.at(this, index);
    }

    @Override
    public Ref<T> first() {
        return BaseArray.first(this);
    }

    @Override
    public Ref<T> last() {
        return BaseArray.last(this);
    }

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

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

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

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