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.ByteObjectConsumer;
import speiger.src.collections.bytes.functions.function.ByteFunction;
import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator;
import speiger.src.collections.bytes.maps.interfaces.Byte2ObjectMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2ObjectOrderedMap;
import speiger.src.collections.bytes.sets.ByteOrderedSet;
import speiger.src.collections.objects.collections.ObjectOrderedCollection;
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.Byte2ObjectMaps;
import speiger.src.collections.objects.collections.AbstractObjectCollection;
import speiger.src.collections.objects.collections.ObjectCollection;
import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
import speiger.src.collections.objects.functions.ObjectSupplier;
import speiger.src.collections.objects.collections.ObjectIterable;
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
 * @param <V> the keyType of elements maintained by this Collection
 */
public abstract class AbstractByte2ObjectMap<V> extends AbstractMap<Byte, V> implements Byte2ObjectMap<V>
{
	protected V defaultReturnValue = null;
	
	@Override
	public V getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractByte2ObjectMap<V> setDefaultReturnValue(V v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Byte2ObjectMap.Entry<V>> getFastIterable(Byte2ObjectMap<V> map) {
		return Byte2ObjectMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Byte2ObjectMap.Entry<V>> getFastIterator(Byte2ObjectMap<V> map) {
		return Byte2ObjectMaps.fastIterator(map);
	}
	
	@Override
	public Byte2ObjectMap<V> copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public V put(Byte key, V value) {
		return put(key.byteValue(), value);
	}
	
	@Override
	public void putAll(Byte2ObjectMap<V> m) {
		for(ObjectIterator<Byte2ObjectMap.Entry<V>> iter = getFastIterator(m);iter.hasNext();) {
			Byte2ObjectMap.Entry<V> entry = iter.next();
			put(entry.getByteKey(), entry.getValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Byte, ? extends V> m)
	{
		if(m instanceof Byte2ObjectMap) putAll((Byte2ObjectMap<V>)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(byte[] keys, V[] 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, V[] 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]);		
	}
	
	@Override
	public void putAllIfAbsent(Byte2ObjectMap<V> m) {
		for(Byte2ObjectMap.Entry<V> entry : getFastIterable(m))
			putIfAbsent(entry.getByteKey(), entry.getValue());
	}
	
	
	@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(Object value) {
		for(ObjectIterator<V> iter = values().iterator();iter.hasNext();)
			if(Objects.equals(value, iter.next())) return true;
		return false;
	}
	
	@Override
	public boolean replace(byte key, V oldValue, V newValue) {
		V curValue = get(key);
		if (!Objects.equals(curValue, oldValue) || (Objects.equals(curValue, getDefaultReturnValue()) && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public V replace(byte key, V value) {
		V curValue;
		if (!Objects.equals((curValue = get(key)), getDefaultReturnValue()) || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceObjects(Byte2ObjectMap<V> m) {
		for(Byte2ObjectMap.Entry<V> entry : getFastIterable(m))
			replace(entry.getByteKey(), entry.getValue());
	}
	
	@Override
	public void replaceObjects(ByteObjectUnaryOperator<V> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Byte2ObjectMap.Entry<V>> iter = getFastIterator(this);iter.hasNext();) {
			Byte2ObjectMap.Entry<V> entry = iter.next();
			entry.setValue(mappingFunction.apply(entry.getByteKey(), entry.getValue()));
		}
	}

	@Override
	public V compute(byte key, ByteObjectUnaryOperator<V> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		V value = get(key);
		V newValue = mappingFunction.apply(key, value);
		if(Objects.equals(newValue, getDefaultReturnValue())) {
			if(!Objects.equals(value, getDefaultReturnValue()) || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public V computeIfAbsent(byte key, ByteFunction<V> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		V value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			V newValue = mappingFunction.apply(key);
			if(!Objects.equals(newValue, getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public V supplyIfAbsent(byte key, ObjectSupplier<V> valueProvider) {
		Objects.requireNonNull(valueProvider);
		V value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			V newValue = valueProvider.get();
			if(!Objects.equals(newValue, getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public V computeIfPresent(byte key, ByteObjectUnaryOperator<V> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		V value;
		if(!Objects.equals((value = get(key)), getDefaultReturnValue()) || containsKey(key)) {
			V newValue = mappingFunction.apply(key, value);
			if(!Objects.equals(newValue, getDefaultReturnValue())) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public V merge(byte key, V value, ObjectObjectUnaryOperator<V, V> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		V oldValue = get(key);
		V newValue = Objects.equals(oldValue, getDefaultReturnValue()) ? value : mappingFunction.apply(oldValue, value);
		if(Objects.equals(newValue, getDefaultReturnValue())) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAll(Byte2ObjectMap<V> m, ObjectObjectUnaryOperator<V, V> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Byte2ObjectMap.Entry<V> entry : getFastIterable(m)) {
			byte key = entry.getByteKey();
			V oldValue = get(key);
			V newValue = Objects.equals(oldValue, getDefaultReturnValue()) ? entry.getValue() : mappingFunction.apply(oldValue, entry.getValue());
			if(Objects.equals(newValue, getDefaultReturnValue())) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public V get(Object key) {
		return key instanceof Byte ? get(((Byte)key).byteValue()) : getDefaultReturnValue();
	}
	
	@Override
	public V getOrDefault(Object key, V defaultValue) {
		return key instanceof Byte ? getOrDefault(((Byte)key).byteValue(), defaultValue) : getDefaultReturnValue();
	}
	
	@Override
	public V getOrDefault(byte key, V defaultValue) {
		V value = get(key);
		return !Objects.equals(value, getDefaultReturnValue()) || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public V remove(Object key) {
		return key instanceof Byte ? remove(((Byte)key).byteValue()) : getDefaultReturnValue();
	}
	
	@Override
	public void forEach(ByteObjectConsumer<V> action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Byte2ObjectMap.Entry<V>> iter = getFastIterator(this);iter.hasNext();) {
			Byte2ObjectMap.Entry<V> entry = iter.next();
			action.accept(entry.getByteKey(), entry.getValue());
		}
	}

	@Override
	public ByteSet keySet() {
		return new AbstractByteSet() {
			@Override
			public boolean remove(byte o) {
				return !Objects.equals(AbstractByte2ObjectMap.this.remove(o), getDefaultReturnValue());
			}
			
			@Override
			public boolean add(byte o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public ByteIterator iterator() {
				return new ByteIterator() {
					ObjectIterator<Byte2ObjectMap.Entry<V>> iter = getFastIterator(AbstractByte2ObjectMap.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 AbstractByte2ObjectMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractByte2ObjectMap.this.clear();
			}
		};
	}

	@Override
	public ObjectCollection<V> values() {
		return new AbstractObjectCollection<V>() {
			@Override
			public boolean add(V o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractByte2ObjectMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractByte2ObjectMap.this.clear();
			}
			
			@Override
			public ObjectIterator<V> iterator() {
				return new ObjectIterator<V>() {
					ObjectIterator<Byte2ObjectMap.Entry<V>> iter = getFastIterator(AbstractByte2ObjectMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public V next() {
						return iter.next().getValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Byte, V>> entrySet() {
		return (ObjectSet)byte2ObjectEntrySet();
	}

	@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 Byte2ObjectMap) return byte2ObjectEntrySet().containsAll(((Byte2ObjectMap<V>)o).byte2ObjectEntrySet());
			return byte2ObjectEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Byte2ObjectMap.Entry<V>> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedByte2ObjectOrderedMap<V> extends AbstractByte2ObjectMap<V> implements Byte2ObjectOrderedMap<V> {
		Byte2ObjectOrderedMap<V> map;
		
		public ReversedByte2ObjectOrderedMap(Byte2ObjectOrderedMap<V> map) {
			this.map = map;
		}
		@Override
		public AbstractByte2ObjectMap<V> setDefaultReturnValue(V v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public V getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Byte2ObjectOrderedMap<V> copy() { throw new UnsupportedOperationException(); }
		@Override
		public V put(byte key, V value) { return map.put(key, value); }
		@Override
		public V putIfAbsent(byte key, V value) { return map.putIfAbsent(key, value); }
		@Override
		public V remove(byte key) { return map.remove(key); }
		@Override
		public boolean remove(byte key, V value) { return map.remove(key, value); }
		@Override
		public V removeOrDefault(byte key, V defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(byte key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(Object value) { return map.containsValue(value); }
		@Override
		public boolean replace(byte key, V oldValue, V newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public V replace(byte key, V value) { return map.replace(key, value); }
		@Override
		public void replaceObjects(Byte2ObjectMap<V> m) { map.replaceObjects(m); }
		@Override
		public void replaceObjects(ByteObjectUnaryOperator<V> mappingFunction) { map.replaceObjects(mappingFunction); }
		@Override
		public V compute(byte key, ByteObjectUnaryOperator<V> mappingFunction) { return map.compute(key, mappingFunction); }
		@Override
		public V computeIfAbsent(byte key, ByteFunction<V> mappingFunction) { return map.computeIfAbsent(key, mappingFunction); }
		@Override
		public V supplyIfAbsent(byte key, ObjectSupplier<V> valueProvider) { return map.supplyIfAbsent(key, valueProvider); }
		@Override
		public V computeIfPresent(byte key, ByteObjectUnaryOperator<V> mappingFunction) { return map.computeIfPresent(key, mappingFunction); }
		@Override
		public V merge(byte key, V value, ObjectObjectUnaryOperator<V, V> mappingFunction) { return map.merge(key, value, mappingFunction); }
		@Override
		public V getOrDefault(byte key, V defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public V get(byte key) { return map.get(key); }
		@Override
		public V putAndMoveToFirst(byte key, V value) { return map.putAndMoveToLast(key, value); }
		@Override
		public V putAndMoveToLast(byte key, V value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public V putFirst(byte key, V value) { return map.putLast(key, value); }
		@Override
		public V putLast(byte key, V 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 V getAndMoveToFirst(byte key) { return map.getAndMoveToLast(key); }
		@Override
		public V 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 V firstValue() { return map.lastValue(); }
		@Override
		public V lastValue() { return map.firstValue(); }
		@Override
		public Byte2ObjectMap.Entry<V> firstEntry() { return map.lastEntry(); }
		@Override
		public Byte2ObjectMap.Entry<V> lastEntry() { return map.firstEntry(); }
		@Override
		public Byte2ObjectMap.Entry<V> pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Byte2ObjectMap.Entry<V> pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Byte2ObjectMap.Entry<V>> byte2ObjectEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.byte2ObjectEntrySet()); }
		@Override
		public ByteOrderedSet keySet() { return new AbstractByteSet.ReversedByteOrderedSet(map.keySet()); }
		@Override
		public ObjectOrderedCollection<V> values() { return map.values().reversed(); }
		@Override
		public Byte2ObjectOrderedMap<V> reversed() { return map; }
	}

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

		@Override
		public V getValue() {
			return value;
		}

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