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

import net.ranides.assira.collection.iterators.IteratorUtils;
import net.ranides.assira.collection.iterators.SpliteratorUtils;
import net.ranides.assira.collection.query.CQuery;
import net.ranides.assira.collection.query.CQueryAbstract;
import net.ranides.assira.functional.Consumers;
import net.ranides.assira.functional.Predicates;
import net.ranides.assira.generic.Ref;

import java.nio.file.Path;
import java.util.Collections;
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.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * This is query implementation which stores just one element.
 * Because it stores just one object, all operations are reported as fast.
 *
 * @param <T> T
 */
public class CQSingle<T> extends CQueryAbstract<T> {

    private final Supplier<? extends RuntimeException> message;

    private final T value;

    public CQSingle(Supplier<? extends RuntimeException> message, T value) {
        this.message = message;
        this.value = value;
    }

    public CQSingle(T value) {
        this.message = null;
        this.value = value;
    }

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

    @Override
    public CQuery<T> message(Supplier<? extends RuntimeException> message) {
        return new CQSingle<>(message, value);
    }

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

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

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

    @Override
    public Stream<T> stream() {
        return Stream.of(value);
    }

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

    @Override
    public Spliterator<T> spliterator() {
        return SpliteratorUtils.singletonSpliterator(value);
    }

    @Override
    public int size() {
        return 1;
    }

    @Override
    @SuppressWarnings("unchecked")
    public T[] array() {
        return (T[])new Object[]{value};
    }

    @Override
    public List<T> list() {
        return Collections.singletonList(value);
    }

    @Override
    public Set<T> set() {
        return Collections.singleton(value);
    }

    @Override
    public CQuery<T> limit(int n) {
        return n == 0 ? CQuery.empty(message) : this;
    }

    @Override
    public CQuery<T> skip(int n) {
        return n != 0 ? CQuery.empty(message) : this;
    }

    @Override
    public CQuery<T> slice(int begin, int end) {
        return begin>0 || end<1 ? CQuery.empty(message) : this;
    }

    @Override
    public Ref<T> at(int index) {
        return index != 0 ? Ref.empty(message) : Ref.of(value, message).withMessage(message());
    }

    @Override
    public Ref<T> first() {
        return Ref.of(value, message);
    }

    @Override
    public Ref<T> last() {
        return Ref.of(value, message);
    }

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

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

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

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

    @Override
    public void forEach(Consumer<? super T> consumer) {
        consumer.accept(value);
    }

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

    @Override
    public boolean whileEach(Predicate<? super T> consumer) {
        return consumer.test(value);
    }

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