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

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

import speiger.src.collections.ints.collections.IntIterator;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.function.Int2ByteFunction;
import speiger.src.collections.ints.functions.function.IntByteUnaryOperator;
import speiger.src.collections.ints.maps.interfaces.Int2ByteMap;
import speiger.src.collections.ints.maps.interfaces.Int2ByteOrderedMap;
import speiger.src.collections.ints.sets.IntOrderedSet;
import speiger.src.collections.bytes.collections.ByteOrderedCollection;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
import speiger.src.collections.ints.sets.AbstractIntSet;
import speiger.src.collections.ints.sets.IntSet;
import speiger.src.collections.ints.utils.maps.Int2ByteMaps;
import speiger.src.collections.bytes.collections.AbstractByteCollection;
import speiger.src.collections.bytes.collections.ByteCollection;
import speiger.src.collections.bytes.collections.ByteIterator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.ByteSupplier;
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 AbstractInt2ByteMap extends AbstractMap<Integer, Byte> implements Int2ByteMap
{
	protected byte defaultReturnValue = (byte)-1;
	
	@Override
	public byte getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractInt2ByteMap setDefaultReturnValue(byte v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Int2ByteMap.Entry> getFastIterable(Int2ByteMap map) {
		return Int2ByteMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Int2ByteMap.Entry> getFastIterator(Int2ByteMap map) {
		return Int2ByteMaps.fastIterator(map);
	}
	
	@Override
	public Int2ByteMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Byte put(Integer key, Byte value) {
		return Byte.valueOf(put(key.intValue(), value.byteValue()));
	}
	
	@Override
	public void addToAll(Int2ByteMap m) {
		for(Int2ByteMap.Entry entry : getFastIterable(m))
			addTo(entry.getIntKey(), entry.getByteValue());
	}
	
	@Override
	public void putAll(Int2ByteMap m) {
		for(ObjectIterator<Int2ByteMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Int2ByteMap.Entry entry = iter.next();
			put(entry.getIntKey(), entry.getByteValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Integer, ? extends Byte> m)
	{
		if(m instanceof Int2ByteMap) putAll((Int2ByteMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(int[] keys, byte[] 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(Integer[] keys, Byte[] 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].intValue(), values[i].byteValue());		
	}
	
	@Override
	public void putAllIfAbsent(Int2ByteMap m) {
		for(Int2ByteMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getIntKey(), entry.getByteValue());
	}
	
	
	@Override
	public boolean containsKey(int key) {
		for(IntIterator iter = keySet().iterator();iter.hasNext();)
			if(iter.nextInt() == key) return true;
		return false;
	}
	
	@Override
	public boolean containsValue(byte value) {
		for(ByteIterator iter = values().iterator();iter.hasNext();)
			if(iter.nextByte() == value) return true;
		return false;
	}
	
	@Override
	public boolean replace(int key, byte oldValue, byte newValue) {
		byte curValue = get(key);
		if (curValue != oldValue || (curValue == getDefaultReturnValue() && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public byte replace(int key, byte value) {
		byte curValue;
		if ((curValue = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceBytes(Int2ByteMap m) {
		for(Int2ByteMap.Entry entry : getFastIterable(m))
			replace(entry.getIntKey(), entry.getByteValue());
	}
	
	@Override
	public void replaceBytes(IntByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Int2ByteMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Int2ByteMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsByte(entry.getIntKey(), entry.getByteValue()));
		}
	}

	@Override
	public byte computeByte(int key, IntByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		byte newValue = mappingFunction.applyAsByte(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public byte computeByteIfAbsent(int key, Int2ByteFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			byte newValue = mappingFunction.applyAsByte(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public byte supplyByteIfAbsent(int key, ByteSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			byte newValue = valueProvider.getAsByte();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public byte computeByteIfPresent(int key, IntByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			byte newValue = mappingFunction.applyAsByte(key, get(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public byte computeByteNonDefault(int key, IntByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		byte value = get(key);
		byte newValue = mappingFunction.applyAsByte(key, value);
		if(newValue == getDefaultReturnValue()) {
			if(value != getDefaultReturnValue() || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public byte computeByteIfAbsentNonDefault(int key, Int2ByteFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		byte value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			byte newValue = mappingFunction.applyAsByte(key);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public byte supplyByteIfAbsentNonDefault(int key, ByteSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		byte value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			byte newValue = valueProvider.getAsByte();
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public byte computeByteIfPresentNonDefault(int key, IntByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		byte value;
		if((value = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			byte newValue = mappingFunction.applyAsByte(key, value);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public byte mergeByte(int key, byte value, ByteByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		byte oldValue = get(key);
		byte newValue = oldValue == getDefaultReturnValue() ? value : mappingFunction.applyAsByte(oldValue, value);
		if(newValue == getDefaultReturnValue()) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllByte(Int2ByteMap m, ByteByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Int2ByteMap.Entry entry : getFastIterable(m)) {
			int key = entry.getIntKey();
			byte oldValue = get(key);
			byte newValue = oldValue == getDefaultReturnValue() ? entry.getByteValue() : mappingFunction.applyAsByte(oldValue, entry.getByteValue());
			if(newValue == getDefaultReturnValue()) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Byte get(Object key) {
		return Byte.valueOf(key instanceof Integer ? get(((Integer)key).intValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Byte getOrDefault(Object key, Byte defaultValue) {
		return Byte.valueOf(key instanceof Integer ? getOrDefault(((Integer)key).intValue(), defaultValue.byteValue()) : getDefaultReturnValue());
	}
	
	@Override
	public byte getOrDefault(int key, byte defaultValue) {
		byte value = get(key);
		return value != getDefaultReturnValue() || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Byte remove(Object key) {
		return key instanceof Integer ? Byte.valueOf(remove(((Integer)key).intValue())) : Byte.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(IntByteConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Int2ByteMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Int2ByteMap.Entry entry = iter.next();
			action.accept(entry.getIntKey(), entry.getByteValue());
		}
	}

	@Override
	public IntSet keySet() {
		return new AbstractIntSet() {
			@Override
			public boolean remove(int o) {
				return AbstractInt2ByteMap.this.remove(o) != getDefaultReturnValue();
			}
			
			@Override
			public boolean add(int o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public IntIterator iterator() {
				return new IntIterator() {
					ObjectIterator<Int2ByteMap.Entry> iter = getFastIterator(AbstractInt2ByteMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}

					@Override
					public int nextInt() {
						return iter.next().getIntKey();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
			
			@Override
			public int size() {
				return AbstractInt2ByteMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractInt2ByteMap.this.clear();
			}
		};
	}

	@Override
	public ByteCollection values() {
		return new AbstractByteCollection() {
			@Override
			public boolean add(byte o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractInt2ByteMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractInt2ByteMap.this.clear();
			}
			
			@Override
			public ByteIterator iterator() {
				return new ByteIterator() {
					ObjectIterator<Int2ByteMap.Entry> iter = getFastIterator(AbstractInt2ByteMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public byte nextByte() {
						return iter.next().getByteValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Integer, Byte>> entrySet() {
		return (ObjectSet)int2ByteEntrySet();
	}

	@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 Int2ByteMap) return int2ByteEntrySet().containsAll(((Int2ByteMap)o).int2ByteEntrySet());
			return int2ByteEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Int2ByteMap.Entry> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedInt2ByteOrderedMap extends AbstractInt2ByteMap implements Int2ByteOrderedMap {
		Int2ByteOrderedMap map;
		
		public ReversedInt2ByteOrderedMap(Int2ByteOrderedMap map) {
			this.map = map;
		}
		@Override
		public AbstractInt2ByteMap setDefaultReturnValue(byte v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public byte getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Int2ByteOrderedMap copy() { throw new UnsupportedOperationException(); }
		@Override
		public byte put(int key, byte value) { return map.put(key, value); }
		@Override
		public byte putIfAbsent(int key, byte value) { return map.putIfAbsent(key, value); }
		@Override
		public byte addTo(int key, byte value) { return map.addTo(key, value); }
		@Override
		public byte subFrom(int key, byte value) { return map.subFrom(key, value); }
		@Override
		public byte remove(int key) { return map.remove(key); }
		@Override
		public boolean remove(int key, byte value) { return map.remove(key, value); }
		@Override
		public byte removeOrDefault(int key, byte defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(int key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(byte value) { return map.containsValue(value); }
		@Override
		public boolean replace(int key, byte oldValue, byte newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public byte replace(int key, byte value) { return map.replace(key, value); }
		@Override
		public void replaceBytes(Int2ByteMap m) { map.replaceBytes(m); }
		@Override
		public void replaceBytes(IntByteUnaryOperator mappingFunction) { map.replaceBytes(mappingFunction); }
		@Override
		public byte computeByte(int key, IntByteUnaryOperator mappingFunction) { return map.computeByte(key, mappingFunction); }
		@Override
		public byte computeByteIfAbsent(int key, Int2ByteFunction mappingFunction) { return map.computeByteIfAbsent(key, mappingFunction); }
		@Override
		public byte supplyByteIfAbsent(int key, ByteSupplier valueProvider) { return map.supplyByteIfAbsent(key, valueProvider); }
		@Override
		public byte computeByteIfPresent(int key, IntByteUnaryOperator mappingFunction) { return map.computeByteIfPresent(key, mappingFunction); }
		@Override
		public byte computeByteNonDefault(int key, IntByteUnaryOperator mappingFunction) { return map.computeByteNonDefault(key, mappingFunction); }
		@Override
		public byte computeByteIfAbsentNonDefault(int key, Int2ByteFunction mappingFunction) { return map.computeByteIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public byte supplyByteIfAbsentNonDefault(int key, ByteSupplier valueProvider) { return map.supplyByteIfAbsentNonDefault(key, valueProvider); }
		@Override
		public byte computeByteIfPresentNonDefault(int key, IntByteUnaryOperator mappingFunction) { return map.computeByteIfPresentNonDefault(key, mappingFunction); }
		@Override
		public byte mergeByte(int key, byte value, ByteByteUnaryOperator mappingFunction) { return map.mergeByte(key, value, mappingFunction); }
		@Override
		public byte getOrDefault(int key, byte defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public byte get(int key) { return map.get(key); }
		@Override
		public byte putAndMoveToFirst(int key, byte value) { return map.putAndMoveToLast(key, value); }
		@Override
		public byte putAndMoveToLast(int key, byte value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public byte putFirst(int key, byte value) { return map.putLast(key, value); }
		@Override
		public byte putLast(int key, byte value) { return map.putFirst(key, value); }
		@Override
		public boolean moveToFirst(int key) { return map.moveToLast(key); }
		@Override
		public boolean moveToLast(int key) { return map.moveToFirst(key); }
		@Override
		public byte getAndMoveToFirst(int key) { return map.getAndMoveToLast(key); }
		@Override
		public byte getAndMoveToLast(int key) { return map.getAndMoveToFirst(key); }
		@Override
		public int firstIntKey() { return map.lastIntKey(); }
		@Override
		public int pollFirstIntKey() { return map.pollLastIntKey(); }
		@Override
		public int lastIntKey() { return map.firstIntKey(); }
		@Override
		public int pollLastIntKey() { return map.pollFirstIntKey(); }
		@Override
		public byte firstByteValue() { return map.lastByteValue(); }
		@Override
		public byte lastByteValue() { return map.firstByteValue(); }
		@Override
		public Int2ByteMap.Entry firstEntry() { return map.lastEntry(); }
		@Override
		public Int2ByteMap.Entry lastEntry() { return map.firstEntry(); }
		@Override
		public Int2ByteMap.Entry pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Int2ByteMap.Entry pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Int2ByteMap.Entry> int2ByteEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.int2ByteEntrySet()); }
		@Override
		public IntOrderedSet keySet() { return new AbstractIntSet.ReversedIntOrderedSet(map.keySet()); }
		@Override
		public ByteOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Int2ByteOrderedMap reversed() { return map; }
	}

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

		@Override
		public byte getByteValue() {
			return value;
		}

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