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.FloatCharConsumer;
import speiger.src.collections.floats.functions.function.Float2CharFunction;
import speiger.src.collections.floats.functions.function.FloatCharUnaryOperator;
import speiger.src.collections.floats.maps.interfaces.Float2CharMap;
import speiger.src.collections.floats.maps.interfaces.Float2CharOrderedMap;
import speiger.src.collections.floats.sets.FloatOrderedSet;
import speiger.src.collections.chars.collections.CharOrderedCollection;
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.Float2CharMaps;
import speiger.src.collections.chars.collections.AbstractCharCollection;
import speiger.src.collections.chars.collections.CharCollection;
import speiger.src.collections.chars.collections.CharIterator;
import speiger.src.collections.chars.functions.function.CharCharUnaryOperator;
import speiger.src.collections.chars.functions.CharSupplier;
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 AbstractFloat2CharMap extends AbstractMap<Float, Character> implements Float2CharMap
{
	protected char defaultReturnValue = (char)-1;
	
	@Override
	public char getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractFloat2CharMap setDefaultReturnValue(char v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Float2CharMap.Entry> getFastIterable(Float2CharMap map) {
		return Float2CharMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Float2CharMap.Entry> getFastIterator(Float2CharMap map) {
		return Float2CharMaps.fastIterator(map);
	}
	
	@Override
	public Float2CharMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Character put(Float key, Character value) {
		return Character.valueOf(put(key.floatValue(), value.charValue()));
	}
	
	@Override
	public void addToAll(Float2CharMap m) {
		for(Float2CharMap.Entry entry : getFastIterable(m))
			addTo(entry.getFloatKey(), entry.getCharValue());
	}
	
	@Override
	public void putAll(Float2CharMap m) {
		for(ObjectIterator<Float2CharMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Float2CharMap.Entry entry = iter.next();
			put(entry.getFloatKey(), entry.getCharValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Float, ? extends Character> m)
	{
		if(m instanceof Float2CharMap) putAll((Float2CharMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(float[] keys, char[] 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, Character[] 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].charValue());		
	}
	
	@Override
	public void putAllIfAbsent(Float2CharMap m) {
		for(Float2CharMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getFloatKey(), entry.getCharValue());
	}
	
	
	@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(char value) {
		for(CharIterator iter = values().iterator();iter.hasNext();)
			if(iter.nextChar() == value) return true;
		return false;
	}
	
	@Override
	public boolean replace(float key, char oldValue, char newValue) {
		char curValue = get(key);
		if (curValue != oldValue || (curValue == getDefaultReturnValue() && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public char replace(float key, char value) {
		char curValue;
		if ((curValue = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceChars(Float2CharMap m) {
		for(Float2CharMap.Entry entry : getFastIterable(m))
			replace(entry.getFloatKey(), entry.getCharValue());
	}
	
	@Override
	public void replaceChars(FloatCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Float2CharMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Float2CharMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsChar(entry.getFloatKey(), entry.getCharValue()));
		}
	}

	@Override
	public char computeChar(float key, FloatCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char newValue = mappingFunction.applyAsChar(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public char computeCharIfAbsent(float key, Float2CharFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			char newValue = mappingFunction.applyAsChar(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public char supplyCharIfAbsent(float key, CharSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			char newValue = valueProvider.getAsChar();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public char computeCharIfPresent(float key, FloatCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			char newValue = mappingFunction.applyAsChar(key, get(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public char computeCharNonDefault(float key, FloatCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char value = get(key);
		char newValue = mappingFunction.applyAsChar(key, value);
		if(newValue == getDefaultReturnValue()) {
			if(value != getDefaultReturnValue() || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public char computeCharIfAbsentNonDefault(float key, Float2CharFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			char newValue = mappingFunction.applyAsChar(key);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public char supplyCharIfAbsentNonDefault(float key, CharSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		char value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			char newValue = valueProvider.getAsChar();
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public char computeCharIfPresentNonDefault(float key, FloatCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char value;
		if((value = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			char newValue = mappingFunction.applyAsChar(key, value);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public char mergeChar(float key, char value, CharCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char oldValue = get(key);
		char newValue = oldValue == getDefaultReturnValue() ? value : mappingFunction.applyAsChar(oldValue, value);
		if(newValue == getDefaultReturnValue()) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllChar(Float2CharMap m, CharCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Float2CharMap.Entry entry : getFastIterable(m)) {
			float key = entry.getFloatKey();
			char oldValue = get(key);
			char newValue = oldValue == getDefaultReturnValue() ? entry.getCharValue() : mappingFunction.applyAsChar(oldValue, entry.getCharValue());
			if(newValue == getDefaultReturnValue()) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Character get(Object key) {
		return Character.valueOf(key instanceof Float ? get(((Float)key).floatValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Character getOrDefault(Object key, Character defaultValue) {
		return Character.valueOf(key instanceof Float ? getOrDefault(((Float)key).floatValue(), defaultValue.charValue()) : getDefaultReturnValue());
	}
	
	@Override
	public char getOrDefault(float key, char defaultValue) {
		char value = get(key);
		return value != getDefaultReturnValue() || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Character remove(Object key) {
		return key instanceof Float ? Character.valueOf(remove(((Float)key).floatValue())) : Character.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(FloatCharConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Float2CharMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Float2CharMap.Entry entry = iter.next();
			action.accept(entry.getFloatKey(), entry.getCharValue());
		}
	}

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

	@Override
	public CharCollection values() {
		return new AbstractCharCollection() {
			@Override
			public boolean add(char o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractFloat2CharMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractFloat2CharMap.this.clear();
			}
			
			@Override
			public CharIterator iterator() {
				return new CharIterator() {
					ObjectIterator<Float2CharMap.Entry> iter = getFastIterator(AbstractFloat2CharMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public char nextChar() {
						return iter.next().getCharValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Float, Character>> entrySet() {
		return (ObjectSet)float2CharEntrySet();
	}

	@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 Float2CharMap) return float2CharEntrySet().containsAll(((Float2CharMap)o).float2CharEntrySet());
			return float2CharEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Float2CharMap.Entry> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedFloat2CharOrderedMap extends AbstractFloat2CharMap implements Float2CharOrderedMap {
		Float2CharOrderedMap map;
		
		public ReversedFloat2CharOrderedMap(Float2CharOrderedMap map) {
			this.map = map;
		}
		@Override
		public AbstractFloat2CharMap setDefaultReturnValue(char v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public char getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Float2CharOrderedMap copy() { throw new UnsupportedOperationException(); }
		@Override
		public char put(float key, char value) { return map.put(key, value); }
		@Override
		public char putIfAbsent(float key, char value) { return map.putIfAbsent(key, value); }
		@Override
		public char addTo(float key, char value) { return map.addTo(key, value); }
		@Override
		public char subFrom(float key, char value) { return map.subFrom(key, value); }
		@Override
		public char remove(float key) { return map.remove(key); }
		@Override
		public boolean remove(float key, char value) { return map.remove(key, value); }
		@Override
		public char removeOrDefault(float key, char defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(float key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(char value) { return map.containsValue(value); }
		@Override
		public boolean replace(float key, char oldValue, char newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public char replace(float key, char value) { return map.replace(key, value); }
		@Override
		public void replaceChars(Float2CharMap m) { map.replaceChars(m); }
		@Override
		public void replaceChars(FloatCharUnaryOperator mappingFunction) { map.replaceChars(mappingFunction); }
		@Override
		public char computeChar(float key, FloatCharUnaryOperator mappingFunction) { return map.computeChar(key, mappingFunction); }
		@Override
		public char computeCharIfAbsent(float key, Float2CharFunction mappingFunction) { return map.computeCharIfAbsent(key, mappingFunction); }
		@Override
		public char supplyCharIfAbsent(float key, CharSupplier valueProvider) { return map.supplyCharIfAbsent(key, valueProvider); }
		@Override
		public char computeCharIfPresent(float key, FloatCharUnaryOperator mappingFunction) { return map.computeCharIfPresent(key, mappingFunction); }
		@Override
		public char computeCharNonDefault(float key, FloatCharUnaryOperator mappingFunction) { return map.computeCharNonDefault(key, mappingFunction); }
		@Override
		public char computeCharIfAbsentNonDefault(float key, Float2CharFunction mappingFunction) { return map.computeCharIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public char supplyCharIfAbsentNonDefault(float key, CharSupplier valueProvider) { return map.supplyCharIfAbsentNonDefault(key, valueProvider); }
		@Override
		public char computeCharIfPresentNonDefault(float key, FloatCharUnaryOperator mappingFunction) { return map.computeCharIfPresentNonDefault(key, mappingFunction); }
		@Override
		public char mergeChar(float key, char value, CharCharUnaryOperator mappingFunction) { return map.mergeChar(key, value, mappingFunction); }
		@Override
		public char getOrDefault(float key, char defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public char get(float key) { return map.get(key); }
		@Override
		public char putAndMoveToFirst(float key, char value) { return map.putAndMoveToLast(key, value); }
		@Override
		public char putAndMoveToLast(float key, char value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public char putFirst(float key, char value) { return map.putLast(key, value); }
		@Override
		public char putLast(float key, char 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 char getAndMoveToFirst(float key) { return map.getAndMoveToLast(key); }
		@Override
		public char 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 char firstCharValue() { return map.lastCharValue(); }
		@Override
		public char lastCharValue() { return map.firstCharValue(); }
		@Override
		public Float2CharMap.Entry firstEntry() { return map.lastEntry(); }
		@Override
		public Float2CharMap.Entry lastEntry() { return map.firstEntry(); }
		@Override
		public Float2CharMap.Entry pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Float2CharMap.Entry pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Float2CharMap.Entry> float2CharEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.float2CharEntrySet()); }
		@Override
		public FloatOrderedSet keySet() { return new AbstractFloatSet.ReversedFloatOrderedSet(map.keySet()); }
		@Override
		public CharOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Float2CharOrderedMap reversed() { return map; }
	}

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

		@Override
		public char getCharValue() {
			return value;
		}

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