package speiger.src.collections.doubles.maps.abstracts;

import java.util.AbstractMap;
import java.util.Map;
import java.util.Objects;

import speiger.src.collections.doubles.collections.DoubleIterator;
import speiger.src.collections.doubles.functions.consumer.DoubleIntConsumer;
import speiger.src.collections.doubles.functions.function.Double2IntFunction;
import speiger.src.collections.doubles.functions.function.DoubleIntUnaryOperator;
import speiger.src.collections.doubles.maps.interfaces.Double2IntMap;
import speiger.src.collections.doubles.maps.interfaces.Double2IntOrderedMap;
import speiger.src.collections.doubles.sets.DoubleOrderedSet;
import speiger.src.collections.ints.collections.IntOrderedCollection;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
import speiger.src.collections.doubles.sets.AbstractDoubleSet;
import speiger.src.collections.doubles.sets.DoubleSet;
import speiger.src.collections.doubles.utils.maps.Double2IntMaps;
import speiger.src.collections.ints.collections.AbstractIntCollection;
import speiger.src.collections.ints.collections.IntCollection;
import speiger.src.collections.ints.collections.IntIterator;
import speiger.src.collections.ints.functions.function.IntIntUnaryOperator;
import speiger.src.collections.ints.functions.IntSupplier;
import speiger.src.collections.objects.collections.ObjectIterable;
import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.sets.ObjectSet;
import speiger.src.collections.utils.SanityChecks;

/**
 * A Base Implementation of a Type Specific Map to reduce boxing/unboxing
 */
public abstract class AbstractDouble2IntMap extends AbstractMap<Double, Integer> implements Double2IntMap
{
	protected int defaultReturnValue = -1;
	
	@Override
	public int getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractDouble2IntMap setDefaultReturnValue(int v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Double2IntMap.Entry> getFastIterable(Double2IntMap map) {
		return Double2IntMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Double2IntMap.Entry> getFastIterator(Double2IntMap map) {
		return Double2IntMaps.fastIterator(map);
	}
	
	@Override
	public Double2IntMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Integer put(Double key, Integer value) {
		return Integer.valueOf(put(key.doubleValue(), value.intValue()));
	}
	
	@Override
	public void addToAll(Double2IntMap m) {
		for(Double2IntMap.Entry entry : getFastIterable(m))
			addTo(entry.getDoubleKey(), entry.getIntValue());
	}
	
	@Override
	public void putAll(Double2IntMap m) {
		for(ObjectIterator<Double2IntMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Double2IntMap.Entry entry = iter.next();
			put(entry.getDoubleKey(), entry.getIntValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Double, ? extends Integer> m)
	{
		if(m instanceof Double2IntMap) putAll((Double2IntMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(double[] keys, int[] values, int offset, int size) {
		SanityChecks.checkArrayCapacity(keys.length, offset, size);
		SanityChecks.checkArrayCapacity(values.length, offset, size);
		for(int i = 0;i<size;i++) put(keys[i], values[i]);
	}
	
	@Override
	public void putAll(Double[] keys, Integer[] values, int offset, int size) {
		SanityChecks.checkArrayCapacity(keys.length, offset, size);
		SanityChecks.checkArrayCapacity(values.length, offset, size);
		for(int i = 0;i<size;i++) put(keys[i].doubleValue(), values[i].intValue());		
	}
	
	@Override
	public void putAllIfAbsent(Double2IntMap m) {
		for(Double2IntMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getDoubleKey(), entry.getIntValue());
	}
	
	
	@Override
	public boolean containsKey(double key) {
		for(DoubleIterator iter = keySet().iterator();iter.hasNext();)
			if(Double.doubleToLongBits(iter.nextDouble()) == Double.doubleToLongBits(key)) return true;
		return false;
	}
	
	@Override
	public boolean containsValue(int value) {
		for(IntIterator iter = values().iterator();iter.hasNext();)
			if(iter.nextInt() == value) return true;
		return false;
	}
	
	@Override
	public boolean replace(double key, int oldValue, int newValue) {
		int curValue = get(key);
		if (curValue != oldValue || (curValue == getDefaultReturnValue() && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public int replace(double key, int value) {
		int curValue;
		if ((curValue = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceInts(Double2IntMap m) {
		for(Double2IntMap.Entry entry : getFastIterable(m))
			replace(entry.getDoubleKey(), entry.getIntValue());
	}
	
	@Override
	public void replaceInts(DoubleIntUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Double2IntMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Double2IntMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsInt(entry.getDoubleKey(), entry.getIntValue()));
		}
	}

	@Override
	public int computeInt(double key, DoubleIntUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		int newValue = mappingFunction.applyAsInt(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public int computeIntIfAbsent(double key, Double2IntFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			int newValue = mappingFunction.applyAsInt(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public int supplyIntIfAbsent(double key, IntSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			int newValue = valueProvider.getAsInt();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public int computeIntIfPresent(double key, DoubleIntUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			int newValue = mappingFunction.applyAsInt(key, get(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public int computeIntNonDefault(double key, DoubleIntUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		int value = get(key);
		int newValue = mappingFunction.applyAsInt(key, value);
		if(newValue == getDefaultReturnValue()) {
			if(value != getDefaultReturnValue() || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public int computeIntIfAbsentNonDefault(double key, Double2IntFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		int value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			int newValue = mappingFunction.applyAsInt(key);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public int supplyIntIfAbsentNonDefault(double key, IntSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		int value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			int newValue = valueProvider.getAsInt();
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public int computeIntIfPresentNonDefault(double key, DoubleIntUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		int value;
		if((value = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			int newValue = mappingFunction.applyAsInt(key, value);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public int mergeInt(double key, int value, IntIntUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		int oldValue = get(key);
		int newValue = oldValue == getDefaultReturnValue() ? value : mappingFunction.applyAsInt(oldValue, value);
		if(newValue == getDefaultReturnValue()) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllInt(Double2IntMap m, IntIntUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Double2IntMap.Entry entry : getFastIterable(m)) {
			double key = entry.getDoubleKey();
			int oldValue = get(key);
			int newValue = oldValue == getDefaultReturnValue() ? entry.getIntValue() : mappingFunction.applyAsInt(oldValue, entry.getIntValue());
			if(newValue == getDefaultReturnValue()) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Integer get(Object key) {
		return Integer.valueOf(key instanceof Double ? get(((Double)key).doubleValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Integer getOrDefault(Object key, Integer defaultValue) {
		return Integer.valueOf(key instanceof Double ? getOrDefault(((Double)key).doubleValue(), defaultValue.intValue()) : getDefaultReturnValue());
	}
	
	@Override
	public int getOrDefault(double key, int defaultValue) {
		int value = get(key);
		return value != getDefaultReturnValue() || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Integer remove(Object key) {
		return key instanceof Double ? Integer.valueOf(remove(((Double)key).doubleValue())) : Integer.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(DoubleIntConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Double2IntMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Double2IntMap.Entry entry = iter.next();
			action.accept(entry.getDoubleKey(), entry.getIntValue());
		}
	}

	@Override
	public DoubleSet keySet() {
		return new AbstractDoubleSet() {
			@Override
			public boolean remove(double o) {
				return AbstractDouble2IntMap.this.remove(o) != getDefaultReturnValue();
			}
			
			@Override
			public boolean add(double o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public DoubleIterator iterator() {
				return new DoubleIterator() {
					ObjectIterator<Double2IntMap.Entry> iter = getFastIterator(AbstractDouble2IntMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}

					@Override
					public double nextDouble() {
						return iter.next().getDoubleKey();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
			
			@Override
			public int size() {
				return AbstractDouble2IntMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractDouble2IntMap.this.clear();
			}
		};
	}

	@Override
	public IntCollection values() {
		return new AbstractIntCollection() {
			@Override
			public boolean add(int o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractDouble2IntMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractDouble2IntMap.this.clear();
			}
			
			@Override
			public IntIterator iterator() {
				return new IntIterator() {
					ObjectIterator<Double2IntMap.Entry> iter = getFastIterator(AbstractDouble2IntMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public int nextInt() {
						return iter.next().getIntValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Double, Integer>> entrySet() {
		return (ObjectSet)double2IntEntrySet();
	}

	@Override
	public boolean equals(Object o) {
		if(o == this) return true;
		if(o instanceof Map) {
			if(size() != ((Map<?, ?>)o).size()) return false;
			if(o instanceof Double2IntMap) return double2IntEntrySet().containsAll(((Double2IntMap)o).double2IntEntrySet());
			return double2IntEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Double2IntMap.Entry> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedDouble2IntOrderedMap extends AbstractDouble2IntMap implements Double2IntOrderedMap {
		Double2IntOrderedMap map;
		
		public ReversedDouble2IntOrderedMap(Double2IntOrderedMap map) {
			this.map = map;
		}
		@Override
		public AbstractDouble2IntMap setDefaultReturnValue(int v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public int getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Double2IntOrderedMap copy() { throw new UnsupportedOperationException(); }
		@Override
		public int put(double key, int value) { return map.put(key, value); }
		@Override
		public int putIfAbsent(double key, int value) { return map.putIfAbsent(key, value); }
		@Override
		public int addTo(double key, int value) { return map.addTo(key, value); }
		@Override
		public int subFrom(double key, int value) { return map.subFrom(key, value); }
		@Override
		public int remove(double key) { return map.remove(key); }
		@Override
		public boolean remove(double key, int value) { return map.remove(key, value); }
		@Override
		public int removeOrDefault(double key, int defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(double key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(int value) { return map.containsValue(value); }
		@Override
		public boolean replace(double key, int oldValue, int newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public int replace(double key, int value) { return map.replace(key, value); }
		@Override
		public void replaceInts(Double2IntMap m) { map.replaceInts(m); }
		@Override
		public void replaceInts(DoubleIntUnaryOperator mappingFunction) { map.replaceInts(mappingFunction); }
		@Override
		public int computeInt(double key, DoubleIntUnaryOperator mappingFunction) { return map.computeInt(key, mappingFunction); }
		@Override
		public int computeIntIfAbsent(double key, Double2IntFunction mappingFunction) { return map.computeIntIfAbsent(key, mappingFunction); }
		@Override
		public int supplyIntIfAbsent(double key, IntSupplier valueProvider) { return map.supplyIntIfAbsent(key, valueProvider); }
		@Override
		public int computeIntIfPresent(double key, DoubleIntUnaryOperator mappingFunction) { return map.computeIntIfPresent(key, mappingFunction); }
		@Override
		public int computeIntNonDefault(double key, DoubleIntUnaryOperator mappingFunction) { return map.computeIntNonDefault(key, mappingFunction); }
		@Override
		public int computeIntIfAbsentNonDefault(double key, Double2IntFunction mappingFunction) { return map.computeIntIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public int supplyIntIfAbsentNonDefault(double key, IntSupplier valueProvider) { return map.supplyIntIfAbsentNonDefault(key, valueProvider); }
		@Override
		public int computeIntIfPresentNonDefault(double key, DoubleIntUnaryOperator mappingFunction) { return map.computeIntIfPresentNonDefault(key, mappingFunction); }
		@Override
		public int mergeInt(double key, int value, IntIntUnaryOperator mappingFunction) { return map.mergeInt(key, value, mappingFunction); }
		@Override
		public int getOrDefault(double key, int defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public int get(double key) { return map.get(key); }
		@Override
		public int putAndMoveToFirst(double key, int value) { return map.putAndMoveToLast(key, value); }
		@Override
		public int putAndMoveToLast(double key, int value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public int putFirst(double key, int value) { return map.putLast(key, value); }
		@Override
		public int putLast(double key, int value) { return map.putFirst(key, value); }
		@Override
		public boolean moveToFirst(double key) { return map.moveToLast(key); }
		@Override
		public boolean moveToLast(double key) { return map.moveToFirst(key); }
		@Override
		public int getAndMoveToFirst(double key) { return map.getAndMoveToLast(key); }
		@Override
		public int getAndMoveToLast(double key) { return map.getAndMoveToFirst(key); }
		@Override
		public double firstDoubleKey() { return map.lastDoubleKey(); }
		@Override
		public double pollFirstDoubleKey() { return map.pollLastDoubleKey(); }
		@Override
		public double lastDoubleKey() { return map.firstDoubleKey(); }
		@Override
		public double pollLastDoubleKey() { return map.pollFirstDoubleKey(); }
		@Override
		public int firstIntValue() { return map.lastIntValue(); }
		@Override
		public int lastIntValue() { return map.firstIntValue(); }
		@Override
		public Double2IntMap.Entry firstEntry() { return map.lastEntry(); }
		@Override
		public Double2IntMap.Entry lastEntry() { return map.firstEntry(); }
		@Override
		public Double2IntMap.Entry pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Double2IntMap.Entry pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Double2IntMap.Entry> double2IntEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.double2IntEntrySet()); }
		@Override
		public DoubleOrderedSet keySet() { return new AbstractDoubleSet.ReversedDoubleOrderedSet(map.keySet()); }
		@Override
		public IntOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Double2IntOrderedMap reversed() { return map; }
	}

	
	/**
	 * A Simple Type Specific Entry class to reduce boxing/unboxing
	 */
	public static class BasicEntry implements Double2IntMap.Entry {
		protected double key;
		protected int value;
		
		/**
		 * A basic Empty constructor
		 */
		public BasicEntry() {}
		/**
		 * A Boxed Constructor for supporting java variants
		 * @param key the key of a entry
		 * @param value the value of a entry
		 */
		public BasicEntry(Double key, Integer value) {
			this.key = key.doubleValue();
			this.value = value.intValue();
		}
		
		/**
		 * A Type Specific Constructor
		 * @param key the key of a entry
		 * @param value the value of a entry 
		 */
		public BasicEntry(double key, int value) {
			this.key = key;
			this.value = value;
		}
		
		/**
		 * A Helper method for fast replacing values
		 * @param key the key that should be replaced
		 * @param value the value that should be replaced
		 */
		public void set(double key, int value) {
			this.key = key;
			this.value = value;
		}
		
		@Override
		public double getDoubleKey() {
			return key;
		}

		@Override
		public int getIntValue() {
			return value;
		}

		@Override
		public int setValue(int value) {
			throw new UnsupportedOperationException();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Map.Entry) {
				if(obj instanceof Double2IntMap.Entry) {
					Double2IntMap.Entry entry = (Double2IntMap.Entry)obj;
					return Double.doubleToLongBits(key) == Double.doubleToLongBits(entry.getDoubleKey()) && value == entry.getIntValue();
				}
				Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
				Object key = entry.getKey();
				Object value = entry.getValue();
				return key instanceof Double && value instanceof Integer && Double.doubleToLongBits(this.key) == Double.doubleToLongBits(((Double)key).doubleValue()) && this.value == ((Integer)value).intValue();
			}
			return false;
		}
		
		@Override
		public int hashCode() {
			return Double.hashCode(key) ^ Integer.hashCode(value);
		}
		
		@Override
		public String toString() {
			return Double.toString(key) + "=" + Integer.toString(value);
		}
	}
}