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

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

import speiger.src.collections.longs.collections.LongIterator;
import speiger.src.collections.longs.functions.consumer.LongCharConsumer;
import speiger.src.collections.longs.functions.function.Long2CharFunction;
import speiger.src.collections.longs.functions.function.LongCharUnaryOperator;
import speiger.src.collections.longs.maps.interfaces.Long2CharMap;
import speiger.src.collections.longs.maps.interfaces.Long2CharOrderedMap;
import speiger.src.collections.longs.sets.LongOrderedSet;
import speiger.src.collections.chars.collections.CharOrderedCollection;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
import speiger.src.collections.longs.sets.AbstractLongSet;
import speiger.src.collections.longs.sets.LongSet;
import speiger.src.collections.longs.utils.maps.Long2CharMaps;
import speiger.src.collections.chars.collections.AbstractCharCollection;
import speiger.src.collections.chars.collections.CharCollection;
import speiger.src.collections.chars.collections.CharIterator;
import speiger.src.collections.chars.functions.function.CharCharUnaryOperator;
import speiger.src.collections.chars.functions.CharSupplier;
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 AbstractLong2CharMap extends AbstractMap<Long, Character> implements Long2CharMap
{
	protected char defaultReturnValue = (char)-1;
	
	@Override
	public char getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractLong2CharMap setDefaultReturnValue(char v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Long2CharMap.Entry> getFastIterable(Long2CharMap map) {
		return Long2CharMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Long2CharMap.Entry> getFastIterator(Long2CharMap map) {
		return Long2CharMaps.fastIterator(map);
	}
	
	@Override
	public Long2CharMap copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Character put(Long key, Character value) {
		return Character.valueOf(put(key.longValue(), value.charValue()));
	}
	
	@Override
	public void addToAll(Long2CharMap m) {
		for(Long2CharMap.Entry entry : getFastIterable(m))
			addTo(entry.getLongKey(), entry.getCharValue());
	}
	
	@Override
	public void putAll(Long2CharMap m) {
		for(ObjectIterator<Long2CharMap.Entry> iter = getFastIterator(m);iter.hasNext();) {
			Long2CharMap.Entry entry = iter.next();
			put(entry.getLongKey(), entry.getCharValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends Long, ? extends Character> m)
	{
		if(m instanceof Long2CharMap) putAll((Long2CharMap)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(long[] keys, char[] 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(Long[] keys, Character[] 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].longValue(), values[i].charValue());		
	}
	
	@Override
	public void putAllIfAbsent(Long2CharMap m) {
		for(Long2CharMap.Entry entry : getFastIterable(m))
			putIfAbsent(entry.getLongKey(), entry.getCharValue());
	}
	
	
	@Override
	public boolean containsKey(long key) {
		for(LongIterator iter = keySet().iterator();iter.hasNext();)
			if(iter.nextLong() == key) return true;
		return false;
	}
	
	@Override
	public boolean containsValue(char value) {
		for(CharIterator iter = values().iterator();iter.hasNext();)
			if(iter.nextChar() == value) return true;
		return false;
	}
	
	@Override
	public boolean replace(long key, char oldValue, char newValue) {
		char curValue = get(key);
		if (curValue != oldValue || (curValue == getDefaultReturnValue() && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public char replace(long key, char value) {
		char curValue;
		if ((curValue = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceChars(Long2CharMap m) {
		for(Long2CharMap.Entry entry : getFastIterable(m))
			replace(entry.getLongKey(), entry.getCharValue());
	}
	
	@Override
	public void replaceChars(LongCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Long2CharMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Long2CharMap.Entry entry = iter.next();
			entry.setValue(mappingFunction.applyAsChar(entry.getLongKey(), entry.getCharValue()));
		}
	}

	@Override
	public char computeChar(long key, LongCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char newValue = mappingFunction.applyAsChar(key, get(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public char computeCharIfAbsent(long key, Long2CharFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			char newValue = mappingFunction.applyAsChar(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public char supplyCharIfAbsent(long key, CharSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			char newValue = valueProvider.getAsChar();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public char computeCharIfPresent(long key, LongCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			char newValue = mappingFunction.applyAsChar(key, get(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public char computeCharNonDefault(long key, LongCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char value = get(key);
		char newValue = mappingFunction.applyAsChar(key, value);
		if(newValue == getDefaultReturnValue()) {
			if(value != getDefaultReturnValue() || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public char computeCharIfAbsentNonDefault(long key, Long2CharFunction mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			char newValue = mappingFunction.applyAsChar(key);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public char supplyCharIfAbsentNonDefault(long key, CharSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		char value;
		if((value = get(key)) == getDefaultReturnValue() || !containsKey(key)) {
			char newValue = valueProvider.getAsChar();
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public char computeCharIfPresentNonDefault(long key, LongCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char value;
		if((value = get(key)) != getDefaultReturnValue() || containsKey(key)) {
			char newValue = mappingFunction.applyAsChar(key, value);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public char mergeChar(long key, char value, CharCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		char oldValue = get(key);
		char newValue = oldValue == getDefaultReturnValue() ? value : mappingFunction.applyAsChar(oldValue, value);
		if(newValue == getDefaultReturnValue()) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllChar(Long2CharMap m, CharCharUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Long2CharMap.Entry entry : getFastIterable(m)) {
			long key = entry.getLongKey();
			char oldValue = get(key);
			char newValue = oldValue == getDefaultReturnValue() ? entry.getCharValue() : mappingFunction.applyAsChar(oldValue, entry.getCharValue());
			if(newValue == getDefaultReturnValue()) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Character get(Object key) {
		return Character.valueOf(key instanceof Long ? get(((Long)key).longValue()) : getDefaultReturnValue());
	}
	
	@Override
	public Character getOrDefault(Object key, Character defaultValue) {
		return Character.valueOf(key instanceof Long ? getOrDefault(((Long)key).longValue(), defaultValue.charValue()) : getDefaultReturnValue());
	}
	
	@Override
	public char getOrDefault(long key, char defaultValue) {
		char value = get(key);
		return value != getDefaultReturnValue() || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Character remove(Object key) {
		return key instanceof Long ? Character.valueOf(remove(((Long)key).longValue())) : Character.valueOf(getDefaultReturnValue());
	}
	
	@Override
	public void forEach(LongCharConsumer action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Long2CharMap.Entry> iter = getFastIterator(this);iter.hasNext();) {
			Long2CharMap.Entry entry = iter.next();
			action.accept(entry.getLongKey(), entry.getCharValue());
		}
	}

	@Override
	public LongSet keySet() {
		return new AbstractLongSet() {
			@Override
			public boolean remove(long o) {
				return AbstractLong2CharMap.this.remove(o) != getDefaultReturnValue();
			}
			
			@Override
			public boolean add(long o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public LongIterator iterator() {
				return new LongIterator() {
					ObjectIterator<Long2CharMap.Entry> iter = getFastIterator(AbstractLong2CharMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}

					@Override
					public long nextLong() {
						return iter.next().getLongKey();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
			
			@Override
			public int size() {
				return AbstractLong2CharMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractLong2CharMap.this.clear();
			}
		};
	}

	@Override
	public CharCollection values() {
		return new AbstractCharCollection() {
			@Override
			public boolean add(char o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractLong2CharMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractLong2CharMap.this.clear();
			}
			
			@Override
			public CharIterator iterator() {
				return new CharIterator() {
					ObjectIterator<Long2CharMap.Entry> iter = getFastIterator(AbstractLong2CharMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public char nextChar() {
						return iter.next().getCharValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<Long, Character>> entrySet() {
		return (ObjectSet)long2CharEntrySet();
	}

	@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 Long2CharMap) return long2CharEntrySet().containsAll(((Long2CharMap)o).long2CharEntrySet());
			return long2CharEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Long2CharMap.Entry> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedLong2CharOrderedMap extends AbstractLong2CharMap implements Long2CharOrderedMap {
		Long2CharOrderedMap map;
		
		public ReversedLong2CharOrderedMap(Long2CharOrderedMap map) {
			this.map = map;
		}
		@Override
		public AbstractLong2CharMap setDefaultReturnValue(char v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public char getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Long2CharOrderedMap copy() { throw new UnsupportedOperationException(); }
		@Override
		public char put(long key, char value) { return map.put(key, value); }
		@Override
		public char putIfAbsent(long key, char value) { return map.putIfAbsent(key, value); }
		@Override
		public char addTo(long key, char value) { return map.addTo(key, value); }
		@Override
		public char subFrom(long key, char value) { return map.subFrom(key, value); }
		@Override
		public char remove(long key) { return map.remove(key); }
		@Override
		public boolean remove(long key, char value) { return map.remove(key, value); }
		@Override
		public char removeOrDefault(long key, char defaultValue) { return map.removeOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(long key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(char value) { return map.containsValue(value); }
		@Override
		public boolean replace(long key, char oldValue, char newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public char replace(long key, char value) { return map.replace(key, value); }
		@Override
		public void replaceChars(Long2CharMap m) { map.replaceChars(m); }
		@Override
		public void replaceChars(LongCharUnaryOperator mappingFunction) { map.replaceChars(mappingFunction); }
		@Override
		public char computeChar(long key, LongCharUnaryOperator mappingFunction) { return map.computeChar(key, mappingFunction); }
		@Override
		public char computeCharIfAbsent(long key, Long2CharFunction mappingFunction) { return map.computeCharIfAbsent(key, mappingFunction); }
		@Override
		public char supplyCharIfAbsent(long key, CharSupplier valueProvider) { return map.supplyCharIfAbsent(key, valueProvider); }
		@Override
		public char computeCharIfPresent(long key, LongCharUnaryOperator mappingFunction) { return map.computeCharIfPresent(key, mappingFunction); }
		@Override
		public char computeCharNonDefault(long key, LongCharUnaryOperator mappingFunction) { return map.computeCharNonDefault(key, mappingFunction); }
		@Override
		public char computeCharIfAbsentNonDefault(long key, Long2CharFunction mappingFunction) { return map.computeCharIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public char supplyCharIfAbsentNonDefault(long key, CharSupplier valueProvider) { return map.supplyCharIfAbsentNonDefault(key, valueProvider); }
		@Override
		public char computeCharIfPresentNonDefault(long key, LongCharUnaryOperator mappingFunction) { return map.computeCharIfPresentNonDefault(key, mappingFunction); }
		@Override
		public char mergeChar(long key, char value, CharCharUnaryOperator mappingFunction) { return map.mergeChar(key, value, mappingFunction); }
		@Override
		public char getOrDefault(long key, char defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public char get(long key) { return map.get(key); }
		@Override
		public char putAndMoveToFirst(long key, char value) { return map.putAndMoveToLast(key, value); }
		@Override
		public char putAndMoveToLast(long key, char value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public char putFirst(long key, char value) { return map.putLast(key, value); }
		@Override
		public char putLast(long key, char value) { return map.putFirst(key, value); }
		@Override
		public boolean moveToFirst(long key) { return map.moveToLast(key); }
		@Override
		public boolean moveToLast(long key) { return map.moveToFirst(key); }
		@Override
		public char getAndMoveToFirst(long key) { return map.getAndMoveToLast(key); }
		@Override
		public char getAndMoveToLast(long key) { return map.getAndMoveToFirst(key); }
		@Override
		public long firstLongKey() { return map.lastLongKey(); }
		@Override
		public long pollFirstLongKey() { return map.pollLastLongKey(); }
		@Override
		public long lastLongKey() { return map.firstLongKey(); }
		@Override
		public long pollLastLongKey() { return map.pollFirstLongKey(); }
		@Override
		public char firstCharValue() { return map.lastCharValue(); }
		@Override
		public char lastCharValue() { return map.firstCharValue(); }
		@Override
		public Long2CharMap.Entry firstEntry() { return map.lastEntry(); }
		@Override
		public Long2CharMap.Entry lastEntry() { return map.firstEntry(); }
		@Override
		public Long2CharMap.Entry pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Long2CharMap.Entry pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Long2CharMap.Entry> long2CharEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.long2CharEntrySet()); }
		@Override
		public LongOrderedSet keySet() { return new AbstractLongSet.ReversedLongOrderedSet(map.keySet()); }
		@Override
		public CharOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Long2CharOrderedMap reversed() { return map; }
	}

	
	/**
	 * A Simple Type Specific Entry class to reduce boxing/unboxing
	 */
	public static class BasicEntry implements Long2CharMap.Entry {
		protected long key;
		protected char 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(Long key, Character value) {
			this.key = key.longValue();
			this.value = value.charValue();
		}
		
		/**
		 * A Type Specific Constructor
		 * @param key the key of a entry
		 * @param value the value of a entry 
		 */
		public BasicEntry(long key, char 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(long key, char value) {
			this.key = key;
			this.value = value;
		}
		
		@Override
		public long getLongKey() {
			return key;
		}

		@Override
		public char getCharValue() {
			return value;
		}

		@Override
		public char setValue(char value) {
			throw new UnsupportedOperationException();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Map.Entry) {
				if(obj instanceof Long2CharMap.Entry) {
					Long2CharMap.Entry entry = (Long2CharMap.Entry)obj;
					return key == entry.getLongKey() && value == entry.getCharValue();
				}
				Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
				Object key = entry.getKey();
				Object value = entry.getValue();
				return key instanceof Long && value instanceof Character && this.key == ((Long)key).longValue() && this.value == ((Character)value).charValue();
			}
			return false;
		}
		
		@Override
		public int hashCode() {
			return Long.hashCode(key) ^ Character.hashCode(value);
		}
		
		@Override
		public String toString() {
			return Long.toString(key) + "=" + Character.toString(value);
		}
	}
}