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

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

import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.functions.consumer.ObjectLongConsumer;
import speiger.src.collections.objects.functions.function.ToLongFunction;
import speiger.src.collections.objects.functions.function.ObjectLongUnaryOperator;
import speiger.src.collections.objects.maps.interfaces.Object2LongMap;
import speiger.src.collections.objects.maps.interfaces.Object2LongOrderedMap;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
import speiger.src.collections.longs.collections.LongOrderedCollection;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet;
import speiger.src.collections.objects.utils.maps.Object2LongMaps;
import speiger.src.collections.longs.collections.AbstractLongCollection;
import speiger.src.collections.longs.collections.LongCollection;
import speiger.src.collections.longs.collections.LongIterator;
import speiger.src.collections.longs.functions.function.LongLongUnaryOperator;
import speiger.src.collections.longs.functions.LongSupplier;
import speiger.src.collections.objects.collections.ObjectIterable;
import speiger.src.collections.utils.SanityChecks;

/**
 * A Base Implementation of a Type Specific Map to reduce boxing/unboxing
 * @param <T> the keyType of elements maintained by this Collection
 */
public abstract class AbstractObject2LongMap<T> extends AbstractMap<T, Long> implements Object2LongMap<T>
{
	protected long defaultReturnValue = -1L;
	
	@Override
	public long getDefaultReturnValue() {
		return defaultReturnValue;
	}
	
	@Override
	public AbstractObject2LongMap<T> setDefaultReturnValue(long v) {
		defaultReturnValue = v;
		return this;
	}
	
	protected ObjectIterable<Object2LongMap.Entry<T>> getFastIterable(Object2LongMap<T> map) {
		return Object2LongMaps.fastIterable(map);
	}
	
	protected ObjectIterator<Object2LongMap.Entry<T>> getFastIterator(Object2LongMap<T> map) {
		return Object2LongMaps.fastIterator(map);
	}
	
	@Override
	public Object2LongMap<T> copy() { 
		throw new UnsupportedOperationException();
	}
	
	@Override
	@Deprecated
	public Long put(T key, Long value) {
		return Long.valueOf(put(key, value.longValue()));
	}
	
	@Override
	public void addToAll(Object2LongMap<T> m) {
		for(Object2LongMap.Entry<T> entry : getFastIterable(m))
			addTo(entry.getKey(), entry.getLongValue());
	}
	
	@Override
	public void putAll(Object2LongMap<T> m) {
		for(ObjectIterator<Object2LongMap.Entry<T>> iter = getFastIterator(m);iter.hasNext();) {
			Object2LongMap.Entry<T> entry = iter.next();
			put(entry.getKey(), entry.getLongValue());
		}
	}
	
	@Override
	public void putAll(Map<? extends T, ? extends Long> m)
	{
		if(m instanceof Object2LongMap) putAll((Object2LongMap<T>)m);
		else super.putAll(m);
	}
	
	@Override
	public void putAll(T[] keys, long[] 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(T[] keys, Long[] 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].longValue());		
	}
	
	@Override
	public void putAllIfAbsent(Object2LongMap<T> m) {
		for(Object2LongMap.Entry<T> entry : getFastIterable(m))
			putIfAbsent(entry.getKey(), entry.getLongValue());
	}
	
	
	@Override
	public boolean containsKey(Object key) {
		for(ObjectIterator<T> iter = keySet().iterator();iter.hasNext();)
			if(Objects.equals(key, iter.next())) return true;
		return false;
	}
	
	@Override
	public boolean containsValue(long value) {
		for(LongIterator iter = values().iterator();iter.hasNext();)
			if(iter.nextLong() == value) return true;
		return false;
	}
	
	@Override
	public boolean replace(T key, long oldValue, long newValue) {
		long curValue = getLong(key);
		if (curValue != oldValue || (curValue == getDefaultReturnValue() && !containsKey(key))) {
			return false;
		}
		put(key, newValue);
		return true;
	}

	@Override
	public long replace(T key, long value) {
		long curValue;
		if ((curValue = getLong(key)) != getDefaultReturnValue() || containsKey(key)) {
			curValue = put(key, value);
		}
		return curValue;
	}
	
	@Override
	public void replaceLongs(Object2LongMap<T> m) {
		for(Object2LongMap.Entry<T> entry : getFastIterable(m))
			replace(entry.getKey(), entry.getLongValue());
	}
	
	@Override
	public void replaceLongs(ObjectLongUnaryOperator<T> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(ObjectIterator<Object2LongMap.Entry<T>> iter = getFastIterator(this);iter.hasNext();) {
			Object2LongMap.Entry<T> entry = iter.next();
			entry.setValue(mappingFunction.applyAsLong(entry.getKey(), entry.getLongValue()));
		}
	}

	@Override
	public long computeLong(T key, ObjectLongUnaryOperator<T> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		long newValue = mappingFunction.applyAsLong(key, getLong(key));
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public long computeLongIfAbsent(T key, ToLongFunction<T> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(!containsKey(key)) {
			long newValue = mappingFunction.applyAsLong(key);
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public long supplyLongIfAbsent(T key, LongSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		if(!containsKey(key)) {
			long newValue = valueProvider.getAsLong();
			put(key, newValue);
			return newValue;
		}
		return get(key);
	}
	
	@Override
	public long computeLongIfPresent(T key, ObjectLongUnaryOperator<T> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		if(containsKey(key)) {
			long newValue = mappingFunction.applyAsLong(key, getLong(key));
			put(key, newValue);
			return newValue;
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public long computeLongNonDefault(T key, ObjectLongUnaryOperator<T> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		long value = getLong(key);
		long newValue = mappingFunction.applyAsLong(key, value);
		if(newValue == getDefaultReturnValue()) {
			if(value != getDefaultReturnValue() || containsKey(key)) {
				remove(key);
				return getDefaultReturnValue();
			}
			return getDefaultReturnValue();
		}
		put(key, newValue);
		return newValue;
	}
	
	@Override
	public long computeLongIfAbsentNonDefault(T key, ToLongFunction<T> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		long value;
		if((value = getLong(key)) == getDefaultReturnValue() || !containsKey(key)) {
			long newValue = mappingFunction.applyAsLong(key);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public long supplyLongIfAbsentNonDefault(T key, LongSupplier valueProvider) {
		Objects.requireNonNull(valueProvider);
		long value;
		if((value = getLong(key)) == getDefaultReturnValue() || !containsKey(key)) {
			long newValue = valueProvider.getAsLong();
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
		}
		return value;
	}
	
	@Override
	public long computeLongIfPresentNonDefault(T key, ObjectLongUnaryOperator<T> mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		long value;
		if((value = getLong(key)) != getDefaultReturnValue() || containsKey(key)) {
			long newValue = mappingFunction.applyAsLong(key, value);
			if(newValue != getDefaultReturnValue()) {
				put(key, newValue);
				return newValue;
			}
			remove(key);
		}
		return getDefaultReturnValue();
	}
	
	@Override
	public long mergeLong(T key, long value, LongLongUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		long oldValue = getLong(key);
		long newValue = oldValue == getDefaultReturnValue() ? value : mappingFunction.applyAsLong(oldValue, value);
		if(newValue == getDefaultReturnValue()) remove(key);
		else put(key, newValue);
		return newValue;
	}
	
	@Override
	public void mergeAllLong(Object2LongMap<T> m, LongLongUnaryOperator mappingFunction) {
		Objects.requireNonNull(mappingFunction);
		for(Object2LongMap.Entry<T> entry : getFastIterable(m)) {
			T key = entry.getKey();
			long oldValue = getLong(key);
			long newValue = oldValue == getDefaultReturnValue() ? entry.getLongValue() : mappingFunction.applyAsLong(oldValue, entry.getLongValue());
			if(newValue == getDefaultReturnValue()) remove(key);
			else put(key, newValue);
		}
	}
	
	@Override
	public Long get(Object key) {
		return Long.valueOf(getLong((T)key));
	}
	
	@Override
	public Long getOrDefault(Object key, Long defaultValue) {
		Long value = get(key);
		return value != getDefaultReturnValue() || containsKey(key) ? value : defaultValue;
	}
	
	
	@Override
	public Long remove(Object key) {
		return Long.valueOf(rem((T)key));
	}
	
	@Override
	public void forEach(ObjectLongConsumer<T> action) {
		Objects.requireNonNull(action);
		for(ObjectIterator<Object2LongMap.Entry<T>> iter = getFastIterator(this);iter.hasNext();) {
			Object2LongMap.Entry<T> entry = iter.next();
			action.accept(entry.getKey(), entry.getLongValue());
		}
	}

	@Override
	public ObjectSet<T> keySet() {
		return new AbstractObjectSet<T>() {
			@Override
			public boolean remove(Object o) {
				if(AbstractObject2LongMap.this.containsKey(o)) {
					AbstractObject2LongMap.this.remove(o);
					return true;
				}
				return false;
			}
			
			@Override
			public boolean add(T o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public ObjectIterator<T> iterator() {
				return new ObjectIterator<T>() {
					ObjectIterator<Object2LongMap.Entry<T>> iter = getFastIterator(AbstractObject2LongMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}

					@Override
					public T next() {
						return iter.next().getKey();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
			
			@Override
			public int size() {
				return AbstractObject2LongMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractObject2LongMap.this.clear();
			}
		};
	}

	@Override
	public LongCollection values() {
		return new AbstractLongCollection() {
			@Override
			public boolean add(long o) {
				throw new UnsupportedOperationException();
			}
			
			@Override
			public int size() {
				return AbstractObject2LongMap.this.size();
			}
			
			@Override
			public void clear() {
				AbstractObject2LongMap.this.clear();
			}
			
			@Override
			public LongIterator iterator() {
				return new LongIterator() {
					ObjectIterator<Object2LongMap.Entry<T>> iter = getFastIterator(AbstractObject2LongMap.this);
					@Override
					public boolean hasNext() {
						return iter.hasNext();
					}
					
					@Override
					public long nextLong() {
						return iter.next().getLongValue();
					}
					
					@Override
					public void remove() {
						iter.remove();
					}
				};
			}
		};
	}
	
	@Override
	@SuppressWarnings("rawtypes")
	public ObjectSet<Map.Entry<T, Long>> entrySet() {
		return (ObjectSet)object2LongEntrySet();
	}

	@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 Object2LongMap) return object2LongEntrySet().containsAll(((Object2LongMap<T>)o).object2LongEntrySet());
			return object2LongEntrySet().containsAll(((Map<?, ?>)o).entrySet());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int hash = 0;
		ObjectIterator<Object2LongMap.Entry<T>> iter = getFastIterator(this);
		while(iter.hasNext()) hash += iter.next().hashCode();
		return hash;
	}
	
	public static class ReversedObject2LongOrderedMap<T> extends AbstractObject2LongMap<T> implements Object2LongOrderedMap<T> {
		Object2LongOrderedMap<T> map;
		
		public ReversedObject2LongOrderedMap(Object2LongOrderedMap<T> map) {
			this.map = map;
		}
		@Override
		public AbstractObject2LongMap<T> setDefaultReturnValue(long v) {
			map.setDefaultReturnValue(v);
			return this;
		}
		@Override
		public long getDefaultReturnValue() { return map.getDefaultReturnValue(); }
		@Override
		public Object2LongOrderedMap<T> copy() { throw new UnsupportedOperationException(); }
		@Override
		public long put(T key, long value) { return map.put(key, value); }
		@Override
		public long putIfAbsent(T key, long value) { return map.putIfAbsent(key, value); }
		@Override
		public long addTo(T key, long value) { return map.addTo(key, value); }
		@Override
		public long subFrom(T key, long value) { return map.subFrom(key, value); }
		@Override
		public long rem(T key) { return map.rem(key); }
		@Override
		public boolean remove(T key, long value) { return map.remove(key, value); }
		@Override
		public long remOrDefault(T key, long defaultValue) { return map.remOrDefault(key, defaultValue); }
		@Override
		public boolean containsKey(Object key) { return map.containsKey(key); }
		@Override
		public boolean containsValue(long value) { return map.containsValue(value); }
		@Override
		public boolean replace(T key, long oldValue, long newValue) { return map.replace(key, oldValue, newValue); }
		@Override
		public long replace(T key, long value) { return map.replace(key, value); }
		@Override
		public void replaceLongs(Object2LongMap<T> m) { map.replaceLongs(m); }
		@Override
		public void replaceLongs(ObjectLongUnaryOperator<T> mappingFunction) { map.replaceLongs(mappingFunction); }
		@Override
		public long computeLong(T key, ObjectLongUnaryOperator<T> mappingFunction) { return map.computeLong(key, mappingFunction); }
		@Override
		public long computeLongIfAbsent(T key, ToLongFunction<T> mappingFunction) { return map.computeLongIfAbsent(key, mappingFunction); }
		@Override
		public long supplyLongIfAbsent(T key, LongSupplier valueProvider) { return map.supplyLongIfAbsent(key, valueProvider); }
		@Override
		public long computeLongIfPresent(T key, ObjectLongUnaryOperator<T> mappingFunction) { return map.computeLongIfPresent(key, mappingFunction); }
		@Override
		public long computeLongNonDefault(T key, ObjectLongUnaryOperator<T> mappingFunction) { return map.computeLongNonDefault(key, mappingFunction); }
		@Override
		public long computeLongIfAbsentNonDefault(T key, ToLongFunction<T> mappingFunction) { return map.computeLongIfAbsentNonDefault(key, mappingFunction); }
		@Override
		public long supplyLongIfAbsentNonDefault(T key, LongSupplier valueProvider) { return map.supplyLongIfAbsentNonDefault(key, valueProvider); }
		@Override
		public long computeLongIfPresentNonDefault(T key, ObjectLongUnaryOperator<T> mappingFunction) { return map.computeLongIfPresentNonDefault(key, mappingFunction); }
		@Override
		public long mergeLong(T key, long value, LongLongUnaryOperator mappingFunction) { return map.mergeLong(key, value, mappingFunction); }
		@Override
		public long getOrDefault(T key, long defaultValue) { return map.getOrDefault(key, defaultValue); }
		@Override
		public long getLong(T key) { return map.getLong(key); }
		@Override
		public long putAndMoveToFirst(T key, long value) { return map.putAndMoveToLast(key, value); }
		@Override
		public long putAndMoveToLast(T key, long value) { return map.putAndMoveToFirst(key, value); }
		@Override
		public long putFirst(T key, long value) { return map.putLast(key, value); }
		@Override
		public long putLast(T key, long value) { return map.putFirst(key, value); }
		@Override
		public boolean moveToFirst(T key) { return map.moveToLast(key); }
		@Override
		public boolean moveToLast(T key) { return map.moveToFirst(key); }
		@Override
		public long getAndMoveToFirst(T key) { return map.getAndMoveToLast(key); }
		@Override
		public long getAndMoveToLast(T key) { return map.getAndMoveToFirst(key); }
		@Override
		public T firstKey() { return map.lastKey(); }
		@Override
		public T pollFirstKey() { return map.pollLastKey(); }
		@Override
		public T lastKey() { return map.firstKey(); }
		@Override
		public T pollLastKey() { return map.pollFirstKey(); }
		@Override
		public long firstLongValue() { return map.lastLongValue(); }
		@Override
		public long lastLongValue() { return map.firstLongValue(); }
		@Override
		public Object2LongMap.Entry<T> firstEntry() { return map.lastEntry(); }
		@Override
		public Object2LongMap.Entry<T> lastEntry() { return map.firstEntry(); }
		@Override
		public Object2LongMap.Entry<T> pollFirstEntry() { return map.pollLastEntry(); }
		@Override
		public Object2LongMap.Entry<T> pollLastEntry() { return map.pollFirstEntry(); }
		@Override
		public ObjectOrderedSet<Object2LongMap.Entry<T>> object2LongEntrySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.object2LongEntrySet()); }
		@Override
		public ObjectOrderedSet<T> keySet() { return new AbstractObjectSet.ReversedObjectOrderedSet<>(map.keySet()); }
		@Override
		public LongOrderedCollection values() { return map.values().reversed(); }
		@Override
		public Object2LongOrderedMap<T> reversed() { return map; }
	}

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

		@Override
		public long getLongValue() {
			return value;
		}

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