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

import lombok.RequiredArgsConstructor;

import net.ranides.assira.collection.iterators.IteratorUtils;
import net.ranides.assira.collection.lists.CopyList;
import net.ranides.assira.collection.query.CQuery;
import net.ranides.assira.collection.query.CQueryAbstract;
import net.ranides.assira.collection.query.support.BaseList;
import net.ranides.assira.functional.Consumers;
import net.ranides.assira.functional.Predicates;
import net.ranides.assira.math.MathUtils;

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

    protected final boolean parallel;

    protected final List<T> data;

    protected final Supplier<? extends RuntimeException> message;

    /**
     * Creates sequential query.
     * @param data data
     */
    public CQList(List<T> data) {
        this(false, data, null);
    }

    /**
     * Creates parallel query.
     * @param parallel parallel
     * @param data data
     */
    public CQList(boolean parallel, List<T> data) {
        this(parallel, data, null);
    }

    /**
     * Creates parallel query.
     * @param parallel parallel
     * @param data data
     */
    public CQList(boolean parallel, List<T> data, Supplier<? extends RuntimeException> message) {
        this.parallel = parallel;
        this.data = data;
        this.message = message;
    }

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

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

    @Override
    public CQuery<T> sequential() {
        return isParallel() ? new CQList<>(false, data, message) : 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 CopyList<>(data);
    }

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

    @Override
    public CQuery<T> slice(int begin, int end) {
        int b = MathUtils.clip(begin, 0, data.size());
        int e = MathUtils.clip(end, b, data.size());
        return new CQList<>(parallel, data.subList(b,e), message);
    }

    @Override
    public void forEach(Consumer<? super T> consumer) {
        BaseList.forEach(this, consumer);
    }

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

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

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

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

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

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

}
