/*******************************************************************************
 *  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.DicomV3Factory;
import cdac.medinfo.sdk.dcm30_04.baselibs.PatientClinicalTrialParticipationSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.PertinentDocumentsSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.PertinentResourcesSequence;
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.enums.EnumPregnancyStatus;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSmokingStatus;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

// TODO: Auto-generated Javadoc
/**
 \* This module defines the Attributes relevant to a patient's medical state or history.
 * 
 * 
 */
public class PatientMedicalModule extends Module  
{

 
 /**
  * The obj single collection.
  */
 public static ISingleCollection<ITag> objSingleCollection = null;
	 

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

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0010,0x2000));
		objSingleCollection.add(new Tag(0x0010,0x2110));
		objSingleCollection.add(new Tag(0x0010,0x21A0));
		objSingleCollection.add(new Tag(0x0010,0x21B0));
		objSingleCollection.add(new Tag(0x0010,0x21C0));
		objSingleCollection.add(new Tag(0x0010,0x21D0));
		objSingleCollection.add(new Tag(0x0010,0x2203));
		objSingleCollection.add(new Tag(0x0038,0x0050));
		objSingleCollection.add(new Tag(0x0038,0x0500));
		objSingleCollection.add(new Tag(0x0038,0x0100));
		objSingleCollection.add(new Tag(0x0038,0x0101));
		objSingleCollection.add(new Tag(0x0038,0x0502));
	}

	/* (non-Javadoc)
	 * @see cdac.medinfo.sdk.dcm30_04.dicommodel.modules.Module#validateModule()
	 */
	public boolean validateModule()throws InvalidModuleException
	{
		return true;
	}

	/**
	 * Sets Conditions to which medical staff should be alerted (e.g. contagious condition, drug allergies, etc.)
	 * 
	 * @param arrStrMedicalAlerts - String array of Medical Alerts.
	 */
	public void setMedicalAlerts(String[] arrStrMedicalAlerts)
	{
		/* Accept Medical Alerts in String Array format of type LO.
		 * 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, 0x2000, DicomConstants.DCM_DT_LO, arrStrMedicalAlerts);
	}

	/**
	 * Sets Description of prior reaction to contrast agents.
	 * 
	 * @param arrStrAllergies - String array of Contrast Allergies.
	 */
	public void setAllergies(String[] arrStrAllergies)
	{
		/* Accept Allergies in String Array format of type LO.
		 * 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, 0x2110, DicomConstants.DCM_DT_LO, arrStrAllergies);
	}

	/**
	 * Indicates whether patient smokes. Enumerated Values:YES, NO, UNKNOWN.
	 * 
	 * @param enumSmokingStatus - Enum value indicating whether patient smokes.
	 */
	public void setSmokingStatus(EnumSmokingStatus enumSmokingStatus)
	{
		/*Accept Smoking Status 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(0x0010, 0x21A0, DicomConstants.DCM_DT_CS, enumSmokingStatus.getValue());
	}

	/**
	 * Sets Additional information about the patient's medical history.
	 * 
	 * @param strAdditionalPatientHistory - String containing additional patient 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);
	}

	/**
	 * Describes pregnancy state of patient. Enumerated Values:0001 = not pregnant,0002 = possibly pregnant
	 * 0003 = definitely pregnant, 0004 = unknown
	 * 
	 * @param enumPregnancyStatus - Enum value indicating pregnancy state of patient.
	 */
	public void setPregnancyStatus(EnumPregnancyStatus enumPregnancyStatus)
	{
		/* Accept Pregnancy Status in String Array format of type US.
		 * 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, 0x21C0, DicomConstants.DCM_DT_US, enumPregnancyStatus.getValue());
	}

	/**
	 * Sets Date of onset of last menstrual period.
	 * 
	 * @param strLastMenstrualDate - String value of Last Menstrual Date.
	 */
	public void setLastMenstrualDate(String strLastMenstrualDate)
	{
		/*Accept Last Menstrual Date in String Array format of type DA.
		 * 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, 0x21D0, DicomConstants.DCM_DT_DA, strLastMenstrualDate);
	}

	/**
	 * 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);
	}
	
	/**
	 * Sets Medical and social needs (e.g. wheelchair, oxygen, non-English-speaking, etc.)
	 * 
	 * @param strSpecialNeeds - String value of Special Needs.
	 */
	public void setSpecialNeeds(String strSpecialNeeds)
	{
		/*Accept Special Needs in String Array format of type LO.
		 * 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(0x0038, 0x0050, DicomConstants.DCM_DT_LO, strSpecialNeeds);
	}

	/**
	 * Description of patient state (comatose, disoriented, vision impaired, etc.)
	 * 
	 * @param strPatientState - String value of Patient State.
	 */
	public void setPatientState(String strPatientState)
	{
		/*Accept Patient State in String Array format of type LO.
		 * 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(0x0038, 0x0500, DicomConstants.DCM_DT_LO, strPatientState);
	}

	/**
	 * Retrieves Conditions to which medical staff should be alerted.
	 * 
	 * @return String[] - String array of Medical Alerts.
	 */
	public String[] getMedicalAlerts()
	{
		Object objectValue = getValue(0x0010, 0x2000);
		if(objectValue!=null)
			return (String[])objectValue;
		return null;
	}

	/**
	 * Retrieves Description of prior reaction to contrast agents.
	 * 
	 * @return String[] - String array of Contrast Allergies
	 */
	public String[] getAllergies()
	{
		Object objectValue = getValue(0x0010, 0x2110);
		if(objectValue!=null)
			return (String[])objectValue;
		return null;
	}

	/**
	 * Retrieves whether patient smokes.
	 * 
	 * @return String - String value of Smoking Status.
	 */
	public String getSmokingStatus()
	{
		 Object objValue = getValue(0x0010,0x21A0);
   if(objValue!=null)
      return objValue.toString();
   return null;

	}

	/**
	 * Retrieves Additional Patient History.
	 * 
	 * @return String - String value of Additional Patient History.
	 */
	public String getAdditionalPatientHistory()
	{
		 Object objValue = getValue(0x0010,0x21B0);
   if(objValue!=null)
      return objValue.toString();
   return null;

	}

	/**
	 * Retrieves Pregnancy Status.
	 * 
	 * @return String - String value of Pregnancy Status.
	 */
	public String getPregnancyStatus()
	{
		 Object objValue = getValue(0x0010,0x21C0);
   if(objValue!=null)
      return objValue.toString();
   return null;

	}

	/**
	 * Retrieves Last Menstrual Date.
	 * 
	 * @return String - String value of Last Menstrual Date.
	 */
	public String getLastMenstrualDate()
	{
		 Object objValue = getValue(0x0010,0x21D0);
   if(objValue!=null)
      return objValue.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;
	}
	
	/**
	 * Retrieves Medical and social needs.
	 * 
	 * @return String - String value of Special Needs.
	 */
	public String getSpecialNeeds()
	{
		Object objectValue = getValue(0x0038, 0x0050);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Retrieves patient state.
	 * 
	 * @return String - String value of Patient State.
	 */
	public String getPatientState()
	{
		Object objectValue = getValue(0x0038, 0x0500);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}
	
	/**
	 * This method provides the pre-initialized object of Pertinent Documents Sequence.
	 * 
	 * @return - object of Pertinent Documents 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 PertinentDocumentsSequence getPertinentDocumentsSequence()
	{
		PertinentDocumentsSequence objPertinentDocumentsSequence = (PertinentDocumentsSequence)getDicomValue(0x0038, 0x0100);
		if(objPertinentDocumentsSequence == 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();
			objPertinentDocumentsSequence = (PertinentDocumentsSequence)objDicomV3Factory.createSequence(0x0038, 0x0100, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0038, 0x0100, DicomConstants.DCM_DT_SQ, objPertinentDocumentsSequence.getValue());
		}
		return objPertinentDocumentsSequence;
	}
	
	/**
	 * This method provides the pre-initialized object of Pertinent Resources Sequence.
	 * 
	 * @return - object of Pertinent Resources 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 PertinentResourcesSequence getPertinentResourcesSequence()
	{
		PertinentResourcesSequence objPertinentResourcesSequence = (PertinentResourcesSequence)getDicomValue(0x0038, 0x0101);
		if(objPertinentResourcesSequence == 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();
			objPertinentResourcesSequence = (PertinentResourcesSequence)objDicomV3Factory.createSequence(0x0038, 0x0101, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0038, 0x0101, DicomConstants.DCM_DT_SQ, objPertinentResourcesSequence.getValue());
		}
		return objPertinentResourcesSequence;
	}
	
	/**
	 * This method provides the pre-initialized object of Patient Clinical Trial Participation Sequence.
	 * 
	 * @return - object of Patient Clinical Trial Participation 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 PatientClinicalTrialParticipationSequence getPatientClinicalTrialParticipationSequence()
	{
		PatientClinicalTrialParticipationSequence objPatientClinicalTrialParticipationSequence = (PatientClinicalTrialParticipationSequence)getDicomValue(0x0038, 0x0502);
		if(objPatientClinicalTrialParticipationSequence == 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();
			objPatientClinicalTrialParticipationSequence = (PatientClinicalTrialParticipationSequence)objDicomV3Factory.createSequence(0x0038, 0x0502, objITransferSyntax, objIDictionaryReader);
			
			// This will add Data Element of SQ to the DataSet.
			addDataElement(0x0038, 0x0502, DicomConstants.DCM_DT_SQ, objPatientClinicalTrialParticipationSequence.getValue());
		}
		return objPatientClinicalTrialParticipationSequence;
	}

	/* 
	 * I have modified the collection type from collection to custom class
	 * This method is used by the Module Iterator to iterate through the DataSet and return only
	 * those elements that belong to the respective Module class whose this method is called.
	 */
	/**
	 * This method returns the collection of keys representing the DataElements
	 * that belong to this module.
	 * 
	 * @return collection of Tags - returns the collection containing the collection of tags
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
