/*******************************************************************************
 *  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.patient;
import java.io.IOException;
import java.util.Locale;


import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NormalizedIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.PatientDemographicModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.PatientIdentificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.PatientMedicalModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.PatientRelationshipModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.enums.EnumModuleNames;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidIODException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;

import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;

/**
 * The Patient Information Object Definition is an abstraction of the information describing a patient
 * upon whom medical imaging services are performed. The Patient IOD is tailored by Service
 * Class Definitions that use services which facilitate the exchange of patient-related information
 * between DICOM Application Entities.
 * 
 */
public class PatientIOD extends NormalizedIOD 
{
	

	/**
	 * The Constant PATIENT_IOD.
	 */
	public static final String PATIENT_IOD = "PATIENT IOD";


	/**
	 * The obj sop common module.
	 */
	SOPCommonModule objSOPCommonModule = null;

	/**
	 * The obj patient relationship module.
	 */
	PatientRelationshipModule objPatientRelationshipModule = null;

	/**
	 * The obj patient identification module.
	 */
	PatientIdentificationModule objPatientIdentificationModule = null;

	/**
	 * The obj patient demographic module.
	 */
	PatientDemographicModule objPatientDemographicModule = null;

	/**
	 * The obj patient medical module.
	 */
	PatientMedicalModule objPatientMedicalModule = null;

	/**
	 * Instantiates a new patient iod.
	 */
	public PatientIOD()
	{
		super();
	}

	/**
	 * This constructor initializes the TransferSyntax  in its superclass IOD.
	 * 
	 * @param objITransferSyntax_ - specifies the DICOM Standard defined TransfeSyntax.
	 */
	public PatientIOD(ITransferSyntax objITransferSyntax_)
	{
		super(objITransferSyntax_);
	}

	/**
	 * This constructor initializes the  DictionaryReader
	 * in its superclass IOD.
	 * 
	 * @param objIDictionaryReader_ - specifies the DICOM Dictionary reader to be used .
	 */
	public PatientIOD(IDictionaryReader objIDictionaryReader_)
	{
		super(objIDictionaryReader_);
	}

	/**
	 * This constructor accepts the DataSet from the user   in its superclass IOD.
	 * 
	 * @param objIDataSet_ - specifies the data set to be set in the IOD.
	 */
	public PatientIOD(IDataSet objIDataSet_)
	{
		super(objIDataSet_);
	}

	/**
	 * This constructor initializes the TransferSyntax and DictionaryReader in its super class IOD.
	 * 
	 * @param objITransferSyntax_ specifies the DICOM Standard defined TransfeSyntax.
	 * @param objIDictionaryReader_ specifies the DICOM Dictionary reader to be used .
	 */
	public PatientIOD(ITransferSyntax objITransferSyntax_,IDictionaryReader objIDictionaryReader_)
	{
		super(objITransferSyntax_,objIDictionaryReader_);
	}

	/**
	 * Creates PatientIOD IOD from a DICOM file containing PatientIOD DICOM data.
	 * @param strFileName DICOM File with absolute path.
	 * @throws IOException Exception caused due to wrong I/O operations. 
	 * @throws ParseException signals if some parse exceptions are occurred.
	 * @throws InvalidIODException signals if some IOD exceptions are occurred.
	 */
	public PatientIOD(String strFileName)throws IOException,InvalidIODException
	{
		super(strFileName);
		try
		{
			if(objIDataSet != null)
			{
				String strSOPClassUID = objIDataSet.getSOPClassUID();
				if(strSOPClassUID != null)
				{
					if(!strSOPClassUID.equals(EnumSOPClassUIDs.PATIENTIOD.getValue()))
					{
						throw new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "InvalidIOD.FileNotMatched", new Locale(DicomConfig.createInstance().getLocale())) + PATIENT_IOD);
					}
				}
				strSOPClassUID = null;
			}
		}
		catch(InvalidIODException iie)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(iie.getMessage());
			}
			throw iie;
		}
	}

	/**
	 * This method returns the SOPCommonModule with preinitializing it.
	 * 
	 * @return - instance of {@link SOPCommonModule}.
	 */
	public SOPCommonModule getSOPCommonModule()
	{
		if(objSOPCommonModule == null)
		{
			objSOPCommonModule = new SOPCommonModule(objIDataSet);
			return objSOPCommonModule;
		}
		return objSOPCommonModule;
	}

	/**
	 * This method returns the PatientRelationshipModule with preinitializing it.
	 * 
	 * @return - instance of {@link PatientRelationshipModule}.
	 */
	public PatientRelationshipModule getPatientRelationshipModule()
	{
		if(objPatientRelationshipModule == null)
		{
			objPatientRelationshipModule = new PatientRelationshipModule(objIDataSet);
			return objPatientRelationshipModule;
		}
		return objPatientRelationshipModule;
	}

	/**
	 * This method returns the PatientDemographicModule with preinitializing it.
	 * 
	 * @return - instance of {@link PatientDemographicModule}.
	 */
	public PatientDemographicModule getPatientDemographicModule()
	{
		if(objPatientDemographicModule == null)
		{
			objPatientDemographicModule = new PatientDemographicModule(objIDataSet);
			return objPatientDemographicModule;
		}
		return objPatientDemographicModule;
	}

	/**
	 * This method returns the PatientIdentificationModule with preinitializing it.
	 * 
	 * @return - instance of {@link PatientIdentificationModule}.
	 */
	public PatientIdentificationModule getPatientIdentificationModule()
	{
		if(objPatientIdentificationModule == null)
		{
			objPatientIdentificationModule = new PatientIdentificationModule(objIDataSet);
			return objPatientIdentificationModule;
		}
		return objPatientIdentificationModule;
	}

	/**
	 * This method returns the PatientMedicalModule with preinitializing it.
	 * 
	 * @return - instance of {@link PatientMedicalModule}.
	 */
	public PatientMedicalModule getPatientMedicalModule()
	{
		if(objPatientMedicalModule == null)
		{
			objPatientMedicalModule = new PatientMedicalModule(objIDataSet);
			return objPatientMedicalModule;
		}
		return objPatientMedicalModule;
	}
	/**
	 *  Dumps the Name of IOD,Module and corresponding Attributs of Module.
	 *  @param objIDicomStream - the Stream on which the Data is to be written.
	 *  @throws IOException - Exception is thrown indicating runtime failure of writing operation.
	 */
	public void dumpDICOMInfo(IDicomStream objIDicomStream) throws IOException
	{
		objIDicomStream.writeString(PATIENT_IOD+ "\n");
		super.dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getSOPCommonModule().getClass().getSimpleName() + "\n");
		getSOPCommonModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getPatientRelationshipModule().getClass().getSimpleName() + "\n");
		getPatientRelationshipModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getPatientIdentificationModule().getClass().getSimpleName() + "\n");
		getPatientIdentificationModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getPatientDemographicModule().getClass().getSimpleName() + "\n");
		getPatientDemographicModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getPatientMedicalModule().getClass().getSimpleName() + "\n");
		getPatientMedicalModule().dumpDICOMInfo(objIDicomStream);
	}

	/**
	 * Checks whether the Module is present in the IOD or not.
	 * @param strModuleName String value of module name 
	 * @return boolean - true if the module is present in the IOD, else false is returned.
	 */
	public boolean isModulePresent(String strModuleName)
	{
		if(!super.isModulePresent(strModuleName))
		{
			if(strModuleName == null || strModuleName.equals(""))
				return false;
			if(strModuleName.equalsIgnoreCase(EnumModuleNames.SOP_COMMON_MODULE.getValue()) && isModulePresent(SOPCommonModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.PATIENT_RELATIONSHIP_MODULE.getValue()) && isModulePresent(PatientRelationshipModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.PATIENT_IDENTIFICATION_MODULE.getValue()) && isModulePresent(PatientIdentificationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.PATIENT_DEMOGRAPHIC_MODULE.getValue()) && isModulePresent(PatientDemographicModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.PATIENT_MEDICAL_MODULE.getValue()) && isModulePresent(PatientMedicalModule.objSingleCollection))
				return true;
			else
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger = DicomLogger.getLogger();
					objDicomLogger.log(this.getClass().getName() + " : " +ResourceBundleWrapper.getValue("status","ModuleNotPresent",new Locale(DicomConfig.createInstance().getLocale())) + strModuleName);
				}
				return false;
			}
		}
		return true;
	}
}
