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

import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumIVUSAcquisition;

import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;
/**
 \* This module describes attributes in IVUS Image Module.
 */
public class IVUSImageModule extends Module  
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


	/**
	 * Creates IVUS Image Module with default initialization.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public IVUSImageModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0018,0x3100));
		objSingleCollection.add(new Tag(0x0018,0x3101));
		objSingleCollection.add(new Tag(0x0018,0x3102));
		objSingleCollection.add(new Tag(0x0018,0x3103));
		objSingleCollection.add(new Tag(0x0018,0x3104));
		
	}

	/**
	 * This method validates each Attribute in the Module according to the Type
	 * of the Attribute.Attributes are of 5 types.
	 * Type 1,Type 2 are Mandatory:
	 * Mandatory Attributes have to present in the Module.
	 * Type 1C,2C are Conditional:
	 * Conditional Attributes' presence depend on a condition.
	 * If the condition is satisfied the Attribute has to be present.
	 * Type 3 are Optional
	 * Optional Attributes can be added as and when required.
	 * 
	 * @return true, if validate module
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{
		/*
		 * Validation Rules for Mandatory Elements
		 */
			if(!isPresent(0x0018,0x3100))
				throw new MandatoryMissingException("0018,3100");
			
			if(isPresent(0x0018,0x3100))
			{
				String strIVUSAcquisition = getIVUSAcquisition();
				if(strIVUSAcquisition.equalsIgnoreCase(EnumIVUSAcquisition.MOTOR_PULLBACK.getValue()) && !isPresent(0x0018,0x3101))
					throw new ConditionFailedException("0018,3101 Required if IVUS Acquisition (0018,3100) value is " + EnumIVUSAcquisition.MOTOR_PULLBACK.getValue());
				if(strIVUSAcquisition.equalsIgnoreCase(EnumIVUSAcquisition.GATED_PULLBACK.getValue()) && !isPresent(0x0018,0x3102))
					throw new ConditionFailedException("0018,3102 Required if IVUS Acquisition (0018,3100) value is " + EnumIVUSAcquisition.GATED_PULLBACK.getValue());
				if((strIVUSAcquisition.equalsIgnoreCase(EnumIVUSAcquisition.GATED_PULLBACK.getValue()) || strIVUSAcquisition.equalsIgnoreCase(EnumIVUSAcquisition.MOTOR_PULLBACK.getValue())) && !isPresent(0x0018,0x3103))
					throw new ConditionFailedException("0018,3103 Required if IVUS Acquisition (0018,3100) value is " + EnumIVUSAcquisition.MOTOR_PULLBACK.getValue() + "or " + EnumIVUSAcquisition.GATED_PULLBACK.getValue());
				if((strIVUSAcquisition.equalsIgnoreCase(EnumIVUSAcquisition.GATED_PULLBACK.getValue()) || strIVUSAcquisition.equalsIgnoreCase(EnumIVUSAcquisition.MOTOR_PULLBACK.getValue())) && !isPresent(0x0018,0x3104))
					throw new ConditionFailedException("0018,3104 Required if IVUS Acquisition (0018,3100) value is " + EnumIVUSAcquisition.MOTOR_PULLBACK.getValue() + "or " + EnumIVUSAcquisition.GATED_PULLBACK.getValue());
			}
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),mme);

		}
		catch(ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(cfe.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),cfe);

		}
		return true;
	}

	/**
	 * Sets IVUS Acquisition
	 * @param enumIVUSAcquisition - enum value of IVUSAcquisition.
	 */
	public void setIVUSAcquisition(EnumIVUSAcquisition enumIVUSAcquisition)
	{
		/*Accept IVUS Acquisition in String format of type CS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		String strIVUSAcquisition = enumIVUSAcquisition.getValue();
		addDataElement(0x0018, 0x3100, DicomConstants.DCM_DT_CS, strIVUSAcquisition);
	}
	
	/**
	 * Return a term that describes the method used to acquire the IVUS Images.
	 * Defined Terms: MOTOR_PULLBACK,MANUAL_PULLBACK,SELECTIVE,GATED_PULLBACK
	 * 
	 * @return String - String value of IVUS Acquisition.
	 */
	public String getIVUSAcquisition()
	{
		Object objectValue = getValue(0x0018, 0x3100);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * Required if IVUS Acquisition (0018,3100) value is MOTOR_PULLBACK. Specified in units of mm/sec.
	 * 
	 * @param strIVUSPullbackRate String value of IVUS Pullback Rate.
	 */
	public void setIVUSPullbackRate(String strIVUSPullbackRate)
	{
		/*Accept IVUS Pullback Rate in String format of type DS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0018, 0x3101, DicomConstants.DCM_DT_DS, strIVUSPullbackRate);
	}
	
	/**
	 * Return IVUS Pull Back Rate Specified in units of mm/sec.
	 * 
	 * @return String - String value of IVUS Pullback Rate.
	 */
	public String getIVUSPullbackRate()
	{
		Object objectValue = getValue(0x0018, 0x3101);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * Required if IVUS Acquisition (0018,3100) value is GATED_PULLBACK. Specified in units of mm/beat.
	 * 
	 * @param strIVUSGatedRate String value of IVUS Gated Rate.
	 */
	public void setIVUSGatedRate(String strIVUSGatedRate)
	{
		/*Accept IVUS Gated Rate in String format of type DS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0018, 0x3102, DicomConstants.DCM_DT_DS, strIVUSGatedRate);
	}
	
	/**
	 * Return IVUS Gated Rate Specified in units of mm/beat.
	 * 
	 * @return String - String value of IVUS Gated Rate.
	 */
	public String getIVUSGatedRate()
	{
		Object objectValue = getValue(0x0018, 0x3102);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * Required if IVUS Acquisition (0018,3100) value is MOTOR_PULLBACK or GATED_PULLBACK.
	 * 
	 * @param strIVUSPullbackStartFrameNumber String value of IVUS Pullback Start Frame Number.
	 */
	public void setIVUSPullbackStartFrameNumber(String strIVUSPullbackStartFrameNumber)
	{
		/*Accept IVUS Pullback Start Frame Number in String format of type IS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0018, 0x3103, DicomConstants.DCM_DT_IS, strIVUSPullbackStartFrameNumber);
	}
	
	/**
	 * Return IVUS Pullback Start Frame Number.
	 * 
	 * @return String - String value of IVUS Pullback Start Frame Number.
	 */
	public String getIVUSPullbackStartFrameNumber()
	{
		Object objectValue = getValue(0x0018, 0x3103);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * Required if IVUS Acquisition (0018,3100) value is MOTOR_PULLBACK or GATED_PULLBACK.
	 * 
	 * @param strIVUSPullbackStopFrameNumber String value of IVUS Pullback Stop Frame Number.
	 */
	public void setIVUSPullbackStopFrameNumber(String strIVUSPullbackStopFrameNumber)
	{
		/*Accept IVUS Pullback Stop Frame Number in String format of type IS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Hash Map
		 */
		addDataElement(0x0018, 0x3104, DicomConstants.DCM_DT_IS, strIVUSPullbackStopFrameNumber);
	}
	
	/**
	 * Return IVUS Pullback Stop Frame Number.
	 * 
	 * @return String - String value of IVUS Pullback Stop Frame Number.
	 */
	public String getIVUSPullbackStopFrameNumber()
	{
		Object objectValue = getValue(0x0018, 0x3104);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	
	
	/**
	 * This method returns the collection of keys representing the DataElements that belong to this module.
	 * 
	 * @return Collection of Tags - returns the collection containing the tags
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}

}
