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

import net.ranides.assira.collection.arrays.ArrayUtils;
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 net.ranides.assira.math.MathUtils;

import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Stream implementation which uses array as source of data.
 * Because it stores internally array, it implements fast:
 * list, stream, iterator and length.
 *
 * @param <T>
 */
public class CQArray<T> extends CQueryAbstract<T> {

    protected final T[] data;

    protected final int begin;

    protected final int end;

    protected final boolean parallel;

    protected final Supplier<? extends RuntimeException> message;

    /**
     * Creates new stream with all elements from provided array.
     * @param data data
     */
    public CQArray(T[] data) {
        this(false, data, 0, data.length);
    }

    /**
     * Creates new stream with elements from provided array in specified range.
     *
     * @param data data
     * @param begin begin
     * @param end end
     */
    public CQArray(T[] data, int begin, int end) {
        this(false, data, begin, end);
    }

    private CQArray(boolean parallel, CQArray<T> source) {
        this(parallel, source.data, source.begin, source.end);
    }

    private CQArray(boolean parallel, T[] data, int begin, int end) {
        this(parallel, data, begin, end, null);
    }

    private CQArray(boolean parallel, T[] data, int begin, int end, Supplier<? extends RuntimeException> message) {
        this.data = data;
        this.begin = MathUtils.clip(begin, 0, data.length);
        this.end = MathUtils.clip(end, begin, data.length);
        this.parallel = parallel;
        this.message = message;
    }

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

    @Override
    public CQuery<T> message(Supplier<? extends RuntimeException> message) {
        return new CQArray<T>(parallel, data, begin, end, message);
    }

    @Override
    public boolean hasFastEach() {
        return true;
    }

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

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

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

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

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

    @Override
    public CQuery<T> parallel() {
        return isParallel() ? this : new CQArray<>(true, this);
    }

    @Override
    public CQuery<T> sequential() {
        return isParallel() ? new CQArray<>(false, this) : this;
    }

    private boolean isSlice() {
        return begin != 0 || end != data.length;
    }

    @Override
    public Stream<T> stream() {
        return BaseArray.stream(data, begin, end, isParallel());
    }

    @Override
    public Iterator<T> iterator() {
        return IteratorUtils.withCause(BaseArray.iterator(data, begin, end), message);
    }

    @Override
    public Spliterator<T> spliterator() {
        return BaseArray.spliterator(data, begin, end);
    }

    @Override
    public int size() {
        return end - begin;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <A> A[] array(IntFunction<A[]> generator) {
        int n = size();
        return (A[])ArrayUtils.copy(data, begin, generator.apply(n), 0, n);
    }

    @Override
    public T[] array() {
        return isSlice() ? ArrayUtils.slice(data, begin, end) : data.clone();
    }

    @Override
    public List<T> list() {
        return isSlice() ? BaseArray.list(data, begin, end) : BaseArray.list(data);
    }

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

    @Override
    public CQuery<T> slice(int begin, int end) {
        int b = MathUtils.adds(this.begin, begin, 0, this.end);
        int e = MathUtils.adds(this.begin, end, b, this.end);
        return new CQArray<>(isParallel(), data, b, e, message);
    }

    @Override
    public Ref<T> at(int index) {
        return begin+index<end ? BaseArray.at(data, begin+index).withMessage(message()) : Ref.empty(message);
    }

    @Override
    public Ref<T> first() {
        return BaseArray.at(data, begin).withMessage(message);
    }

    @Override
    public Ref<T> last() {
        return BaseArray.at(data, end-1).withMessage(message);
    }

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

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

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

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

    @Override
    public CQuery<T> fetch() {
        return this;
    }

    @Override
    public CQuery<T> prefetch() {
        return this;
    }

    @Override
    public CQuery<T> cache(Path filename) {
        return this;
    }

}
