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

import lombok.RequiredArgsConstructor;

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

import java.nio.file.Path;
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 as source of data.
 * Because it stores internally collection, it implements fast:
 * list, stream, iterator and length.
 *
 * @param <T>
 */
@RequiredArgsConstructor
public class CQCollection<T> extends CQueryAbstract<T> {

    protected final Collection<T> data;

    protected final boolean parallel;

    protected final Supplier<? extends RuntimeException> message;

    /**
     * Creates new sequential stream with provided data.
     *
     * @param data data
     */
    public CQCollection(Collection<T> data) {
        this(data, false, null);
    }

    private CQCollection(Collection<T> data, boolean parallel) {
        this(data, parallel, null);
    }

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

    @Override
    public CQuery<T> message(Supplier<? extends RuntimeException> message) {
        return new CQCollection<>(data, parallel, 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 false;
    }

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

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

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

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

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

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

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

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

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

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

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

    @Override
    public boolean whileEach(Predicates.EachPredicate<? super T> consumer) {
        return BaseCollection.whileEach(data, 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;
    }
}
