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

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.InterpretationApprovalModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.InterpretationIdentificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.InterpretationRecordingModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.InterpretationRelationshipModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.InterpretationStateModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.InterpretationTranscriptionModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NormalizedIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.enums.EnumModuleNames;
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 Interpretation Information Object Definition is an abstraction of the information resulting from
 * a single interpretation of a diagnostic imaging study.
 * 
 * 
 */
public class InterpretationIOD extends NormalizedIOD 
{
	

	/**
	 * The Constant INTERPRETATION_IOD
	 */
	public static final String INTERPRETATION_IOD = "Interpretation IOD";

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

	/**
	 * The obj interpretation relationship module.
	 */
	InterpretationRelationshipModule objInterpretationRelationshipModule = null;

	/**
	 * The obj interpretation identification module.
	 */
	InterpretationIdentificationModule objInterpretationIdentificationModule = null;

	/**
	 * The obj interpretation state module.
	 */
	InterpretationStateModule objInterpretationStateModule = null;

	/**
	 * The obj interpretation recording module.
	 */
	InterpretationRecordingModule objInterpretationRecordingModule = null;

	/**
	 * The obj interpretation transcription module.
	 */
	InterpretationTranscriptionModule objInterpretationTranscriptionModule = null;

	/**
	 * The obj interpretation approval module.
	 */
	InterpretationApprovalModule objInterpretationApprovalModule = null;

	/**
	 * Instantiates a new interpretation iod.
	 */
	public InterpretationIOD()
	{
		super();
	}

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

	/**
	 * This constructor initializes the  DictionaryReader
	 * in its superclass IOD.
	 * 
	 * @param objIDictionaryReader_ - specifies the DICOM Dictionary reader to be used .
	 */
	public InterpretationIOD(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 InterpretationIOD(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 InterpretationIOD(ITransferSyntax objITransferSyntax_,IDictionaryReader objIDictionaryReader_)
	{
		super(objITransferSyntax_,objIDictionaryReader_);
	}

	/**
	 * Creates InterpretationIOD IOD from a DICOM file containing InterpretationIOD 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 InterpretationIOD(String strFileName)throws IOException,InvalidIODException
	{
		super(strFileName);
	}

	/**
	 * Returns the InterpretationApprovalModule with preinitializing it.
	 * 
	 * @return instance of {@link InterpretationApprovalModule}.
	 */
	public InterpretationApprovalModule getInterpretationApprovalModule() 
	{
		if(objInterpretationApprovalModule == null)
		{
			objInterpretationApprovalModule = new InterpretationApprovalModule(objIDataSet);
			return objInterpretationApprovalModule;
		}
		return objInterpretationApprovalModule;
	}

	/**
	 * Returns the InterpretationIdentificationModule with preinitializing it.
	 * 
	 * @return instance of {@link InterpretationIdentificationModule}.
	 */
	public InterpretationIdentificationModule getInterpretationIdentificationModule() 
	{
		if(objInterpretationIdentificationModule == null)
		{
			objInterpretationIdentificationModule = new InterpretationIdentificationModule(objIDataSet);
			return objInterpretationIdentificationModule;
		}
		return objInterpretationIdentificationModule;
	}

	/**
	 * Returns the InterpretationRecordingModule with preinitializing it.
	 * 
	 * @return instance of {@link InterpretationRecordingModule}.
	 */
	public InterpretationRecordingModule getInterpretationRecordingModule() 
	{
		if(objInterpretationRecordingModule == null)
		{
			objInterpretationRecordingModule = new InterpretationRecordingModule(objIDataSet);
			return objInterpretationRecordingModule;
		}
		return objInterpretationRecordingModule;
	}

	/**
	 * Returns the InterpretationRelationshipModule with preinitializing it.
	 * 
	 * @return instance of {@link InterpretationRelationshipModule}.
	 */
	public InterpretationRelationshipModule getInterpretationRelationshipModule() 
	{
		if(objInterpretationRelationshipModule == null)
		{
			objInterpretationRelationshipModule = new InterpretationRelationshipModule(objIDataSet);
			return objInterpretationRelationshipModule;
		}
		return objInterpretationRelationshipModule;
	}

	/**
	 * Returns the InterpretationTranscriptionModule with preinitializing it.
	 * 
	 * @return instance of {@link InterpretationTranscriptionModule}.
	 */
	public InterpretationStateModule getInterpretationStateModule() 
	{
		if(objInterpretationStateModule == null)
		{
			objInterpretationStateModule = new InterpretationStateModule(objIDataSet);
			return objInterpretationStateModule;
		}
		return objInterpretationStateModule;
	}

	/**
	 * Returns the InterpretationTranscriptionModule with preinitializing it.
	 * 
	 * @return instance of {@link InterpretationTranscriptionModule}.
	 */
	public InterpretationTranscriptionModule getInterpretationTranscriptionModule() 
	{
		if(objInterpretationTranscriptionModule == null)
		{
			objInterpretationTranscriptionModule = new InterpretationTranscriptionModule(objIDataSet);
			return objInterpretationTranscriptionModule;
		}
		return objInterpretationTranscriptionModule;
	}

	/**
	 * 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;
	}
	/**
	 *  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(INTERPRETATION_IOD+ "\n");
		super.dumpDICOMInfo(objIDicomStream);

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

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

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

		objIDicomStream.writeString("\t" + getInterpretationStateModule().getClass().getSimpleName() + "\n");
		getInterpretationStateModule().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.INTERPRETATION_RELATIONSHIP_MODULE.getValue()) && isModulePresent(InterpretationRelationshipModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.INTERPRETATION_STATE_MODULE.getValue()) && isModulePresent(InterpretationStateModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.INTERPRETATION_IDENTIFICATION_MODULE.getValue()) && isModulePresent(InterpretationIdentificationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.INTERPRETATION_RECORDING_MODULE.getValue()) && isModulePresent(InterpretationRecordingModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.INTERPRETATION_TRANSCRIPTION_MODULE.getValue()) && isModulePresent(InterpretationTranscriptionModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.INTERPRETATION_APPROVAL_MODULE.getValue()) && isModulePresent(InterpretationApprovalModule.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;
	}
}
