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.FloatBooleanConsumer;
import speiger.src.collections.floats.functions.function.FloatPredicate;
import speiger.src.collections.floats.functions.function.FloatBooleanUnaryOperator;
import speiger.src.collections.floats.maps.interfaces.Float2BooleanMap;
import speiger.src.collections.floats.maps.interfaces.Float2BooleanOrderedMap;
import speiger.src.collections.floats.sets.FloatOrderedSet;
import speiger.src.collections.booleans.collections.BooleanOrderedCollection;
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.Float2BooleanMaps;
import speiger.src.collections.booleans.collections.AbstractBooleanCollection;
import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
import speiger.src.collections.booleans.functions.BooleanSupplier;
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 AbstractFloat2BooleanMap extends AbstractMap<Float, Boolean> implements Float2BooleanMap
{
	protected boolean defaultReturnValue = false;
	
	@Override
	public boolean getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractFloat2BooleanMap setDefaultReturnValue(boolean v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Float2BooleanMap.Entry> getFastIterable(Float2BooleanMap map) {
		return Float2BooleanMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Float2BooleanMap.Entry> getFastIterator(Float2BooleanMap map) {
		return Float2BooleanMaps.fastIterator(map);
	}
	
	@Override
	public Float2BooleanMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Boolean put(Float key, Boolean value) {
		return Boolean.valueOf(put(key.floatValue(), value.booleanValue()));
	}
	
	@Override
	public void putAll(Float2BooleanMap m) {
		for(ObjectIterator<Float2BooleanMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Float2BooleanMap.Entry entry = iter.next();
			put(entry.getFloatKey(), entry.getBooleanValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Float, ? extends Boolean> m)
	{
		if(m instanceof Float2BooleanMap) putAll((Float2BooleanMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(float[] keys, boolean[] 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, Boolean[] 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].booleanValue());		
	}
	
	@Override
	public void putAllIfAbsent(Float2BooleanMap m) {
		for(Float2BooleanMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getFloatKey(), entry.getBooleanValue());
	}
	
	
	@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(boolean value) {
		for(BooleanIterator iter = values().iterator();iter.hasNext();)
			if(iter.nextBoolean() == value) return true;
		return false;
	}
	
	@Override
	public boolean replace(float key, boolean oldValue, boolean newValue) {
		boolean curValue = get(key);
		if (curValue != oldValue || (curValue == getDefaultReturnValue() && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public boolean replace(float key, boolean value) {
		boolean curValue;
		if ((curValue = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceBooleans(Float2BooleanMap m) {
		for(Float2BooleanMap.Entry entry : getFastIterable(m))
			replace(entry.getFloatKey(), entry.getBooleanValue());
	}
	
	@Override
	public void replaceBooleans(FloatBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Float2BooleanMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Float2BooleanMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsBoolean(entry.getFloatKey(), entry.getBooleanValue()));
		}
	}

	@Override
	public boolean computeBoolean(float key, FloatBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean newValue = mappingFunction.applyAsBoolean(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public boolean computeBooleanIfAbsent(float key, FloatPredicate mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			boolean newValue = mappingFunction.test(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public boolean supplyBooleanIfAbsent(float key, BooleanSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			boolean newValue = valueProvider.getAsBoolean();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public boolean computeBooleanIfPresent(float key, FloatBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			boolean newValue = mappingFunction.applyAsBoolean(key, get(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public boolean computeBooleanNonDefault(float key, FloatBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean value = get(key);
		boolean newValue = mappingFunction.applyAsBoolean(key, value);
		if(newValue == getDefaultReturnValue()) {
			if(value != getDefaultReturnValue() || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public boolean computeBooleanIfAbsentNonDefault(float key, FloatPredicate mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			boolean newValue = mappingFunction.test(key);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public boolean supplyBooleanIfAbsentNonDefault(float key, BooleanSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		boolean value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			boolean newValue = valueProvider.getAsBoolean();
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public boolean computeBooleanIfPresentNonDefault(float key, FloatBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean value;
		if((value = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			boolean newValue = mappingFunction.applyAsBoolean(key, value);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public boolean mergeBoolean(float key, boolean value, BooleanBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean oldValue = get(key);
		boolean newValue = oldValue == getDefaultReturnValue() ? value : mappingFunction.applyAsBoolean(oldValue, value);
		if(newValue == getDefaultReturnValue()) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllBoolean(Float2BooleanMap m, BooleanBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Float2BooleanMap.Entry entry : getFastIterable(m)) {
			float key = entry.getFloatKey();
			boolean oldValue = get(key);
			boolean newValue = oldValue == getDefaultReturnValue() ? entry.getBooleanValue() : mappingFunction.applyAsBoolean(oldValue, entry.getBooleanValue());
			if(newValue == getDefaultReturnValue()) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Boolean get(Object key) {
		return Boolean.valueOf(key instanceof Float ? get(((Float)key).floatValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Boolean getOrDefault(Object key, Boolean defaultValue) {
		return Boolean.valueOf(key instanceof Float ? getOrDefault(((Float)key).floatValue(), defaultValue.booleanValue()) : getDefaultReturnValue());
	}
	
	@Override
	public boolean getOrDefault(float key, boolean defaultValue) {
		boolean value = get(key);
		return value != getDefaultReturnValue() || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Boolean remove(Object key) {
		return key instanceof Float ? Boolean.valueOf(remove(((Float)key).floatValue())) : Boolean.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(FloatBooleanConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Float2BooleanMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Float2BooleanMap.Entry entry = iter.next();
			action.accept(entry.getFloatKey(), entry.getBooleanValue());
		}
	}

	@Override
	public FloatSet keySet() {
		return new AbstractFloatSet() {
			@Override
			public boolean remove(float o) {
				return AbstractFloat2BooleanMap.this.remove(o) != getDefaultReturnValue();
			}
			
			@Override
			public boolean add(float o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public FloatIterator iterator() {
				return new FloatIterator() {
					ObjectIterator<Float2BooleanMap.Entry> iter = getFastIterator(AbstractFloat2BooleanMap.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 AbstractFloat2BooleanMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractFloat2BooleanMap.this.clear();
			}
		};
	}

	@Override
	public BooleanCollection values() {
		return new AbstractBooleanCollection() {
			@Override
			public boolean add(boolean o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractFloat2BooleanMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractFloat2BooleanMap.this.clear();
			}
			
			@Override
			public BooleanIterator iterator() {
				return new BooleanIterator() {
					ObjectIterator<Float2BooleanMap.Entry> iter = getFastIterator(AbstractFloat2BooleanMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public boolean nextBoolean() {
						return iter.next().getBooleanValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Float, Boolean>> entrySet() {
		return (ObjectSet)float2BooleanEntrySet();
	}

	@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 Float2BooleanMap) return float2BooleanEntrySet().containsAll(((Float2BooleanMap)o).float2BooleanEntrySet());
			return float2BooleanEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Float2BooleanMap.Entry> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedFloat2BooleanOrderedMap extends AbstractFloat2BooleanMap implements Float2BooleanOrderedMap {
		Float2BooleanOrderedMap map;
		
		public ReversedFloat2BooleanOrderedMap(Float2BooleanOrderedMap map) {
			this.map = map;
		}
		@Override
		public AbstractFloat2BooleanMap setDefaultReturnValue(boolean v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public boolean getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Float2BooleanOrderedMap copy() { throw new UnsupportedOperationException(); }
		@Override
		public boolean put(float key, boolean value) { return map.put(key, value); }
		@Override
		public boolean putIfAbsent(float key, boolean value) { return map.putIfAbsent(key, value); }
		@Override
		public boolean remove(float key) { return map.remove(key); }
		@Override
		public boolean remove(float key, boolean value) { return map.remove(key, value); }
		@Override
		public boolean removeOrDefault(float key, boolean defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(float key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(boolean value) { return map.containsValue(value); }
		@Override
		public boolean replace(float key, boolean oldValue, boolean newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public boolean replace(float key, boolean value) { return map.replace(key, value); }
		@Override
		public void replaceBooleans(Float2BooleanMap m) { map.replaceBooleans(m); }
		@Override
		public void replaceBooleans(FloatBooleanUnaryOperator mappingFunction) { map.replaceBooleans(mappingFunction); }
		@Override
		public boolean computeBoolean(float key, FloatBooleanUnaryOperator mappingFunction) { return map.computeBoolean(key, mappingFunction); }
		@Override
		public boolean computeBooleanIfAbsent(float key, FloatPredicate mappingFunction) { return map.computeBooleanIfAbsent(key, mappingFunction); }
		@Override
		public boolean supplyBooleanIfAbsent(float key, BooleanSupplier valueProvider) { return map.supplyBooleanIfAbsent(key, valueProvider); }
		@Override
		public boolean computeBooleanIfPresent(float key, FloatBooleanUnaryOperator mappingFunction) { return map.computeBooleanIfPresent(key, mappingFunction); }
		@Override
		public boolean computeBooleanNonDefault(float key, FloatBooleanUnaryOperator mappingFunction) { return map.computeBooleanNonDefault(key, mappingFunction); }
		@Override
		public boolean computeBooleanIfAbsentNonDefault(float key, FloatPredicate mappingFunction) { return map.computeBooleanIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public boolean supplyBooleanIfAbsentNonDefault(float key, BooleanSupplier valueProvider) { return map.supplyBooleanIfAbsentNonDefault(key, valueProvider); }
		@Override
		public boolean computeBooleanIfPresentNonDefault(float key, FloatBooleanUnaryOperator mappingFunction) { return map.computeBooleanIfPresentNonDefault(key, mappingFunction); }
		@Override
		public boolean mergeBoolean(float key, boolean value, BooleanBooleanUnaryOperator mappingFunction) { return map.mergeBoolean(key, value, mappingFunction); }
		@Override
		public boolean getOrDefault(float key, boolean defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public boolean get(float key) { return map.get(key); }
		@Override
		public boolean putAndMoveToFirst(float key, boolean value) { return map.putAndMoveToLast(key, value); }
		@Override
		public boolean putAndMoveToLast(float key, boolean value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public boolean putFirst(float key, boolean value) { return map.putLast(key, value); }
		@Override
		public boolean putLast(float key, boolean 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 boolean getAndMoveToFirst(float key) { return map.getAndMoveToLast(key); }
		@Override
		public boolean 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 boolean firstBooleanValue() { return map.lastBooleanValue(); }
		@Override
		public boolean lastBooleanValue() { return map.firstBooleanValue(); }
		@Override
		public Float2BooleanMap.Entry firstEntry() { return map.lastEntry(); }
		@Override
		public Float2BooleanMap.Entry lastEntry() { return map.firstEntry(); }
		@Override
		public Float2BooleanMap.Entry pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Float2BooleanMap.Entry pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Float2BooleanMap.Entry> float2BooleanEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.float2BooleanEntrySet()); }
		@Override
		public FloatOrderedSet keySet() { return new AbstractFloatSet.ReversedFloatOrderedSet(map.keySet()); }
		@Override
		public BooleanOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Float2BooleanOrderedMap reversed() { return map; }
	}

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

		@Override
		public boolean getBooleanValue() {
			return value;
		}

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