/*******************************************************************************
 *  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.AdmittingDiagnosesCodeSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.DicomV3Factory;
import cdac.medinfo.sdk.dcm30_04.baselibs.IssuerOfAdmissionIDSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.IssuerofServiceEpisodeIDSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.PatientsSizeCodeSequence;
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.EnumPatientsSexNeutered;

import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
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;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

/**
 \* The class PatientStudyModule.
 \* This module specifies Attributes that provide information about the Patient at the time the Study
 * was performed.
 */
public class PatientStudyModule extends Module   
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;


	/**
	 * Instantiates a new patient study module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public PatientStudyModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0008,0x1080));
		objSingleCollection.add(new Tag(0x0008,0x1084));
		objSingleCollection.add(new Tag(0x0010,0x1010));
		objSingleCollection.add(new Tag(0x0010,0x1020));
		objSingleCollection.add(new Tag(0x0010,0x1030));
		objSingleCollection.add(new Tag(0x0010,0x2180));
		objSingleCollection.add(new Tag(0x0010,0x21B0));
		objSingleCollection.add(new Tag(0x0010,0x1021));
		objSingleCollection.add(new Tag(0x0038,0x0010));
		objSingleCollection.add(new Tag(0x0038,0x0014));
		objSingleCollection.add(new Tag(0x0038,0x0060));
		objSingleCollection.add(new Tag(0x0038,0x0064));
		objSingleCollection.add(new Tag(0x0038,0x0062));
		objSingleCollection.add(new Tag(0x0010,0x2203));
	}

	/**
	 * Sets the Description of the admitting diagnosis (diagnoses)in Patient Study Module.
	 * The function accepts a String Array as and creates the DE based on the transfer syntax and
	 * populates the DE with right values.
	 * This DE is then added to the Module Data Set
	 * 
	 * @param arrStrAdmittingDiagnosesDescription :	Description of the admitting diagnosis (diagnoses)
	 */	
	public void setAdmittingDiagnosesDescription(String[] arrStrAdmittingDiagnosesDescription)
	{
		/* Accept Admitting Diagnoses Description in String Array format of type CS.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		addDataElement(0x0008, 0x1080, DicomConstants.DCM_DT_LO, arrStrAdmittingDiagnosesDescription);
	}

	/**
	 * Sets the Age of the Patient in Patient Study Module.
	 * The function accepts a String as Value and creates the DE based on the transfer syntax and
	 * populates the DE with right values.
	 * This DE is then added to the Module Data Set
	 * 
	 * @param strPatientAge : Age of the Patient
	 */
	public void setPatientAge(String strPatientAge)
	{
		/* Accept the Patient Age value in String format of type AS.
		 * 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(0x0010, 0x1010, DicomConstants.DCM_DT_AS, strPatientAge);
	}

	/**
	 * Sets the Patient Size in Patient Study Module.
	 * The function accepts a String as Value and creates the DE based on the transfer syntax and
	 * populates the DE with right values.
	 * This DE is then added to the Module Data Set
	 * 
	 * @param strPatientSize : Length or size of the Patient, in meters.
	 */
	public void setPatientSize(String strPatientSize)
	{
		/*Accept the Patient Size 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 Data Set
		 */
		addDataElement(0x0010, 0x1020, DicomConstants.DCM_DT_DS, strPatientSize);		
	}

	/**
	 * Sets the Patient Weight in Patient Study Module.
	 * The function accepts a String as Value and creates the DE based on the transfer syntax and
	 * populates the DE with right values.
	 * This DE is then added to the Module Data Set
	 * 
	 * @param strPatientWeight : Weight of the Patient, in kilograms.
	 */
	public void setPatientWeight(String strPatientWeight)
	{
		/*Accept the Patient Weight 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 Data Set
		 */
		addDataElement(0x0010, 0x1030, DicomConstants.DCM_DT_DS, strPatientWeight);	
	}

	/**
	 * Sets the Patient's Occupation in Patient Study Module.
	 * The function accepts a String as Value and creates the DE based on the transfer syntax and
	 * populates the DE with right values.
	 * This DE is then added to the Module Data Set
	 * 
	 * @param strOccupation : Occupation of the Patient.
	 */
	public void setOccupation(String strOccupation)
	{
		/* Accept the Patient Occupation 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 Data Set
		 */
		addDataElement(0x0010, 0x2180, DicomConstants.DCM_DT_SH, strOccupation);
	}

	/**
	 * Sets the Additional Patient History in Patient Study Module.
	 * 
	 * @param strAdditionalPatientHistory : Additional information about the Patient's medical history.
	 */
	public void setAdditionalPatientHistory(String strAdditionalPatientHistory)
	{
		/* Accept Additional Patient History in String Array format of type LT.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		addDataElement(0x0010, 0x21B0, DicomConstants.DCM_DT_LT, strAdditionalPatientHistory);
	}
	
	/**
	 * Sets Admission ID.
	 * Identifier of the visit as assigned by the health care provider
	 * @param strAdmissionID String value of Admission ID.
	 */
	public void setAdmissionID(String strAdmissionID)
	{
		/* Accept Admission ID String format of type LO.
		 * 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(0x0038, 0x0010, DicomConstants.DCM_DT_LO, strAdmissionID);
	}

	/**
	 * Sets Identifier of the Service Episode as assigned by the healthcare provider.
	 * 
	 * @param strServiceEpisodeID String value of Service Episode ID
	 */
	public void setServiceEpisodeID(String strServiceEpisodeID)
	{
		/*Accept name of Service Episode ID in String format of type LO.
		 * 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(0x0038, 0x0060, DicomConstants.DCM_DT_LO, strServiceEpisodeID);
	}
	
	/**
	 * Sets Description of the type of service episode.
	 * 
	 * @param strServiceEpisodeDescription String value of Service Episode Description
	 */
	public void setServiceEpisodeDescription(String strServiceEpisodeDescription)
	{
		/*Accept Service Episode Description in String format of type LO.
		 * 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(0x0038, 0x0062, DicomConstants.DCM_DT_LO, strServiceEpisodeDescription);
	}
	
	/**
	 * Sets Patients Sex Neutered.
	 * 
	 * @param enumPatientsSexNeutered - enum value of Patients Sex Neutered.
	 */
	public void setPatientsSexNeutered(EnumPatientsSexNeutered enumPatientsSexNeutered)
	{
		/* Accept the Patients Sex Neutered value 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
		 */
		addDataElement(0x0010, 0x2203, DicomConstants.DCM_DT_CS, enumPatientsSexNeutered);
	}
	
	/**
	 * 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 Boolean - status indicating success or failure
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{	
			if(isPresent(0x0008,0x1084))
				getAdmittingDiagnosesCodeSequence().validateForModule();
		}
		catch(ValidationException ve)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ve.getMessage());

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

		}
		return true;
	}

	/**
	 * This method Returns provides the pre-initialized object of Admitting Diagnoses Code Sequence
	 * return - object of Admitting Diagnoses Code Sequence. If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 * 
	 * @return - object of Admitting Diagnoses Code Sequence.
	 */
	public AdmittingDiagnosesCodeSequence getAdmittingDiagnosesCodeSequence()
	{
		AdmittingDiagnosesCodeSequence objAdmittingDiagnosesCodeSequence = (AdmittingDiagnosesCodeSequence)getDicomValue(0x0008, 0x1084);
		if(objAdmittingDiagnosesCodeSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objAdmittingDiagnosesCodeSequence = (AdmittingDiagnosesCodeSequence)objDicomV3Factory.createSequence(0x0008, 0x1084, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0008, 0x1084, DicomConstants.DCM_DT_SQ, objAdmittingDiagnosesCodeSequence.getValue());
		}
		return objAdmittingDiagnosesCodeSequence;
	}	

	/**
	 * Retrieves the Array of String values of Description of the admitting diagnosis(diagnoses).
	 * 
	 * @return String[] - String array of Admitting Diagnoses Description
	 */
	public String[] getAdmittingDiagnosesDescription()
	{
		Object objValue = objIDataSet.getValue(0x0008, 0x1080);
		if(objValue != null)
			return (String[]) objValue;
		return null;
	}

	/**
	 * Retrieves the String value of the attribute Patient Age.
	 * 
	 * @return String - String value of Patient Age.
	 */
	public String getPatientAge()
	{
		Object objValue = objIDataSet.getValue(0x0010, 0x1010);
		if(objValue != null)
			return objValue.toString();
		return null;
	} 

	/**
	 * Retrieves the String value of the attribute Patient Size.
	 * 
	 * @return String - String value of Patient Size.
	 */
	public String getPatientSize()
	{
		Object objValue = objIDataSet.getValue(0x0010, 0x1020);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves the String value of the attribute Patient Weight.
	 * 
	 * @return String - String value of Patient Weight.
	 */
	public String getPatientWeight()
	{
		Object objValue = objIDataSet.getValue(0x0010, 0x1030);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves the String value of the attribute Patient Occupation.
	 * 
	 * @return String - String value of Patient Occupation.
	 */
	public String getOccupation()
	{
		Object objValue = objIDataSet.getValue(0x0010, 0x2180);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves the String value of the attribute Patient AdditionalPatientHistory.
	 * 
	 * @return String - String value of Additional Patient History.
	 */
	public String getAdditionalPatientHistory()
	{
		Object objValue = objIDataSet.getValue(0x0010, 0x21B0);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * This method provides the pre-initialized object of Patients Size Code Sequence.
	 * 
	 * @return - object of Patients Size Code Sequence. If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 */
	public PatientsSizeCodeSequence getPatientsSizeCodeSequence()
	{
		PatientsSizeCodeSequence objPatientsSizeCodeSequence = (PatientsSizeCodeSequence)getDicomValue(0x0010, 0x1021);
		if(objPatientsSizeCodeSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objPatientsSizeCodeSequence = (PatientsSizeCodeSequence)objDicomV3Factory.createSequence(0x0010, 0x1021, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0010, 0x1021, DicomConstants.DCM_DT_SQ, objPatientsSizeCodeSequence.getValue());
		}
		return objPatientsSizeCodeSequence;
	}
	
	/**
	 * Retrieves Admission ID.
	 * Identifier of the visit as assigned by the health care provider
	 * @return String - String value of Admission ID.
	 */
	public String getAdmissionID()
	{
		Object objValue = getValue(0x0038,0x0010);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * This method provides the pre-initialized object of Issuer Of Admission ID Sequence.
	 * 
	 * @return - object of Issuer Of Admission ID Sequence. If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 */
	public IssuerOfAdmissionIDSequence getIssuerOfAdmissionIDSequence()
	{
		IssuerOfAdmissionIDSequence objIssuerOfAdmissionIDSequence = (IssuerOfAdmissionIDSequence)getDicomValue(0x0038, 0x0014);
		if(objIssuerOfAdmissionIDSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objIssuerOfAdmissionIDSequence = (IssuerOfAdmissionIDSequence)objDicomV3Factory.createSequence(0x0038, 0x0014, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0038, 0x0014, DicomConstants.DCM_DT_SQ, objIssuerOfAdmissionIDSequence.getValue());
		}
		return objIssuerOfAdmissionIDSequence;
	}

	/**
	 * Retrieves Service Episode ID.
	 * 
	 * @return String - String value of Service Episode ID
	 */
	public String getServiceEpisodeID()
	{
		Object objectValue = getValue(0x0038, 0x0060);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * This method provides the pre-initialized object of Issuer of Service Episode ID Sequence.
	 * 
	 * @return - object of Issuer of Service Episode ID Sequence. If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence
	 */
	public IssuerofServiceEpisodeIDSequence getIssuerofServiceEpisodeIDSequence()
	{
		IssuerofServiceEpisodeIDSequence objIssuerofServiceEpisodeIDSequence = (IssuerofServiceEpisodeIDSequence)getDicomValue(0x0038, 0x0064);
		if(objIssuerofServiceEpisodeIDSequence == null)
		{
			/*
			 * If the sequence object is not present in the DataSet, a new sequence object is created and
			 * the TransferSyntax and DictionaryReader is set into it as it is set in the Module. This ensures that 
			 * the DataElements getting added in the DataSet follow the same TransferSyntax and use the same
			 * DictionaryReader object as that of the module.
			 */
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			objIssuerofServiceEpisodeIDSequence = (IssuerofServiceEpisodeIDSequence)objDicomV3Factory.createSequence(0x0038, 0x0064, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0038, 0x0064, DicomConstants.DCM_DT_SQ, objIssuerofServiceEpisodeIDSequence.getValue());
		}
		return objIssuerofServiceEpisodeIDSequence;
	}
	
	/**
	 * Retrieves Service Episode Description.
	 * 
	 * @return String - String value of Service Episode Description
	 */
	public String getServiceEpisodeDescription()
	{
		Object objectValue = getValue(0x0038, 0x0062);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * Retrieves Patients Sex Neutered.
	 * 
	 * @return String - String value of Patients Sex Neutered.
	 */
	public String getPatientsSexNeutered()
	{
		Object objectValue = getValue(0x0010, 0x2203);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * This method returns the vector containing the list of DataElements belonging to the CT
	 * Image Module specified by DICOM Standard.
	 * 
	 * @return collection - custom collection containing the list of DataElements belonging to the CT Image Module
	 */
	public  ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
