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

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

import speiger.src.collections.floats.collections.FloatIterator;
import speiger.src.collections.floats.functions.consumer.FloatDoubleConsumer;
import speiger.src.collections.floats.functions.function.Float2DoubleFunction;
import speiger.src.collections.floats.functions.function.FloatDoubleUnaryOperator;
import speiger.src.collections.floats.maps.interfaces.Float2DoubleMap;
import speiger.src.collections.floats.maps.interfaces.Float2DoubleOrderedMap;
import speiger.src.collections.floats.sets.FloatOrderedSet;
import speiger.src.collections.doubles.collections.DoubleOrderedCollection;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
import speiger.src.collections.floats.sets.AbstractFloatSet;
import speiger.src.collections.floats.sets.FloatSet;
import speiger.src.collections.floats.utils.maps.Float2DoubleMaps;
import speiger.src.collections.doubles.collections.AbstractDoubleCollection;
import speiger.src.collections.doubles.collections.DoubleCollection;
import speiger.src.collections.doubles.collections.DoubleIterator;
import speiger.src.collections.doubles.functions.function.DoubleDoubleUnaryOperator;
import speiger.src.collections.doubles.functions.DoubleSupplier;
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 AbstractFloat2DoubleMap extends AbstractMap<Float, Double> implements Float2DoubleMap
{
	protected double defaultReturnValue = -1D;
	
	@Override
	public double getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractFloat2DoubleMap setDefaultReturnValue(double v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Float2DoubleMap.Entry> getFastIterable(Float2DoubleMap map) {
		return Float2DoubleMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Float2DoubleMap.Entry> getFastIterator(Float2DoubleMap map) {
		return Float2DoubleMaps.fastIterator(map);
	}
	
	@Override
	public Float2DoubleMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Double put(Float key, Double value) {
		return Double.valueOf(put(key.floatValue(), value.doubleValue()));
	}
	
	@Override
	public void addToAll(Float2DoubleMap m) {
		for(Float2DoubleMap.Entry entry : getFastIterable(m))
			addTo(entry.getFloatKey(), entry.getDoubleValue());
	}
	
	@Override
	public void putAll(Float2DoubleMap m) {
		for(ObjectIterator<Float2DoubleMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Float2DoubleMap.Entry entry = iter.next();
			put(entry.getFloatKey(), entry.getDoubleValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Float, ? extends Double> m)
	{
		if(m instanceof Float2DoubleMap) putAll((Float2DoubleMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(float[] keys, double[] 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(Float[] keys, Double[] 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].floatValue(), values[i].doubleValue());		
	}
	
	@Override
	public void putAllIfAbsent(Float2DoubleMap m) {
		for(Float2DoubleMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getFloatKey(), entry.getDoubleValue());
	}
	
	
	@Override
	public boolean containsKey(float key) {
		for(FloatIterator iter = keySet().iterator();iter.hasNext();)
			if(Float.floatToIntBits(iter.nextFloat()) == Float.floatToIntBits(key)) return true;
		return false;
	}
	
	@Override
	public boolean containsValue(double value) {
		for(DoubleIterator iter = values().iterator();iter.hasNext();)
			if(Double.doubleToLongBits(iter.nextDouble()) == Double.doubleToLongBits(value)) return true;
		return false;
	}
	
	@Override
	public boolean replace(float key, double oldValue, double newValue) {
		double curValue = get(key);
		if (Double.doubleToLongBits(curValue) != Double.doubleToLongBits(oldValue) || (Double.doubleToLongBits(curValue) == Double.doubleToLongBits(getDefaultReturnValue()) && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public double replace(float key, double value) {
		double curValue;
		if (Double.doubleToLongBits((curValue = get(key))) != Double.doubleToLongBits(getDefaultReturnValue()) || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceDoubles(Float2DoubleMap m) {
		for(Float2DoubleMap.Entry entry : getFastIterable(m))
			replace(entry.getFloatKey(), entry.getDoubleValue());
	}
	
	@Override
	public void replaceDoubles(FloatDoubleUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Float2DoubleMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Float2DoubleMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsDouble(entry.getFloatKey(), entry.getDoubleValue()));
		}
	}

	@Override
	public double computeDouble(float key, FloatDoubleUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		double newValue = mappingFunction.applyAsDouble(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public double computeDoubleIfAbsent(float key, Float2DoubleFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			double newValue = mappingFunction.applyAsDouble(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public double supplyDoubleIfAbsent(float key, DoubleSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			double newValue = valueProvider.getAsDouble();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public double computeDoubleIfPresent(float key, FloatDoubleUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			double newValue = mappingFunction.applyAsDouble(key, get(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public double computeDoubleNonDefault(float key, FloatDoubleUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		double value = get(key);
		double newValue = mappingFunction.applyAsDouble(key, value);
		if(Double.doubleToLongBits(newValue) == Double.doubleToLongBits(getDefaultReturnValue())) {
			if(Double.doubleToLongBits(value) != Double.doubleToLongBits(getDefaultReturnValue()) || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public double computeDoubleIfAbsentNonDefault(float key, Float2DoubleFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		double value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			double newValue = mappingFunction.applyAsDouble(key);
			if(Double.doubleToLongBits(newValue) != Double.doubleToLongBits(getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public double supplyDoubleIfAbsentNonDefault(float key, DoubleSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		double value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			double newValue = valueProvider.getAsDouble();
			if(Double.doubleToLongBits(newValue) != Double.doubleToLongBits(getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public double computeDoubleIfPresentNonDefault(float key, FloatDoubleUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		double value;
		if(Double.doubleToLongBits((value = get(key))) != Double.doubleToLongBits(getDefaultReturnValue()) || containsKey(key)) {
			double newValue = mappingFunction.applyAsDouble(key, value);
			if(Double.doubleToLongBits(newValue) != Double.doubleToLongBits(getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public double mergeDouble(float key, double value, DoubleDoubleUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		double oldValue = get(key);
		double newValue = Double.doubleToLongBits(oldValue) == Double.doubleToLongBits(getDefaultReturnValue()) ? value : mappingFunction.applyAsDouble(oldValue, value);
		if(Double.doubleToLongBits(newValue) == Double.doubleToLongBits(getDefaultReturnValue())) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllDouble(Float2DoubleMap m, DoubleDoubleUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Float2DoubleMap.Entry entry : getFastIterable(m)) {
			float key = entry.getFloatKey();
			double oldValue = get(key);
			double newValue = Double.doubleToLongBits(oldValue) == Double.doubleToLongBits(getDefaultReturnValue()) ? entry.getDoubleValue() : mappingFunction.applyAsDouble(oldValue, entry.getDoubleValue());
			if(Double.doubleToLongBits(newValue) == Double.doubleToLongBits(getDefaultReturnValue())) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Double get(Object key) {
		return Double.valueOf(key instanceof Float ? get(((Float)key).floatValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Double getOrDefault(Object key, Double defaultValue) {
		return Double.valueOf(key instanceof Float ? getOrDefault(((Float)key).floatValue(), defaultValue.doubleValue()) : getDefaultReturnValue());
	}
	
	@Override
	public double getOrDefault(float key, double defaultValue) {
		double value = get(key);
		return Double.doubleToLongBits(value) != Double.doubleToLongBits(getDefaultReturnValue()) || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Double remove(Object key) {
		return key instanceof Float ? Double.valueOf(remove(((Float)key).floatValue())) : Double.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(FloatDoubleConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Float2DoubleMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Float2DoubleMap.Entry entry = iter.next();
			action.accept(entry.getFloatKey(), entry.getDoubleValue());
		}
	}

	@Override
	public FloatSet keySet() {
		return new AbstractFloatSet() {
			@Override
			public boolean remove(float o) {
				return Double.doubleToLongBits(AbstractFloat2DoubleMap.this.remove(o)) != Double.doubleToLongBits(getDefaultReturnValue());
			}
			
			@Override
			public boolean add(float o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public FloatIterator iterator() {
				return new FloatIterator() {
					ObjectIterator<Float2DoubleMap.Entry> iter = getFastIterator(AbstractFloat2DoubleMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}

					@Override
					public float nextFloat() {
						return iter.next().getFloatKey();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
			
			@Override
			public int size() {
				return AbstractFloat2DoubleMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractFloat2DoubleMap.this.clear();
			}
		};
	}

	@Override
	public DoubleCollection values() {
		return new AbstractDoubleCollection() {
			@Override
			public boolean add(double o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractFloat2DoubleMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractFloat2DoubleMap.this.clear();
			}
			
			@Override
			public DoubleIterator iterator() {
				return new DoubleIterator() {
					ObjectIterator<Float2DoubleMap.Entry> iter = getFastIterator(AbstractFloat2DoubleMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public double nextDouble() {
						return iter.next().getDoubleValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Float, Double>> entrySet() {
		return (ObjectSet)float2DoubleEntrySet();
	}

	@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 Float2DoubleMap) return float2DoubleEntrySet().containsAll(((Float2DoubleMap)o).float2DoubleEntrySet());
			return float2DoubleEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Float2DoubleMap.Entry> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedFloat2DoubleOrderedMap extends AbstractFloat2DoubleMap implements Float2DoubleOrderedMap {
		Float2DoubleOrderedMap map;
		
		public ReversedFloat2DoubleOrderedMap(Float2DoubleOrderedMap map) {
			this.map = map;
		}
		@Override
		public AbstractFloat2DoubleMap setDefaultReturnValue(double v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public double getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Float2DoubleOrderedMap copy() { throw new UnsupportedOperationException(); }
		@Override
		public double put(float key, double value) { return map.put(key, value); }
		@Override
		public double putIfAbsent(float key, double value) { return map.putIfAbsent(key, value); }
		@Override
		public double addTo(float key, double value) { return map.addTo(key, value); }
		@Override
		public double subFrom(float key, double value) { return map.subFrom(key, value); }
		@Override
		public double remove(float key) { return map.remove(key); }
		@Override
		public boolean remove(float key, double value) { return map.remove(key, value); }
		@Override
		public double removeOrDefault(float key, double defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(float key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(double value) { return map.containsValue(value); }
		@Override
		public boolean replace(float key, double oldValue, double newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public double replace(float key, double value) { return map.replace(key, value); }
		@Override
		public void replaceDoubles(Float2DoubleMap m) { map.replaceDoubles(m); }
		@Override
		public void replaceDoubles(FloatDoubleUnaryOperator mappingFunction) { map.replaceDoubles(mappingFunction); }
		@Override
		public double computeDouble(float key, FloatDoubleUnaryOperator mappingFunction) { return map.computeDouble(key, mappingFunction); }
		@Override
		public double computeDoubleIfAbsent(float key, Float2DoubleFunction mappingFunction) { return map.computeDoubleIfAbsent(key, mappingFunction); }
		@Override
		public double supplyDoubleIfAbsent(float key, DoubleSupplier valueProvider) { return map.supplyDoubleIfAbsent(key, valueProvider); }
		@Override
		public double computeDoubleIfPresent(float key, FloatDoubleUnaryOperator mappingFunction) { return map.computeDoubleIfPresent(key, mappingFunction); }
		@Override
		public double computeDoubleNonDefault(float key, FloatDoubleUnaryOperator mappingFunction) { return map.computeDoubleNonDefault(key, mappingFunction); }
		@Override
		public double computeDoubleIfAbsentNonDefault(float key, Float2DoubleFunction mappingFunction) { return map.computeDoubleIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public double supplyDoubleIfAbsentNonDefault(float key, DoubleSupplier valueProvider) { return map.supplyDoubleIfAbsentNonDefault(key, valueProvider); }
		@Override
		public double computeDoubleIfPresentNonDefault(float key, FloatDoubleUnaryOperator mappingFunction) { return map.computeDoubleIfPresentNonDefault(key, mappingFunction); }
		@Override
		public double mergeDouble(float key, double value, DoubleDoubleUnaryOperator mappingFunction) { return map.mergeDouble(key, value, mappingFunction); }
		@Override
		public double getOrDefault(float key, double defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public double get(float key) { return map.get(key); }
		@Override
		public double putAndMoveToFirst(float key, double value) { return map.putAndMoveToLast(key, value); }
		@Override
		public double putAndMoveToLast(float key, double value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public double putFirst(float key, double value) { return map.putLast(key, value); }
		@Override
		public double putLast(float key, double value) { return map.putFirst(key, value); }
		@Override
		public boolean moveToFirst(float key) { return map.moveToLast(key); }
		@Override
		public boolean moveToLast(float key) { return map.moveToFirst(key); }
		@Override
		public double getAndMoveToFirst(float key) { return map.getAndMoveToLast(key); }
		@Override
		public double getAndMoveToLast(float key) { return map.getAndMoveToFirst(key); }
		@Override
		public float firstFloatKey() { return map.lastFloatKey(); }
		@Override
		public float pollFirstFloatKey() { return map.pollLastFloatKey(); }
		@Override
		public float lastFloatKey() { return map.firstFloatKey(); }
		@Override
		public float pollLastFloatKey() { return map.pollFirstFloatKey(); }
		@Override
		public double firstDoubleValue() { return map.lastDoubleValue(); }
		@Override
		public double lastDoubleValue() { return map.firstDoubleValue(); }
		@Override
		public Float2DoubleMap.Entry firstEntry() { return map.lastEntry(); }
		@Override
		public Float2DoubleMap.Entry lastEntry() { return map.firstEntry(); }
		@Override
		public Float2DoubleMap.Entry pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Float2DoubleMap.Entry pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Float2DoubleMap.Entry> float2DoubleEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.float2DoubleEntrySet()); }
		@Override
		public FloatOrderedSet keySet() { return new AbstractFloatSet.ReversedFloatOrderedSet(map.keySet()); }
		@Override
		public DoubleOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Float2DoubleOrderedMap reversed() { return map; }
	}

	
	/**
	 * A Simple Type Specific Entry class to reduce boxing/unboxing
	 */
	public static class BasicEntry implements Float2DoubleMap.Entry {
		protected float key;
		protected double 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(Float key, Double value) {
			this.key = key.floatValue();
			this.value = value.doubleValue();
		}
		
		/**
		 * A Type Specific Constructor
		 * @param key the key of a entry
		 * @param value the value of a entry 
		 */
		public BasicEntry(float key, double 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(float key, double value) {
			this.key = key;
			this.value = value;
		}
		
		@Override
		public float getFloatKey() {
			return key;
		}

		@Override
		public double getDoubleValue() {
			return value;
		}

		@Override
		public double setValue(double value) {
			throw new UnsupportedOperationException();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Map.Entry) {
				if(obj instanceof Float2DoubleMap.Entry) {
					Float2DoubleMap.Entry entry = (Float2DoubleMap.Entry)obj;
					return Float.floatToIntBits(key) == Float.floatToIntBits(entry.getFloatKey()) && Double.doubleToLongBits(value) == Double.doubleToLongBits(entry.getDoubleValue());
				}
				Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
				Object key = entry.getKey();
				Object value = entry.getValue();
				return key instanceof Float && value instanceof Double && Float.floatToIntBits(this.key) == Float.floatToIntBits(((Float)key).floatValue()) && Double.doubleToLongBits(this.value) == Double.doubleToLongBits(((Double)value).doubleValue());
			}
			return false;
		}
		
		@Override
		public int hashCode() {
			return Float.hashCode(key) ^ Double.hashCode(value);
		}
		
		@Override
		public String toString() {
			return Float.toString(key) + "=" + Double.toString(value);
		}
	}
}