/*******************************************************************************
 *  Copyright 2017-2018 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.dcm30_04.services.mediacreation.mediacreationsourceimpl;

import cdac.medinfo.sdk.dcm30_04.baselibs.DataSet;
import cdac.medinfo.sdk.dcm30_04.baselibs.ExplicitDE;
import cdac.medinfo.sdk.dcm30_04.baselibs.ImplicitDE;
import cdac.medinfo.sdk.dcm30_04.baselibs.TransferSyntax;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.dictionaryreaderwriters.DictionaryReader;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.ICollectionIterator;
import cdac.medinfo.sdk.dcm30_04.utils.StandardMethods;
/**
 * This class initiates Attribute class
 */
public abstract class Attributes 
{
	private IDataSet objIDataSet;
	
	/**
	 * Initiates the Attribute class
	 */
	Attributes()
	{
		ITransferSyntax objITransferSyntax_ = new TransferSyntax();
		IDictionaryReader objIDictionaryReader_ = DictionaryReader.createInstance();
		objIDataSet = new DataSet(objITransferSyntax_, objIDictionaryReader_);
	}
	
	/**
	 * Retrieves the object of DataSet.
	 * @return
	 */
	protected IDataSet getDataSet()
	{
		return this.objIDataSet;
	}

	/**
	 * Adds the data element in to the dataset.
	 * @param iGroupNumber integer value of Group Number.
	 * @param iElementNumber integer value of element number
	 * @param iVRType int value for VR Type.
	 * @param objValue Value of a dataelement.
	 */
	protected void addDataElement(int iGroupNumber,int iElementNumber, int iVRType,Object objValue)
	{
		IDataElement objIDataElement = null;
		
		ITransferSyntax objITransferSyntax = objIDataSet.getTransferSyntax();
		IDictionaryReader objIDictionaryReader = objIDataSet.getDictionaryReader();
		
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(iGroupNumber, iElementNumber, objValue, objITransferSyntax, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(iGroupNumber, iElementNumber, iVRType, objValue,  objITransferSyntax, objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
	}
	
	/**
	 * Gets the DICOM Value.
	 * @param iGroupNumber integer value of Group Number.
	 * @param iElementNumber integer value of element number
	 * @return Object of IDicomValue
	 */
	protected IDicomValue getDicomValue(int iGroupNumber, int iElementNumber)
	{
		IDataElement objIDataElement =  objIDataSet.getDataForTag(iGroupNumber,iElementNumber);
		if(objIDataElement == null)
			return null;
		IDicomValue objIDicomValue = objIDataElement.getDicomValue();
		if(objIDicomValue == null)
			return null;
		return objIDicomValue;
	}
	
	/**
	 * This method returns value of DicomValue.
	 * @param iGroupNumber integer value of Group Number.
	 * @param iElementNumber integer value of element number
	 * @return Object - value of the Data Element.
	 */
	protected Object getValue(int iGroupNumber, int iElementNumber)
	{
		IDataElement objIDataElement =  objIDataSet.getDataForTag(iGroupNumber,iElementNumber);
		if(objIDataElement == null)
			return null;
		IDicomValue objIDicomValue = objIDataElement.getDicomValue();
		if(objIDicomValue == null)
			return null;
		return objIDicomValue.getValue();
	}
	
	/**
	 * Populates the Query Model from filled dataset.
	 * @param objIDataSet Object of IDataSet
	 */
	public void populate(IDataSet objIDataSet)
	{
		ICollectionIterator<IDataElement> objDataElementsColl = objIDataSet.iterator();
		
		while(objDataElementsColl.hasNext())
		{
			IDataElement objIDataElement = objDataElementsColl.next();
			//this.objIDataSet.addDataElement(objIDataElement);
			
			ITag objITag = objIDataElement.getTag();
			int iGroupNumber = objITag.getGroupNumber();
			int iElementNumber = objITag.getElementNumber();
			
			IDictionaryReader objIDictionaryReader = this.objIDataSet.getDictionaryReader();
			String strVRType = objIDictionaryReader.getVR(iGroupNumber, iElementNumber);
			int iVRType = StandardMethods.getiVRtype(strVRType);
			
			Object objValue = objIDataElement.getValue();
			
			addDataElement(iGroupNumber, iElementNumber, iVRType, objValue);
		}
	}	
	
	/**
	 * Fills the DataSet according to populated Query Model.
	 * @param objIDataSet Object of IDataSet
	 */
	public void fill(IDataSet objIDataSet)
	{
		/* while filling the dataset using already populated query model, the transfer syntax of dataset present 
		 * in QueryModel should be same as transfer syntax of the data set to be filled.
		 * 
		 */
		ITransferSyntax objITransferSyntax = objIDataSet.getTransferSyntax();
		IDictionaryReader objIDictionaryReader = objIDataSet.getDictionaryReader();
		
		ICollectionIterator<IDataElement> objDataElementsColl = this.objIDataSet.iterator();
		
		while(objDataElementsColl.hasNext())
		{
			IDataElement objIDataElement = objDataElementsColl.next();
			ITag objITag = objIDataElement.getTag();
			int iGroupNumber = objITag.getGroupNumber();
			int iElementNumber = objITag.getElementNumber();
			
			String strVRType = objIDictionaryReader.getVR(iGroupNumber, iElementNumber);
			int iVRType = StandardMethods.getiVRtype(strVRType);
			Object objValue = objIDataElement.getValue();
			
			addDataElement(objIDataSet, iGroupNumber, iElementNumber, iVRType, objValue, objITransferSyntax, objIDictionaryReader);
		}
	}
	
	/**
	 * Adds the data element in to the dataset.
	 * @param objIDataSet Object of dataset into which data element is to be added.
	  * @param iGroupNumber integer value of Group Number.
	 * @param iElementNumber integer value of element number
	 * @param iVRType int value for VR Type.
	 * @param objValue Value of a dataelement.
	 * @param objITransferSyntax Object of ITranseferSyntax.
	 * @param objIDictionaryReader Object of IDictionaryReader.
	 */
	protected void addDataElement(IDataSet objIDataSet, int iGroupNumber,int iElementNumber, int iVRType,Object objValue, ITransferSyntax objITransferSyntax, IDictionaryReader objIDictionaryReader)
	{
		IDataElement objIDataElement = null;
		
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(iGroupNumber, iElementNumber, objValue, objITransferSyntax, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(iGroupNumber, iElementNumber, iVRType, objValue,  objITransferSyntax, objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
	}

}
