package speiger.src.collections.longs.queues;

import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.OptionalLong;
import java.util.stream.LongStream;
import java.util.function.LongPredicate;

import speiger.src.collections.longs.collections.LongCollection;
import speiger.src.collections.longs.collections.LongIterator;
import speiger.src.collections.longs.functions.LongComparator;
import speiger.src.collections.longs.functions.LongConsumer;
import speiger.src.collections.ints.functions.consumer.IntLongConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectLongConsumer;
import speiger.src.collections.longs.functions.function.LongLongUnaryOperator;
import speiger.src.collections.longs.utils.LongArrays;
import speiger.src.collections.utils.SanityChecks;

/**
 * A Simple Heap base Priority Queue implementation
 * It is a ArrayBased Alternative to TreeSets that has less object allocations
 */
public class LongHeapPriorityQueue extends AbstractLongPriorityQueue
{
	/** The Backing Array */
	protected transient long[] array = LongArrays.EMPTY_ARRAY;
	/** The Amount of elements stored within the array */
	protected int size;
	/** The Sorter of the Array */
	protected LongComparator comparator;
	
	/**
	 * Default Constructor
	 */
	public LongHeapPriorityQueue() {
		this(0, null);
	}
	
	/**
	 * Constructor using custom sorter
	 * @param comp Comparator to sort the Array. Can be null
	 */
	public LongHeapPriorityQueue(LongComparator comp) {
		this(0, comp);
	}
	
	/**
	 * Constructor with a Min Capacity
	 * @param size the initial capacity of the backing array
	 * @throws IllegalStateException if the initial size is smaller 0
	 */
	public LongHeapPriorityQueue(int size) {
		this(size, null);
	}
	
	/**
	 * Constructor with a Min Capacity and custom Sorter
	 * @param size the initial capacity of the backing array
	 * @param comp Comparator to sort the Array. Can be null
	 * @throws IllegalStateException if the initial size is smaller 0
	 */
	public LongHeapPriorityQueue(int size, LongComparator comp) {
		if(size > 0) array = new long[size];
		comparator = comp;
	}
	
	/**
	 * Constructor using a initial array
	 * @param array the Array that should be used
	 */
	public LongHeapPriorityQueue(long[] array) {
		this(array, array.length);
	}
	
	/**
	 * Constructor using a initial array
	 * @param array the Array that should be used
	 * @param size the amount of elements found within the array
	 * @throws NegativeArraySizeException if size is smaller then 0
	 */
	public LongHeapPriorityQueue(long[] array, int size) {
		this.array = Arrays.copyOf(array, size);
		this.size = size;
		LongArrays.heapify(array, size, null);
	}
	
	/**
	 * Constructor using a initial array and a custom sorter
	 * @param array the Array that should be used
	 * @param comp Comparator to sort the Array. Can be null
	 */
	public LongHeapPriorityQueue(long[] array, LongComparator comp) {
		this(array, array.length, comp);
	}
	
	/**
	 * Constructor using a initial array and a custom sorter
	 * @param array the Array that should be used
	 * @param size the amount of elements found within the array
	 * @param comp Comparator to sort the Array. Can be null
	 * @throws NegativeArraySizeException if size is smaller then 0
	 */
	public LongHeapPriorityQueue(long[] array, int size, LongComparator comp) {
		this.array = Arrays.copyOf(array, size);
		this.size = size;
		comparator = comp;
		LongArrays.heapify(array, size, comp);
	}
	
	/**
	 * Constructor using a Collection
	 * @param c the Collection that should be used
	 */
	public LongHeapPriorityQueue(LongCollection c) {
		array = c.toLongArray();
		size = c.size();
		LongArrays.heapify(array, size, null);
	}
	
	/**
	 * Constructor using a Collection and a custom sorter
	 * @param c the Collection that should be used
	 * @param comp Comparator to sort the Array. Can be null
	 */
	public LongHeapPriorityQueue(LongCollection c, LongComparator comp) {
		array = c.toLongArray();
		size = c.size();
		comparator = comp;
		LongArrays.heapify(array, size, comp);
	}
	
	/**
	 * Wrapping method to help serialization
	 * @param array the array that should be used
	 * @return a HeapPriorityQueue containing the original input array
	 */
	public static LongHeapPriorityQueue wrap(long[] array) { 
		return wrap(array, array.length);
	}
	
	/**
	 * Wrapping method to help serialization
	 * @param array the array that should be used
	 * @param size the amount of elements within the array
	 * @return a HeapPriorityQueue containing the original input array
	 */
	public static LongHeapPriorityQueue wrap(long[] array, int size) {
		LongHeapPriorityQueue queue = new LongHeapPriorityQueue();
		queue.array = array;
		queue.size = size;
		LongArrays.heapify(array, size, null);
		return queue;
	}
	
	/**
	 * Wrapping method to help serialization, using a custom sorter
	 * @param array the array that should be used
	 * @param comp Comparator to sort the Array. Can be null
	 * @return a HeapPriorityQueue containing the original input array
	 */
	public static LongHeapPriorityQueue wrap(long[] array, LongComparator comp) {
		return wrap(array, array.length, comp);
	}
	
	/**
	 * Wrapping method to help serialization, using a custom sorter
	 * @param array the array that should be used
	 * @param size the amount of elements within the array
	 * @param comp Comparator to sort the Array. Can be null
	 * @return a HeapPriorityQueue containing the original input array
	 */
	public static LongHeapPriorityQueue wrap(long[] array, int size, LongComparator comp) {
		LongHeapPriorityQueue queue = new LongHeapPriorityQueue(comp);
		queue.array = array;
		queue.size = size;
		LongArrays.heapify(array, size, comp);
		return queue;
	}
	

	/**
	 * Collects a Stream to a ArrayFIFOQueue
	 * @return a queue with the contents of the Stream
	 */
	public static LongHeapPriorityQueue toQueue(LongStream stream) {
		return stream.collect(LongHeapPriorityQueue::new, LongHeapPriorityQueue::enqueue, LongHeapPriorityQueue::merge);
	}
	
	private LongHeapPriorityQueue merge(LongHeapPriorityQueue a) {
		enqueueAll(a.toLongArray());
		return this;
	}
	
	@Override
	public int size() {
		return size;
	}
	
	@Override
	public void clear() {
		size = 0;
	}
	
	@Override
	public LongIterator iterator() {
		return new Iter();
	}
	
	@Override
	public void enqueue(long e) {
		if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
		array[size++] = e;
		LongArrays.shiftUp(array, size-1, comparator);
	}
	
	@Override
	public long dequeue() {
		if(size <= 0) throw new NoSuchElementException();
		long value = array[0];
		array[0] = array[--size];
		if(size != 0) LongArrays.shiftDown(array, size, 0, comparator);
		return value;
	}
	
	@Override
	public long peek(int index) {
		if(index < 0 || index >= size) throw new NoSuchElementException();
		return array[index];
	}
	
	@Override
	public boolean contains(long e) {
		for(int i = 0;i<size;i++)
			if(e == array[i]) return true;
		return false;
	}
	
	@Override
	public boolean removeFirst(long e) {
		for(int i = 0;i<size;i++)
			if(e == array[i]) return removeIndex(i);
		return false;
	}
	
	@Override
	public boolean removeLast(long e) {
		for(int i = size-1;i>=0;i--)
			if(e == array[i]) return removeIndex(i);
		return false;
	}
	
	@Override
	public void forEach(LongConsumer action) {
		Objects.requireNonNull(action);
		for(int i = 0,m=size;i<m;i++) action.accept(dequeue());
	}
	
	@Override
	public void forEachIndexed(IntLongConsumer action) {
		Objects.requireNonNull(action);
		for(int i = 0,m=size;i<m;i++) action.accept(i, dequeue());
	}
	
	@Override
	public <E> void forEach(E input, ObjectLongConsumer<E> action) {
		Objects.requireNonNull(action);
		for(int i = 0,m=size;i<m;i++) action.accept(input, dequeue());
	}
	
	@Override
	public boolean matchesAny(LongPredicate filter) {
		Objects.requireNonNull(filter);
		for(int i = 0;i<size;i++) {
			if(filter.test(array[i])) return true;
		}
		return false;
	}
	
	@Override
	public boolean matchesNone(LongPredicate filter) {
		Objects.requireNonNull(filter);
		for(int i = 0;i<size;i++) {
			if(filter.test(array[i])) return false;
		}
		return true;
	}
	
	@Override
	public boolean matchesAll(LongPredicate filter) {
		Objects.requireNonNull(filter);
		for(int i = 0;i<size;i++) {
			if(!filter.test(array[i])) return false;
		}
		return true;
	}
	
	@Override
	public long reduce(long identity, LongLongUnaryOperator operator) {
		Objects.requireNonNull(operator);
		long state = identity;
		for(int i = 0;i<size;i++) {
			state = operator.applyAsLong(state, array[i]);
		}
		return state;
	}
	
	@Override
	public OptionalLong reduce(LongLongUnaryOperator operator) {
		Objects.requireNonNull(operator);
		long state = 0L;
		boolean empty = true;
		for(int i = 0;i<size;i++) {
			if(empty) {
				empty = false;
				state = array[i];
				continue;
			}
			state = operator.applyAsLong(state, array[i]);
		}
		return empty ? OptionalLong.empty() : OptionalLong.of(state);
	}
	
	@Override
	public OptionalLong findFirst(LongPredicate filter) {
		Objects.requireNonNull(filter);
		for(int i = 0;i<size;i++) {
			if(filter.test(array[i])) {
				long data = array[i];
				removeIndex(i);
				return OptionalLong.of(data);
			}
		}
		return OptionalLong.empty();
	}
	
	@Override
	public int count(LongPredicate filter) {
		Objects.requireNonNull(filter);
		int result = 0;
		for(int i = 0;i<size;i++) {
			if(filter.test(array[i])) result++;
		}
		return result;
	}
	
	protected boolean removeIndex(int index) {
		array[index] = array[--size];
		if(size != index) LongArrays.shiftDown(array, size, index, comparator);
		return true;
	}
	
	@Override
	public void onChanged() {
		if(size <= 0) return;
		LongArrays.shiftDown(array, size, 0, comparator);
	}
	
	@Override
	public LongHeapPriorityQueue copy() {
		LongHeapPriorityQueue queue = new LongHeapPriorityQueue();
		queue.size = size;
		queue.comparator = comparator;
		queue.array = Arrays.copyOf(array, array.length);
		return queue;
	}
	
	@Override
	public LongComparator comparator() {
		return comparator;
	}
	
	@Override
	public long[] toLongArray(long[] input) {
		if(input == null || input.length < size()) input = new long[size()];
		System.arraycopy(array, 0, input, 0, size());
		return input;
	}
	
	private class Iter implements LongIterator {
		@Override
		public boolean hasNext() {
			return !isEmpty();
		}
		
		@Override
		public long nextLong() {
			if(!hasNext()) throw new NoSuchElementException();
			return dequeue();
		}
	}
}