/*******************************************************************************
 *  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.baselibs;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumFillMode;
import cdac.medinfo.sdk.dcm30_04.enums.EnumImageSetSelectorCategory;
import cdac.medinfo.sdk.dcm30_04.enums.EnumRelativeTimeUnits;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
/**
* This item class contains methods to validate, set and get the attributes of Time Based Image Sets Sequence.
 */

public class TimeBasedImageSetsSequenceItem extends Item{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	TimeBasedImageSetsSequenceItem()
	{
		super();
	}


	/**
	 * This constructor creates the item and internal code sequence macro with specified TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	TimeBasedImageSetsSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates TimeBasedImageSetsSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	TimeBasedImageSetsSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	
	public boolean validateForModule()  throws InvalidItemException
	{	
	
		try
		{
			if(!contains(0x0072,0x0032))
				throw new MandatoryMissingException("0072,0032") ;
			if(!contains(0x0072,0x0034))
				throw new MandatoryMissingException("0072,0034") ;
			if(contains(0x0072,0x003A) && (!contains(0x0072,0x0038)))
				throw new ConditionFailedException("0072,0038") ;
			if(contains(0x0072,0x003C) && getDataForTag(0x0072, 0x0034).getValue().equals(EnumImageSetSelectorCategory.ABSTRACT_PRIOR))
				if(!contains(0x0072,0x003E))
				throw new ConditionFailedException("0x0072,0x003C") ;
			if(contains(0x0072,0x003E) && getDataForTag(0x0072, 0x0034).getValue().equals(EnumImageSetSelectorCategory.ABSTRACT_PRIOR))
				if(!contains(0x0072,0x003C))
				throw new ConditionFailedException("0x0072,0x003E") ;
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	

	/**
	 * Sets Number of significant bits a monotonically increasing integer
	 * @param imageSetNumber short value of Number of significant bits
	 */
	public void setImageSetNumber(short imageSetNumber)
	{
		/* Accept the Integer type value.
		 * Set the Image Set Number Protocol Instance
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0072,0x0032,imageSetNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0072,0x0032,DicomConstants.DCM_DT_US, imageSetNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of significant bits with increasing value
	 * @return  value of Number of significant bits
	 */
	public short getImageSetNumber()
	{
		Object objValue = getValue(0x0072,0x0032);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets the Value Type Defined in category of the Time Based Image Set selector
	 * @param enumImageSetSelectorCategory Specifies type of the value encoded in this Item
	 */
	public void setImageSetSelectorCategory(EnumImageSetSelectorCategory enumImageSetSelectorCategory)
	{
		/*Accept the enumerated value of ImageSet Selector Category.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * "RELATIVE_TIME"
		 * "ABSTRACT_PRIOR"
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0072,0x0034, enumImageSetSelectorCategory,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0072,0x0034,DicomConstants.DCM_DT_CS, enumImageSetSelectorCategory);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of the Image Set Selector Category
	 * @return String - Type of the value encoded in this Item
	 */
	public String getImageSetSelectorCategory()
	{
		Object objValue = getValue(0x0072,0x0034);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	public void setRelativeTime(short[] arrRelativeTime)
	{
		/*Accept the Relative Time in Array of short format of type US.
		 * two numeric values, indicating the start and end values of a prior range of instance acquisition times 
		 * It is relative to the date and time of a current image set.
		 * The units shall be specified in Relative Time Units (0072,003A).
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0072,0x0038, arrRelativeTime,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0072,0x0038,DicomConstants.DCM_DT_US, arrRelativeTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves get Relative Time.
	 * @return short Array  Relative Time Units.
	 */
	public short[] getRelativeTime()
	{
		Object objValue = getValue(0x0072,0x0038);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}

	/**
	 * Sets the Value Type Defined in Terms of TIME
	 * @param strRelativeTimeUnits Specifies type of the value encoded in this Item
	 */
	public void setRelativeTimeUnits(EnumRelativeTimeUnits enumRelativeTimeUnits)
	{
		/*Accept the Value Type in enumRelativeTimeUnits.
		 *Enumerated Values: SECONDS,MINUTES,HOURS,DAYS,WEEKS,MONTHS,YEARS
		 *Units of time for Relative Time (0072,0038). 
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0072, 0x003A, enumRelativeTimeUnits,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0072, 0x003A,DicomConstants.DCM_DT_CS, enumRelativeTimeUnits);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves type of the value encoded in this Item
	 * @return String - Relative Time Units in this Item
	 */
	public String getRelativeTimeUnits()
	{
		Object objValue = getValue(0x0072, 0x003A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Exactly two integer values, indicating the range of prior studies to include. 
	 * Each value shall be greater than zero, where 1 indicates the most recent prior and 
	 * higher values indicate successively older priors. 
	 * @param sAbstractPriorValue short array of integer value. 
	 */
	public void setAbstractPriorValue(short[] sAbstractPriorValue)
	{
		/* Accept the sAbstractPriorValue in short Array format 
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Required if Image Set Selector Category (0072,0034) is ABSTRACT_PRIOR and 
		 * Abstract Prior Code Sequence (0072,003E) is not present.
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0072, 0x003C, sAbstractPriorValue,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0072, 0x003C,DicomConstants.DCM_DT_US, sAbstractPriorValue);
		addDataElement(objIDataElement);
	}
	/**
	 * Returns the format of the LUT Data in this Sequence.
	 * @return short[] - short array of Abstract Prior Value.
	 */
	public short[] getAbstractPriorValue()
	{
		Object objValue = getValue(0x0072, 0x003C);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
	
	/**
	 * Identifies a prior image set using coded terminology.
	 * Only a single Item shall be included in this Sequence.
	 * and then returns the object of sequence.
	 * @return AbstractPriorCodeSequence - instance of AbstractPriorCodeSequence. 
	 */
	public AbstractPriorCodeSequence getAbstractPriorCodeSequence()
	{
		AbstractPriorCodeSequence objAbstractPriorCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0072, 0x003E);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objAbstractPriorCodeSequence = (AbstractPriorCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objAbstractPriorCodeSequence = (AbstractPriorCodeSequence)objDicomV3Factory.createSequence(0x0072, 0x003E, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0072, 0x003E, objAbstractPriorCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0072, 0x003E, DicomConstants.DCM_DT_SQ, objAbstractPriorCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objAbstractPriorCodeSequence;
	}

	/**
	 * Sets ImageSetLabel.
	 * @param strImageSetLabel String value.
	 */
	public void setImageSetLabel(String strImageSetLabel)
	{
		/* Accept the strImageSetLabel Type in String format.
		 * Description of the objective of the image set defined by this sequence item.
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0072,0x0040, strImageSetLabel,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0072,0x0040,DicomConstants.DCM_DT_LO, strImageSetLabel);

		addDataElement(objIDataElement);
	}

	/**
	 * Return image set defined identifier for ImageSetLabel.
	 * @return String - value of Image Set Label.
	 */
	public String getImageSetLabel()
	{
		Object objValue = getValue(0x0072,0x0040);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
