/*******************************************************************************
 *  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.EnumPatientFrameofReferenceSource;
import cdac.medinfo.sdk.dcm30_04.enums.EnumUltrasoundAcquisitionGeometry;

import cdac.medinfo.sdk.dcm30_04.enums.EnumVolumetoTransducerRelationship;
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;

//TODO: Auto-generated Javadoc
/**
\* This module specifies the Attributes that describe Ultrasound Frame of Reference Module.
*/
public class UltrasoundFrameofReferenceModule extends Module  
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


	/**
	 * Creates Ultrasound Frameof Reference Module with default initialization.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public UltrasoundFrameofReferenceModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0020,0x9312));
		objSingleCollection.add(new Tag(0x0020,0x9307));
		objSingleCollection.add(new Tag(0x0020,0x9308));
		objSingleCollection.add(new Tag(0x0020,0x930B));
		objSingleCollection.add(new Tag(0x0020,0x9309));
		objSingleCollection.add(new Tag(0x0020,0x930C));
		objSingleCollection.add(new Tag(0x0020,0x9313));
		objSingleCollection.add(new Tag(0x0020,0x930A));
	}

	/**
	 * 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(0x0020,0x9312))
				throw new MandatoryMissingException("0020,9312");
			if(!isPresent(0x0020,0x9307))
				throw new MandatoryMissingException("0020,9307");
			if(!isPresent(0x0020,0x9309))
				throw new MandatoryMissingException("0020,9309");
			
			if(isPresent(0x0020,0x9307) && getUltrasoundAcquisitionGeometry().equalsIgnoreCase(EnumUltrasoundAcquisitionGeometry.APEX.getValue()))
				if(!isPresent(0x0020,0x9308))
					throw new ConditionFailedException("0x0020,0x9308");
			
			if(isPresent(0x0020,0x0032) || isPresent(0x0020,0x0037))
				if(!isPresent(0x0020,0x930C))
					throw new ConditionFailedException("0x0020,0x930C");
			
			if(isPresent(0x0020,0x930C) && getPatientFrameofReferenceSource().equalsIgnoreCase(EnumPatientFrameofReferenceSource.TABLE.getValue())){
				if(!isPresent(0x0020,0x9313))
					throw new ConditionFailedException("0x0020,0x9313");
				if(!isPresent(0x0020,0x930A))
					throw new ConditionFailedException("0x0020,0x930A");
			}
		}
		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 Volume Frame of Reference UID.
	 * Uniquely identifies this Volume Frame of Reference.
	 * @param strVolumeFrameofReferenceUID - Volume Frame of Reference UID.
	 */
	public void setVolumeFrameofReferenceUID(String strVolumeFrameofReferenceUID)
	{
		/*Accept Volume Frame of Reference UID for a Series in String format of type UI.
		 * 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(0x0020, 0x9312, DicomConstants.DCM_DT_UI, strVolumeFrameofReferenceUID);
	}


	/**
	 * Retrieves Volume Frame of Reference UID.
	 * Uniquely identifies this Volume Frame of Reference.
	 * @return - returns the Volume Frame of Reference UID
	 */
	public String getVolumeFrameofReferenceUID()
	{
		Object objectValue = getValue(0x0020, 0x9312);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * Sets Ultrasound Acquisition Geometry.
	 * Characteristic of the ultrasound acquisition geometry.
	 * Enumerated Values:
	 * 1) APEX
	 * 
	 * @param enumUltrasoundAcquisitionGeometry the enum Ultrasound Acquisition Geometry
	 */
	public void setUltrasoundAcquisitionGeometry(EnumUltrasoundAcquisitionGeometry  enumUltrasoundAcquisitionGeometry)
	{
		/* Accept Ultrasound Acquisition Geometry 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 Data Set
		 */
		addDataElement(0x0020, 0x9307, DicomConstants.DCM_DT_CS, enumUltrasoundAcquisitionGeometry.getValue());
	}

	/**
	 * Retrieves Ultrasound Acquisition Geometry.
	 * Characteristic of the ultrasound acquisition geometry.
	 * @return String - String value of Ultrasound Acquisition Geometry.
	 */ 
	public String getUltrasoundAcquisitionGeometry()
	{
		Object objValue = getValue(0x0020,0x9307);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Apex Position
	 * @param dblArrApexPosition Double value of Apex Position
	 */
	public void setApexPosition(double[] dblArrApexPosition)
	{
		/*Accept the Apex Position in double format of type FD.
		 * 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 Data Set
		 */
		addDataElement(0x0020, 0x9308, DicomConstants.DCM_DT_FD, dblArrApexPosition);
	}
	/**
	 * Retrieves the Apex Position.
	 * @return double - double value of Apex Position
	 */
	public double[] getApexPosition()
	{
		Object objValue = getValue(0x0020,0x9308);
		if(objValue != null)
			return (double[])objValue;
		return null;
	}
	
	/**
	 * Sets Volume to Transducer Relationship.
	 * Relationship between the transducer and the acquired volume.
	 * Enumerated Values:
	 * 1) FIXED
	 * 2) POSITION_VAR
	 * 3) ORIENTATION_VAR
	 * 4) VARIABLE
	 * 
	 * @param enumVolumetoTransducerRelationship the enumvalue Volume to Transducer Relationship
	 */
	public void setVolumetoTransducerRelationship(EnumVolumetoTransducerRelationship  enumVolumetoTransducerRelationship)
	{
		/* Accept Volume to Transducer Relationship 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 Data Set
		 */
		addDataElement(0x0020, 0x930B, DicomConstants.DCM_DT_CS, enumVolumetoTransducerRelationship.getValue());
	}

	/**
	 * Retrieves Volume to Transducer Relationship.
	 * Relationship between the transducer and the acquired volume.
	 * @return String - String value of Volume to Transducer Relationship.
	 */ 
	public String getVolumetoTransducerRelationship()
	{
		Object objValue = getValue(0x0020,0x930B);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Volume to Transducer Mapping Matrix
	 * @param dblArrVolumetoTransducerMappingMatrix Double value of Volume to Transducer Mapping Matrix
	 */
	public void setVolumetoTransducerMappingMatrix(double[] dblArrVolumetoTransducerMappingMatrix)
	{
		/*Accept the Volume to Transducer Mapping Matrix in double format of type FD.
		 * 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 Data Set
		 */
		addDataElement(0x0020, 0x9309, DicomConstants.DCM_DT_FD, dblArrVolumetoTransducerMappingMatrix);
	}
	/**
	 * Retrieves the Volume to Transducer Mapping Matrix.
	 * @return double - double value of Volume to Transducer Mapping Matrix
	 */
	public double[] getVolumetoTransducerMappingMatrix()
	{
		Object objValue = getValue(0x0020,0x9309);
		if(objValue != null)
			return (double[])objValue;
		return null;
	}
	
	/**
	 * Sets Patient Frame of Reference Source.
	 * Indicates how the supplied Image Position (Patient) (0020,0032)
	 * and ImageOrientation (Patient) (0020,0037) values are obtained.
	 * Enumerated Values:
	 * 1) TABLE
	 * 2) ESTIMATED
	 * 3) REGISTRATION
	 * 
	 * @param enumPatientFrameofReferenceSource the enumvalue Patient Frame of Reference Source
	 */
	public void setPatientFrameofReferenceSource(EnumPatientFrameofReferenceSource  enumPatientFrameofReferenceSource)
	{
		/* Accept Patient Frame of Reference Source 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 Data Set
		 */
		addDataElement(0x0020, 0x930C, DicomConstants.DCM_DT_CS, enumPatientFrameofReferenceSource.getValue());
	}

	/**
	 * Retrieves Patient Frame of Reference Source.
	 * @return String - String value of Patient Frame of Reference Source.
	 */ 
	public String getPatientFrameofReferenceSource()
	{
		Object objValue = getValue(0x0020,0x930C);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Table Frame of Reference UID.
	 * Uniquely identifies this Table Frame of Reference.
	 * @param strTableFrameofReferenceUID - Table Frame of Reference UID.
	 */
	public void setTableFrameofReferenceUID(String strTableFrameofReferenceUID)
	{
		/*Accept Table Frame of Reference UID in String format of type UI.
		 * 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(0x0020, 0x9313, DicomConstants.DCM_DT_UI, strTableFrameofReferenceUID);
	}


	/**
	 * Retrieves the Table Frame of Reference UID.
	 * Uniquely identifies this Table Frame of Reference.
	 * @return - returns the Table Frame of Reference UID
	 */
	public String getTableFrameofReferenceUID()
	{
		Object objectValue = getValue(0x0020, 0x9313);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * Sets the Volume to Table Mapping Matrix
	 * @param dblArrVolumetoTableMappingMatrix Double value of Volume to Table Mapping Matrix
	 */
	public void setVolumetoTableMappingMatrix(double[] dblArrVolumetoTableMappingMatrix)
	{
		/*Accept the Volume to Table Mapping Matrix in double format of type FD.
		 * 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 Data Set
		 */
		addDataElement(0x0020, 0x930A, DicomConstants.DCM_DT_FD, dblArrVolumetoTableMappingMatrix);
	}
	/**
	 * Retrieves the Volume to Table Mapping Matrix.
	 * @return double - double value of Volume to Table Mapping Matrix
	 */
	public double[] getVolumetoTableMappingMatrix()
	{
		Object objValue = getValue(0x0020,0x930A);
		if(objValue != null)
			return (double[])objValue;
		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;
	}


}