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.ByteBooleanConsumer;
import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator;
import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanOrderedMap;
import speiger.src.collections.bytes.sets.ByteOrderedSet;
import speiger.src.collections.booleans.collections.BooleanOrderedCollection;
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.Byte2BooleanMaps;
import speiger.src.collections.booleans.collections.AbstractBooleanCollection;
import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
import speiger.src.collections.booleans.functions.BooleanSupplier;
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 AbstractByte2BooleanMap extends AbstractMap<Byte, Boolean> implements Byte2BooleanMap
{
	protected boolean defaultReturnValue = false;
	
	@Override
	public boolean getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractByte2BooleanMap setDefaultReturnValue(boolean v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Byte2BooleanMap.Entry> getFastIterable(Byte2BooleanMap map) {
		return Byte2BooleanMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Byte2BooleanMap.Entry> getFastIterator(Byte2BooleanMap map) {
		return Byte2BooleanMaps.fastIterator(map);
	}
	
	@Override
	public Byte2BooleanMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Boolean put(Byte key, Boolean value) {
		return Boolean.valueOf(put(key.byteValue(), value.booleanValue()));
	}
	
	@Override
	public void putAll(Byte2BooleanMap m) {
		for(ObjectIterator<Byte2BooleanMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Byte2BooleanMap.Entry entry = iter.next();
			put(entry.getByteKey(), entry.getBooleanValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Byte, ? extends Boolean> m)
	{
		if(m instanceof Byte2BooleanMap) putAll((Byte2BooleanMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(byte[] keys, boolean[] 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, Boolean[] 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].booleanValue());		
	}
	
	@Override
	public void putAllIfAbsent(Byte2BooleanMap m) {
		for(Byte2BooleanMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getByteKey(), entry.getBooleanValue());
	}
	
	
	@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(boolean value) {
		for(BooleanIterator iter = values().iterator();iter.hasNext();)
			if(iter.nextBoolean() == value) return true;
		return false;
	}
	
	@Override
	public boolean replace(byte key, boolean oldValue, boolean newValue) {
		boolean curValue = get(key);
		if (curValue != oldValue || (curValue == getDefaultReturnValue() && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public boolean replace(byte key, boolean value) {
		boolean curValue;
		if ((curValue = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceBooleans(Byte2BooleanMap m) {
		for(Byte2BooleanMap.Entry entry : getFastIterable(m))
			replace(entry.getByteKey(), entry.getBooleanValue());
	}
	
	@Override
	public void replaceBooleans(ByteBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Byte2BooleanMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Byte2BooleanMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsBoolean(entry.getByteKey(), entry.getBooleanValue()));
		}
	}

	@Override
	public boolean computeBoolean(byte key, ByteBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean newValue = mappingFunction.applyAsBoolean(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public boolean computeBooleanIfAbsent(byte key, BytePredicate mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			boolean newValue = mappingFunction.test(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public boolean supplyBooleanIfAbsent(byte key, BooleanSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			boolean newValue = valueProvider.getAsBoolean();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public boolean computeBooleanIfPresent(byte key, ByteBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			boolean newValue = mappingFunction.applyAsBoolean(key, get(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public boolean computeBooleanNonDefault(byte key, ByteBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean value = get(key);
		boolean newValue = mappingFunction.applyAsBoolean(key, value);
		if(newValue == getDefaultReturnValue()) {
			if(value != getDefaultReturnValue() || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public boolean computeBooleanIfAbsentNonDefault(byte key, BytePredicate mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			boolean newValue = mappingFunction.test(key);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public boolean supplyBooleanIfAbsentNonDefault(byte key, BooleanSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		boolean value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			boolean newValue = valueProvider.getAsBoolean();
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public boolean computeBooleanIfPresentNonDefault(byte key, ByteBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean value;
		if((value = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			boolean newValue = mappingFunction.applyAsBoolean(key, value);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public boolean mergeBoolean(byte key, boolean value, BooleanBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		boolean oldValue = get(key);
		boolean newValue = oldValue == getDefaultReturnValue() ? value : mappingFunction.applyAsBoolean(oldValue, value);
		if(newValue == getDefaultReturnValue()) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllBoolean(Byte2BooleanMap m, BooleanBooleanUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Byte2BooleanMap.Entry entry : getFastIterable(m)) {
			byte key = entry.getByteKey();
			boolean oldValue = get(key);
			boolean newValue = oldValue == getDefaultReturnValue() ? entry.getBooleanValue() : mappingFunction.applyAsBoolean(oldValue, entry.getBooleanValue());
			if(newValue == getDefaultReturnValue()) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Boolean get(Object key) {
		return Boolean.valueOf(key instanceof Byte ? get(((Byte)key).byteValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Boolean getOrDefault(Object key, Boolean defaultValue) {
		return Boolean.valueOf(key instanceof Byte ? getOrDefault(((Byte)key).byteValue(), defaultValue.booleanValue()) : getDefaultReturnValue());
	}
	
	@Override
	public boolean getOrDefault(byte key, boolean defaultValue) {
		boolean value = get(key);
		return value != getDefaultReturnValue() || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Boolean remove(Object key) {
		return key instanceof Byte ? Boolean.valueOf(remove(((Byte)key).byteValue())) : Boolean.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(ByteBooleanConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Byte2BooleanMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Byte2BooleanMap.Entry entry = iter.next();
			action.accept(entry.getByteKey(), entry.getBooleanValue());
		}
	}

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

	@Override
	public BooleanCollection values() {
		return new AbstractBooleanCollection() {
			@Override
			public boolean add(boolean o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractByte2BooleanMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractByte2BooleanMap.this.clear();
			}
			
			@Override
			public BooleanIterator iterator() {
				return new BooleanIterator() {
					ObjectIterator<Byte2BooleanMap.Entry> iter = getFastIterator(AbstractByte2BooleanMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public boolean nextBoolean() {
						return iter.next().getBooleanValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Byte, Boolean>> entrySet() {
		return (ObjectSet)byte2BooleanEntrySet();
	}

	@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 Byte2BooleanMap) return byte2BooleanEntrySet().containsAll(((Byte2BooleanMap)o).byte2BooleanEntrySet());
			return byte2BooleanEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Byte2BooleanMap.Entry> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedByte2BooleanOrderedMap extends AbstractByte2BooleanMap implements Byte2BooleanOrderedMap {
		Byte2BooleanOrderedMap map;
		
		public ReversedByte2BooleanOrderedMap(Byte2BooleanOrderedMap map) {
			this.map = map;
		}
		@Override
		public AbstractByte2BooleanMap setDefaultReturnValue(boolean v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public boolean getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Byte2BooleanOrderedMap copy() { throw new UnsupportedOperationException(); }
		@Override
		public boolean put(byte key, boolean value) { return map.put(key, value); }
		@Override
		public boolean putIfAbsent(byte key, boolean value) { return map.putIfAbsent(key, value); }
		@Override
		public boolean remove(byte key) { return map.remove(key); }
		@Override
		public boolean remove(byte key, boolean value) { return map.remove(key, value); }
		@Override
		public boolean removeOrDefault(byte key, boolean defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(byte key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(boolean value) { return map.containsValue(value); }
		@Override
		public boolean replace(byte key, boolean oldValue, boolean newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public boolean replace(byte key, boolean value) { return map.replace(key, value); }
		@Override
		public void replaceBooleans(Byte2BooleanMap m) { map.replaceBooleans(m); }
		@Override
		public void replaceBooleans(ByteBooleanUnaryOperator mappingFunction) { map.replaceBooleans(mappingFunction); }
		@Override
		public boolean computeBoolean(byte key, ByteBooleanUnaryOperator mappingFunction) { return map.computeBoolean(key, mappingFunction); }
		@Override
		public boolean computeBooleanIfAbsent(byte key, BytePredicate mappingFunction) { return map.computeBooleanIfAbsent(key, mappingFunction); }
		@Override
		public boolean supplyBooleanIfAbsent(byte key, BooleanSupplier valueProvider) { return map.supplyBooleanIfAbsent(key, valueProvider); }
		@Override
		public boolean computeBooleanIfPresent(byte key, ByteBooleanUnaryOperator mappingFunction) { return map.computeBooleanIfPresent(key, mappingFunction); }
		@Override
		public boolean computeBooleanNonDefault(byte key, ByteBooleanUnaryOperator mappingFunction) { return map.computeBooleanNonDefault(key, mappingFunction); }
		@Override
		public boolean computeBooleanIfAbsentNonDefault(byte key, BytePredicate mappingFunction) { return map.computeBooleanIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public boolean supplyBooleanIfAbsentNonDefault(byte key, BooleanSupplier valueProvider) { return map.supplyBooleanIfAbsentNonDefault(key, valueProvider); }
		@Override
		public boolean computeBooleanIfPresentNonDefault(byte key, ByteBooleanUnaryOperator mappingFunction) { return map.computeBooleanIfPresentNonDefault(key, mappingFunction); }
		@Override
		public boolean mergeBoolean(byte key, boolean value, BooleanBooleanUnaryOperator mappingFunction) { return map.mergeBoolean(key, value, mappingFunction); }
		@Override
		public boolean getOrDefault(byte key, boolean defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public boolean get(byte key) { return map.get(key); }
		@Override
		public boolean putAndMoveToFirst(byte key, boolean value) { return map.putAndMoveToLast(key, value); }
		@Override
		public boolean putAndMoveToLast(byte key, boolean value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public boolean putFirst(byte key, boolean value) { return map.putLast(key, value); }
		@Override
		public boolean putLast(byte key, boolean 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 boolean getAndMoveToFirst(byte key) { return map.getAndMoveToLast(key); }
		@Override
		public boolean 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 boolean firstBooleanValue() { return map.lastBooleanValue(); }
		@Override
		public boolean lastBooleanValue() { return map.firstBooleanValue(); }
		@Override
		public Byte2BooleanMap.Entry firstEntry() { return map.lastEntry(); }
		@Override
		public Byte2BooleanMap.Entry lastEntry() { return map.firstEntry(); }
		@Override
		public Byte2BooleanMap.Entry pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Byte2BooleanMap.Entry pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Byte2BooleanMap.Entry> byte2BooleanEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.byte2BooleanEntrySet()); }
		@Override
		public ByteOrderedSet keySet() { return new AbstractByteSet.ReversedByteOrderedSet(map.keySet()); }
		@Override
		public BooleanOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Byte2BooleanOrderedMap reversed() { return map; }
	}

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

		@Override
		public boolean getBooleanValue() {
			return value;
		}

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