/*******************************************************************************
 *  Copyright 2017 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.hl7282.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.collections.map.MultiValueMap;

import cdac.medinfo.sdk.hl7282.commoninterface.ICollectionIterator;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
/**
 * This interface provides the user with an abstract interface towards collections
 * holding series of objects. 
 * @param <T1> object object specifying key at the time of instantiation of this interface
 * @param <T2> object object specifying value at the time of instantiation of this interface
 */
public class MultiValuedPairedCollection <T1,T2> implements IMultiValuedPairedCollection<T1,T2>
{
	MultiValueMap objMultiValueMap;

	/**
	 * Constructs a new MultiValuedPairedCollection with the same mappings as the specified Collection.
	 */
	public MultiValuedPairedCollection()
	{
		objMultiValueMap = new MultiValueMap();
	}

	/**
	 * This inner class works as an Iterator which helps iterating over the map contained in Message or Group
	 * This iterator enables you to traverse through a collection 
	 * This class is implemented such that the programmer can have any 
	 * object stored in it he/she wants and having ITag as its key.
	 */
	public class CollectionIterator implements ICollectionIterator<T2>
	{
		Set<T1> mapKeySet = null;
		Iterator<T1> objIterator = null;

		/**
		 * Constructs an Iterator over underlying collection
		 */
		@SuppressWarnings("unchecked")
		public CollectionIterator()
		{
			mapKeySet = objMultiValueMap.keySet();
			objIterator = mapKeySet.iterator();
		}

		/**
		 * Gives the status whether collection contains more element.
		 * @return boolean - status as success or failure.
		 */
		public boolean hasNext()
		{
			return objIterator.hasNext();
		}

		/**
		 * Gives the next element from collection.
		 * @return T2 - generic type object.
		 */
		@SuppressWarnings("unchecked")
		public T2 next()
		{
			T1 objT1 = objIterator.next();
			T2 objT2 = (T2)objMultiValueMap.get(objT1);
			return objT2;
		}

		/**
		 * Removes the element from collection.
		 */
		public void remove()
		{

		}
	}	

	/**
	 * This inner class works as an Sorted Iterator which helps iterating over the map contained in Message or Group
	 * This iterator can be used for the purpose of getting all the elements of the DataSet one by one as it iterates elements
	 * in Sorted order. This class is implemented such that the programmer can have any 
	 * object stored in it he/she wants and having ITag as its key.
	 */
	public class SortedIterator implements ICollectionIterator<T2>
	{
		Set<T1> mapKeySet = null;
		TreeSet<T1> objSortedTreeSet = null;
		Iterator<T1> objIterator = null;

		/**
		 * Constructs an SortedIterator over underlying collection
		 */
		@SuppressWarnings("unchecked")
		public SortedIterator()
		{
			mapKeySet = objMultiValueMap.keySet();
			objSortedTreeSet = new TreeSet<T1>();
			Iterator<T1> objSetIterator = mapKeySet.iterator();
			while(objSetIterator.hasNext())
			{
				objSortedTreeSet.add(objSetIterator.next());
			}
			objIterator = objSortedTreeSet.iterator();
		}

		/**
		 * Gives the status whether collection contains more element.
		 * @return boolean - status as success or failure.
		 */
		public boolean hasNext()
		{
			return objIterator.hasNext();
		}

		/**
		 * Gives the next element from collection.
		 * @return T2 - generic type object.
		 */
		@SuppressWarnings("unchecked")
		public T2 next()
		{
			T1 objT1 = objIterator.next();
			T2 objT2 = (T2)objMultiValueMap.get(objT1);
			return objT2;
		}

		/**
		 * Removes the element from collection.
		 */
		public void remove()
		{

		}
	}

	/**
	 * Returns an iterator over the internal collection.
	 * @return iterator - returns the iterator over the internal collection.
	 */
	public ICollectionIterator<T2> iterator()
	{
		return new CollectionIterator();
	}

	/**
	 * Returns an iterator over the internal collection.
	 * @return ICollectionIterator&lt;T2&gt; - returns the iterator over the internal collection.
	 */
	public ICollectionIterator<T2> sortedIterator()
	{
		return new SortedIterator();
	}

	/**
	 * Provides key based accessing over the internal collection
	 * @param objT1 This parameter specifies the key whose corresponding value is to be retrieved 
	 * @return T2 - object of the type value user specifies at the time of instantiation of this interface
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<T2> get(T1 objT1)
	{
		ArrayList<T2> arrayList = (ArrayList<T2>)objMultiValueMap.get(objT1);
		if(arrayList == null)
			return null;
		ISingleCollection<T2> collection = new SingleCollection<T2>();
		if(collection.addAll(arrayList))
			return collection;
		return null;
	}

	/**
	 * Retrieves All values present in the collection
	 * @return ISingleCollection<T2> list of T2 type values
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<T2> getAllValues()
	{
		ISingleCollection<T2> collection = new SingleCollection<T2>();
		Set<T1> objMapKeySet = objMultiValueMap.keySet();
		Iterator<T1> objIterator = objMapKeySet.iterator();
		T1 objT1 = null;
		while(objIterator.hasNext())
		{
			objT1 = objIterator.next();
			ArrayList<T2> arrayList = (ArrayList<T2>)objMultiValueMap.get(objT1);
			if(arrayList != null)
			{
				collection.addAll(arrayList);
			}
		}
		return collection;
	}

	/**
	 * Provides a mechanism for adding a key value pair to the collection
	 * @param objT1 This parameter specifies the key.
	 * @param objT2 This parameter specifies the value.
	 */
	public void put(T1 objT1, T2 objT2)
	{
		objMultiValueMap.put(objT1, objT2);
	}

	/**
	 * Provides a mechanism for removing a key value pair to the collection
	 * @param objT1 This parameter specifies the key whose corresponding value to remove
	 */
	public void remove(T1 objT1)
	{
		if(objMultiValueMap.containsKey(objT1))
			objMultiValueMap.remove(objT1);
	}
	/**
	 * Provides a removes a specified value present for specified key and removes key too if no 
	 * values present for that key. 
	 * @param objT1 This parameter specifies the key whose corresponding value to remove
	 * @param objT2 object specifying the value corresponding to the key in
	 * the specified collection
	 */
	@SuppressWarnings("unchecked")
	public void remove(T1 objT1, T2 objT2) 
	{
		if(objMultiValueMap.containsKey(objT1))
		{
			ArrayList<T2> arrayList = (ArrayList<T2>)objMultiValueMap.get(objT1);
			if(arrayList.contains(objT2))
				arrayList.remove(objT2);			
			if(arrayList.size() == 0)
				objMultiValueMap.remove(objT1);				
		}

	}

	/**
	 * Provides a mechanism for adding a key value pair to the collection
	 * @param objT1 This parameter specifies the key.
	 * @param objT2 This parameter specifies the value.
	 */
	public void put(T1 objT1, ISingleCollection<T2> objT2)
	{
		ICollectionIterator<T2> iterator = objT2 != null ? (objT2.iterator()) : null;
		if(iterator != null)
		{
			while(iterator.hasNext())
				objMultiValueMap.put(objT1, iterator.next());
		}
		else
			objMultiValueMap.put(objT1, null);
	}

	/**
	 * Returns the status indicating whether the key is present in the collection or not
	 * @param objT1 element whose presence in this collection is to be tested 
	 * @return boolean - returns the status indicating whether the key is present in the collection or not
	 */
	public boolean contains(T1 objT1)
	{
		return objMultiValueMap.containsKey(objT1);
	}

	/**
	 * Returns the size of the collection
	 * @return int - returns the size of the collection
	 */
	public int size()
	{
		return objMultiValueMap.size();
	}


}
