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.ByteByteConsumer;
import speiger.src.collections.bytes.functions.function.ByteUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.maps.interfaces.Byte2ByteMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2ByteOrderedMap;
import speiger.src.collections.bytes.sets.ByteOrderedSet;
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.bytes.sets.AbstractByteSet;
import speiger.src.collections.bytes.sets.ByteSet;
import speiger.src.collections.bytes.utils.maps.Byte2ByteMaps;
import speiger.src.collections.bytes.collections.AbstractByteCollection;
import speiger.src.collections.bytes.collections.ByteCollection;
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 AbstractByte2ByteMap extends AbstractMap<Byte, Byte> implements Byte2ByteMap
{
	protected byte defaultReturnValue = (byte)-1;
	
	@Override
	public byte getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractByte2ByteMap setDefaultReturnValue(byte v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Byte2ByteMap.Entry> getFastIterable(Byte2ByteMap map) {
		return Byte2ByteMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Byte2ByteMap.Entry> getFastIterator(Byte2ByteMap map) {
		return Byte2ByteMaps.fastIterator(map);
	}
	
	@Override
	public Byte2ByteMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Byte put(Byte key, Byte value) {
		return Byte.valueOf(put(key.byteValue(), value.byteValue()));
	}
	
	@Override
	public void addToAll(Byte2ByteMap m) {
		for(Byte2ByteMap.Entry entry : getFastIterable(m))
			addTo(entry.getByteKey(), entry.getByteValue());
	}
	
	@Override
	public void putAll(Byte2ByteMap m) {
		for(ObjectIterator<Byte2ByteMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Byte2ByteMap.Entry entry = iter.next();
			put(entry.getByteKey(), entry.getByteValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Byte, ? extends Byte> m)
	{
		if(m instanceof Byte2ByteMap) putAll((Byte2ByteMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(byte[] 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(Byte[] 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].byteValue(), values[i].byteValue());		
	}
	
	@Override
	public void putAllIfAbsent(Byte2ByteMap m) {
		for(Byte2ByteMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getByteKey(), entry.getByteValue());
	}
	
	
	@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(byte value) {
		for(ByteIterator iter = values().iterator();iter.hasNext();)
			if(iter.nextByte() == value) return true;
		return false;
	}
	
	@Override
	public boolean replace(byte 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(byte key, byte value) {
		byte curValue;
		if ((curValue = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceBytes(Byte2ByteMap m) {
		for(Byte2ByteMap.Entry entry : getFastIterable(m))
			replace(entry.getByteKey(), entry.getByteValue());
	}
	
	@Override
	public void replaceBytes(ByteByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Byte2ByteMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Byte2ByteMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsByte(entry.getByteKey(), entry.getByteValue()));
		}
	}

	@Override
	public byte computeByte(byte key, ByteByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		byte newValue = mappingFunction.applyAsByte(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public byte computeByteIfAbsent(byte key, ByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			byte newValue = mappingFunction.applyAsByte(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public byte supplyByteIfAbsent(byte 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(byte key, ByteByteUnaryOperator 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(byte key, ByteByteUnaryOperator 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(byte key, ByteUnaryOperator 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(byte 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(byte key, ByteByteUnaryOperator 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(byte 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(Byte2ByteMap m, ByteByteUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Byte2ByteMap.Entry entry : getFastIterable(m)) {
			byte key = entry.getByteKey();
			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 Byte ? get(((Byte)key).byteValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Byte getOrDefault(Object key, Byte defaultValue) {
		return Byte.valueOf(key instanceof Byte ? getOrDefault(((Byte)key).byteValue(), defaultValue.byteValue()) : getDefaultReturnValue());
	}
	
	@Override
	public byte getOrDefault(byte key, byte defaultValue) {
		byte value = get(key);
		return value != getDefaultReturnValue() || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Byte remove(Object key) {
		return key instanceof Byte ? Byte.valueOf(remove(((Byte)key).byteValue())) : Byte.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(ByteByteConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Byte2ByteMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Byte2ByteMap.Entry entry = iter.next();
			action.accept(entry.getByteKey(), entry.getByteValue());
		}
	}

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

	@Override
	public ByteCollection values() {
		return new AbstractByteCollection() {
			@Override
			public boolean add(byte o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractByte2ByteMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractByte2ByteMap.this.clear();
			}
			
			@Override
			public ByteIterator iterator() {
				return new ByteIterator() {
					ObjectIterator<Byte2ByteMap.Entry> iter = getFastIterator(AbstractByte2ByteMap.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<Byte, Byte>> entrySet() {
		return (ObjectSet)byte2ByteEntrySet();
	}

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

	
	/**
	 * A Simple Type Specific Entry class to reduce boxing/unboxing
	 */
	public static class BasicEntry implements Byte2ByteMap.Entry {
		protected byte 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(Byte key, Byte value) {
			this.key = key.byteValue();
			this.value = value.byteValue();
		}
		
		/**
		 * A Type Specific Constructor
		 * @param key the key of a entry
		 * @param value the value of a entry 
		 */
		public BasicEntry(byte 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(byte key, byte value) {
			this.key = key;
			this.value = value;
		}
		
		@Override
		public byte getByteKey() {
			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 Byte2ByteMap.Entry) {
					Byte2ByteMap.Entry entry = (Byte2ByteMap.Entry)obj;
					return key == entry.getByteKey() && value == entry.getByteValue();
				}
				Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
				Object key = entry.getKey();
				Object value = entry.getValue();
				return key instanceof Byte && value instanceof Byte && this.key == ((Byte)key).byteValue() && this.value == ((Byte)value).byteValue();
			}
			return false;
		}
		
		@Override
		public int hashCode() {
			return Byte.hashCode(key) ^ Byte.hashCode(value);
		}
		
		@Override
		public String toString() {
			return Byte.toString(key) + "=" + Byte.toString(value);
		}
	}
}