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

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

import speiger.src.collections.bytes.collections.ByteIterator;
import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer;
import speiger.src.collections.bytes.functions.function.Byte2FloatFunction;
import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator;
import speiger.src.collections.bytes.maps.interfaces.Byte2FloatMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2FloatOrderedMap;
import speiger.src.collections.bytes.sets.ByteOrderedSet;
import speiger.src.collections.floats.collections.FloatOrderedCollection;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
import speiger.src.collections.bytes.sets.AbstractByteSet;
import speiger.src.collections.bytes.sets.ByteSet;
import speiger.src.collections.bytes.utils.maps.Byte2FloatMaps;
import speiger.src.collections.floats.collections.AbstractFloatCollection;
import speiger.src.collections.floats.collections.FloatCollection;
import speiger.src.collections.floats.collections.FloatIterator;
import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator;
import speiger.src.collections.floats.functions.FloatSupplier;
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 AbstractByte2FloatMap extends AbstractMap<Byte, Float> implements Byte2FloatMap
{
	protected float defaultReturnValue = -1F;
	
	@Override
	public float getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractByte2FloatMap setDefaultReturnValue(float v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Byte2FloatMap.Entry> getFastIterable(Byte2FloatMap map) {
		return Byte2FloatMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Byte2FloatMap.Entry> getFastIterator(Byte2FloatMap map) {
		return Byte2FloatMaps.fastIterator(map);
	}
	
	@Override
	public Byte2FloatMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Float put(Byte key, Float value) {
		return Float.valueOf(put(key.byteValue(), value.floatValue()));
	}
	
	@Override
	public void addToAll(Byte2FloatMap m) {
		for(Byte2FloatMap.Entry entry : getFastIterable(m))
			addTo(entry.getByteKey(), entry.getFloatValue());
	}
	
	@Override
	public void putAll(Byte2FloatMap m) {
		for(ObjectIterator<Byte2FloatMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Byte2FloatMap.Entry entry = iter.next();
			put(entry.getByteKey(), entry.getFloatValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Byte, ? extends Float> m)
	{
		if(m instanceof Byte2FloatMap) putAll((Byte2FloatMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(byte[] keys, float[] 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(Byte[] keys, Float[] 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].byteValue(), values[i].floatValue());		
	}
	
	@Override
	public void putAllIfAbsent(Byte2FloatMap m) {
		for(Byte2FloatMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getByteKey(), entry.getFloatValue());
	}
	
	
	@Override
	public boolean containsKey(byte key) {
		for(ByteIterator iter = keySet().iterator();iter.hasNext();)
			if(iter.nextByte() == key) return true;
		return false;
	}
	
	@Override
	public boolean containsValue(float value) {
		for(FloatIterator iter = values().iterator();iter.hasNext();)
			if(Float.floatToIntBits(iter.nextFloat()) == Float.floatToIntBits(value)) return true;
		return false;
	}
	
	@Override
	public boolean replace(byte key, float oldValue, float newValue) {
		float curValue = get(key);
		if (Float.floatToIntBits(curValue) != Float.floatToIntBits(oldValue) || (Float.floatToIntBits(curValue) == Float.floatToIntBits(getDefaultReturnValue()) && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public float replace(byte key, float value) {
		float curValue;
		if (Float.floatToIntBits((curValue = get(key))) != Float.floatToIntBits(getDefaultReturnValue()) || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceFloats(Byte2FloatMap m) {
		for(Byte2FloatMap.Entry entry : getFastIterable(m))
			replace(entry.getByteKey(), entry.getFloatValue());
	}
	
	@Override
	public void replaceFloats(ByteFloatUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Byte2FloatMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Byte2FloatMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsFloat(entry.getByteKey(), entry.getFloatValue()));
		}
	}

	@Override
	public float computeFloat(byte key, ByteFloatUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		float newValue = mappingFunction.applyAsFloat(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public float computeFloatIfAbsent(byte key, Byte2FloatFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			float newValue = mappingFunction.applyAsFloat(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public float supplyFloatIfAbsent(byte key, FloatSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			float newValue = valueProvider.getAsFloat();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public float computeFloatIfPresent(byte key, ByteFloatUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			float newValue = mappingFunction.applyAsFloat(key, get(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public float computeFloatNonDefault(byte key, ByteFloatUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		float value = get(key);
		float newValue = mappingFunction.applyAsFloat(key, value);
		if(Float.floatToIntBits(newValue) == Float.floatToIntBits(getDefaultReturnValue())) {
			if(Float.floatToIntBits(value) != Float.floatToIntBits(getDefaultReturnValue()) || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public float computeFloatIfAbsentNonDefault(byte key, Byte2FloatFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		float value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			float newValue = mappingFunction.applyAsFloat(key);
			if(Float.floatToIntBits(newValue) != Float.floatToIntBits(getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public float supplyFloatIfAbsentNonDefault(byte key, FloatSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		float value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			float newValue = valueProvider.getAsFloat();
			if(Float.floatToIntBits(newValue) != Float.floatToIntBits(getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public float computeFloatIfPresentNonDefault(byte key, ByteFloatUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		float value;
		if(Float.floatToIntBits((value = get(key))) != Float.floatToIntBits(getDefaultReturnValue()) || containsKey(key)) {
			float newValue = mappingFunction.applyAsFloat(key, value);
			if(Float.floatToIntBits(newValue) != Float.floatToIntBits(getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public float mergeFloat(byte key, float value, FloatFloatUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		float oldValue = get(key);
		float newValue = Float.floatToIntBits(oldValue) == Float.floatToIntBits(getDefaultReturnValue()) ? value : mappingFunction.applyAsFloat(oldValue, value);
		if(Float.floatToIntBits(newValue) == Float.floatToIntBits(getDefaultReturnValue())) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllFloat(Byte2FloatMap m, FloatFloatUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Byte2FloatMap.Entry entry : getFastIterable(m)) {
			byte key = entry.getByteKey();
			float oldValue = get(key);
			float newValue = Float.floatToIntBits(oldValue) == Float.floatToIntBits(getDefaultReturnValue()) ? entry.getFloatValue() : mappingFunction.applyAsFloat(oldValue, entry.getFloatValue());
			if(Float.floatToIntBits(newValue) == Float.floatToIntBits(getDefaultReturnValue())) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Float get(Object key) {
		return Float.valueOf(key instanceof Byte ? get(((Byte)key).byteValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Float getOrDefault(Object key, Float defaultValue) {
		return Float.valueOf(key instanceof Byte ? getOrDefault(((Byte)key).byteValue(), defaultValue.floatValue()) : getDefaultReturnValue());
	}
	
	@Override
	public float getOrDefault(byte key, float defaultValue) {
		float value = get(key);
		return Float.floatToIntBits(value) != Float.floatToIntBits(getDefaultReturnValue()) || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Float remove(Object key) {
		return key instanceof Byte ? Float.valueOf(remove(((Byte)key).byteValue())) : Float.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(ByteFloatConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Byte2FloatMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Byte2FloatMap.Entry entry = iter.next();
			action.accept(entry.getByteKey(), entry.getFloatValue());
		}
	}

	@Override
	public ByteSet keySet() {
		return new AbstractByteSet() {
			@Override
			public boolean remove(byte o) {
				return Float.floatToIntBits(AbstractByte2FloatMap.this.remove(o)) != Float.floatToIntBits(getDefaultReturnValue());
			}
			
			@Override
			public boolean add(byte o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public ByteIterator iterator() {
				return new ByteIterator() {
					ObjectIterator<Byte2FloatMap.Entry> iter = getFastIterator(AbstractByte2FloatMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}

					@Override
					public byte nextByte() {
						return iter.next().getByteKey();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
			
			@Override
			public int size() {
				return AbstractByte2FloatMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractByte2FloatMap.this.clear();
			}
		};
	}

	@Override
	public FloatCollection values() {
		return new AbstractFloatCollection() {
			@Override
			public boolean add(float o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractByte2FloatMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractByte2FloatMap.this.clear();
			}
			
			@Override
			public FloatIterator iterator() {
				return new FloatIterator() {
					ObjectIterator<Byte2FloatMap.Entry> iter = getFastIterator(AbstractByte2FloatMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public float nextFloat() {
						return iter.next().getFloatValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Byte, Float>> entrySet() {
		return (ObjectSet)byte2FloatEntrySet();
	}

	@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 Byte2FloatMap) return byte2FloatEntrySet().containsAll(((Byte2FloatMap)o).byte2FloatEntrySet());
			return byte2FloatEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Byte2FloatMap.Entry> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedByte2FloatOrderedMap extends AbstractByte2FloatMap implements Byte2FloatOrderedMap {
		Byte2FloatOrderedMap map;
		
		public ReversedByte2FloatOrderedMap(Byte2FloatOrderedMap map) {
			this.map = map;
		}
		@Override
		public AbstractByte2FloatMap setDefaultReturnValue(float v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public float getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Byte2FloatOrderedMap copy() { throw new UnsupportedOperationException(); }
		@Override
		public float put(byte key, float value) { return map.put(key, value); }
		@Override
		public float putIfAbsent(byte key, float value) { return map.putIfAbsent(key, value); }
		@Override
		public float addTo(byte key, float value) { return map.addTo(key, value); }
		@Override
		public float subFrom(byte key, float value) { return map.subFrom(key, value); }
		@Override
		public float remove(byte key) { return map.remove(key); }
		@Override
		public boolean remove(byte key, float value) { return map.remove(key, value); }
		@Override
		public float removeOrDefault(byte key, float defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(byte key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(float value) { return map.containsValue(value); }
		@Override
		public boolean replace(byte key, float oldValue, float newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public float replace(byte key, float value) { return map.replace(key, value); }
		@Override
		public void replaceFloats(Byte2FloatMap m) { map.replaceFloats(m); }
		@Override
		public void replaceFloats(ByteFloatUnaryOperator mappingFunction) { map.replaceFloats(mappingFunction); }
		@Override
		public float computeFloat(byte key, ByteFloatUnaryOperator mappingFunction) { return map.computeFloat(key, mappingFunction); }
		@Override
		public float computeFloatIfAbsent(byte key, Byte2FloatFunction mappingFunction) { return map.computeFloatIfAbsent(key, mappingFunction); }
		@Override
		public float supplyFloatIfAbsent(byte key, FloatSupplier valueProvider) { return map.supplyFloatIfAbsent(key, valueProvider); }
		@Override
		public float computeFloatIfPresent(byte key, ByteFloatUnaryOperator mappingFunction) { return map.computeFloatIfPresent(key, mappingFunction); }
		@Override
		public float computeFloatNonDefault(byte key, ByteFloatUnaryOperator mappingFunction) { return map.computeFloatNonDefault(key, mappingFunction); }
		@Override
		public float computeFloatIfAbsentNonDefault(byte key, Byte2FloatFunction mappingFunction) { return map.computeFloatIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public float supplyFloatIfAbsentNonDefault(byte key, FloatSupplier valueProvider) { return map.supplyFloatIfAbsentNonDefault(key, valueProvider); }
		@Override
		public float computeFloatIfPresentNonDefault(byte key, ByteFloatUnaryOperator mappingFunction) { return map.computeFloatIfPresentNonDefault(key, mappingFunction); }
		@Override
		public float mergeFloat(byte key, float value, FloatFloatUnaryOperator mappingFunction) { return map.mergeFloat(key, value, mappingFunction); }
		@Override
		public float getOrDefault(byte key, float defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public float get(byte key) { return map.get(key); }
		@Override
		public float putAndMoveToFirst(byte key, float value) { return map.putAndMoveToLast(key, value); }
		@Override
		public float putAndMoveToLast(byte key, float value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public float putFirst(byte key, float value) { return map.putLast(key, value); }
		@Override
		public float putLast(byte key, float value) { return map.putFirst(key, value); }
		@Override
		public boolean moveToFirst(byte key) { return map.moveToLast(key); }
		@Override
		public boolean moveToLast(byte key) { return map.moveToFirst(key); }
		@Override
		public float getAndMoveToFirst(byte key) { return map.getAndMoveToLast(key); }
		@Override
		public float getAndMoveToLast(byte key) { return map.getAndMoveToFirst(key); }
		@Override
		public byte firstByteKey() { return map.lastByteKey(); }
		@Override
		public byte pollFirstByteKey() { return map.pollLastByteKey(); }
		@Override
		public byte lastByteKey() { return map.firstByteKey(); }
		@Override
		public byte pollLastByteKey() { return map.pollFirstByteKey(); }
		@Override
		public float firstFloatValue() { return map.lastFloatValue(); }
		@Override
		public float lastFloatValue() { return map.firstFloatValue(); }
		@Override
		public Byte2FloatMap.Entry firstEntry() { return map.lastEntry(); }
		@Override
		public Byte2FloatMap.Entry lastEntry() { return map.firstEntry(); }
		@Override
		public Byte2FloatMap.Entry pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Byte2FloatMap.Entry pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Byte2FloatMap.Entry> byte2FloatEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.byte2FloatEntrySet()); }
		@Override
		public ByteOrderedSet keySet() { return new AbstractByteSet.ReversedByteOrderedSet(map.keySet()); }
		@Override
		public FloatOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Byte2FloatOrderedMap reversed() { return map; }
	}

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

		@Override
		public float getFloatValue() {
			return value;
		}

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