package cdac.medinfo.sdk.dcm30_04.dicommodel.sr;

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.EnhancedGeneralEquipmentModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.ImageIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SRDocumentContentModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SRDocumentGeneralModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SynchronizationModule;
import cdac.medinfo.sdk.dcm30_04.enums.EnumModuleNames;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidIODException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
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 X-Ray Radiation Dose SR IOD is used to convey the exposure characteristics and dose from X-Rays generated by imaging devices.
 * @author saloni
 *
 */
public class XRayRadiationDoseSRIOD extends ImageIOD
{

	/**
	 * The Constant X_RAY_3D_CRANIOFACIAL_IMAGE_IOD.
	 */
	private static final String X_RAY_3D_RADIATION_DOSE_SR_IOD ="X-Ray Radiation Dose SR IOD";

	/**
	 * The obj Synchronization module.
	 */
	SynchronizationModule objSynchronizationModule= null;

	/**
	 * The obj Enhanced General Equipment module.
	 */
	EnhancedGeneralEquipmentModule objEnhancedGeneralEquipmentModule= null;
	
	/**
	 * The obj SR Document General module.
	 */
	SRDocumentGeneralModule objSRDocumentGeneralModule= null;
	
	/**
	 * The obj SR Document Content module.
	 */
	SRDocumentContentModule objSRDocumentContentModule= null;
	
	/**
	 * The obj sop common module.
	 */
	SOPCommonModule objSOPCommonModule = null;
	
	

	/**
	 * Instantiates a new XRay3DCraniofacialImageIOD.
	 */
	public XRayRadiationDoseSRIOD()
	{
		super();
	}

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

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

	/**
	 * Creates XRayRadiationDoseSRIOD from a DICOM file containing XRayRadiationDoseSRIOD 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 XRayRadiationDoseSRIOD(String strFileName)throws IOException,InvalidIODException
	{
		super(strFileName);
		try
		{
			if(objIDataSet != null)
			{
				String strSOPClassUID = objIDataSet.getSOPClassUID();
				if(strSOPClassUID != null)
				{
					if(!strSOPClassUID.equals(EnumSOPClassUIDs.XRAYRADIATIONDOSESRIOD.getValue()))
					{
						throw new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "InvalidIOD.FileNotMatched", new Locale(DicomConfig.createInstance().getLocale())) + X_RAY_3D_RADIATION_DOSE_SR_IOD);
					}
				}
				strSOPClassUID = null;
			}
		}
		catch(InvalidIODException iie)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(iie.getMessage());
			}
			throw iie;
		}
	}

	/**
	 * Retrieves pre-initialized instance of SynchronizationModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return SynchronizationModule - instance of {@link SynchronizationModule}.
	 */
	public SynchronizationModule getSynchronizationModule()
	{
		if(objSynchronizationModule == null)
		{
			objSynchronizationModule = new SynchronizationModule(objIDataSet);
			return objSynchronizationModule;
		}
		return objSynchronizationModule;
	}

	/**
	 * Retrieves pre-initialized instance of Enhanced General Equipment Module. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return EnhancedGeneralEquipmentModule - instance of {@link EnhancedGeneralEquipmentModule}.
	 */
	public EnhancedGeneralEquipmentModule getEnhancedGeneralEquipmentModule()
	{
		if(objEnhancedGeneralEquipmentModule == null)
		{
			objEnhancedGeneralEquipmentModule = new EnhancedGeneralEquipmentModule(objIDataSet);
			return objEnhancedGeneralEquipmentModule;
		}
		return objEnhancedGeneralEquipmentModule;
	}
	/**
	 * Retrieves pre-initialized instance of SRDocumentGeneralModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return SRDocumentGeneralModule - instance of {@link SRDocumentGeneralModule}.
	 */
	public SRDocumentGeneralModule getSRDocumentGeneralModule()
	{
		if(objSRDocumentGeneralModule == null)
		{
			objSRDocumentGeneralModule = new SRDocumentGeneralModule(objIDataSet);
			return objSRDocumentGeneralModule;
		}
		return objSRDocumentGeneralModule;
	}
	/**
	 * Retrieves pre-initialized instance of SRDocumentContentModule. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return SRDocumentContentModule - instance of {@link SRDocumentContentModule}.
	 */
	public SRDocumentContentModule getSRDocumentContentModule()
	{
		if(objSRDocumentContentModule == null)
		{
			objSRDocumentContentModule = new SRDocumentContentModule(objIDataSet);
			return objSRDocumentContentModule;
		}
		return objSRDocumentContentModule;
	}

	/**
	 * Retrieves pre-initialized instance of SOP Common Module. Creates a new instance
	 * if no pre-existing module instance is available.If previously created instance is
	 * present then returns the same module instance.
	 * 
	 * @return objSOPCommonModule - instance of {@link objSOPCommonModule}.
	 */
	public SOPCommonModule getSOPCommonModule()
	{
		if(objSOPCommonModule == null)
		{
			objSOPCommonModule = new SOPCommonModule(objIDataSet);
			return objSOPCommonModule;
		}
		return objSOPCommonModule;
	}
	
	/**
	 * This method validates X-Ray 3D Craniofacial Image IOD Modules according to the Module Table given in The DICOM Standard.
	 * The validate method fails if one of the mandatory modules are absent or any of the
	 * modules are invalid.The validate method also checks for conditional modules.
	 * 
	 * @return boolean - function status indicating success or failure.
	 * 
	 * @throws InvalidIODException - throws when IOD is invalid.
	 */
	public boolean validate() throws InvalidIODException
	{
		/*
		 * This condition calls ImageIOD class's validation which checks for common modules validation
		 */
		super.validate();

		/*
		 * This condition checks for X-Ray 3D Craniofacial Image IOD Modules mandatory modules validation
		 */
		try
		{
			getEnhancedGeneralEquipmentModule().validateModule();
			getSRDocumentGeneralModule().validateModule();
			getSRDocumentContentModule().validateModule();
			getSOPCommonModule().validateModule();		

			/*
			 * Condition for Enhanced Contrast/Bolus ,Cardiac Synchronization ,Respiratory Synchronization and Frame Extraction is not clear
			 */
			
		}
		catch(ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(cfe.getMessage());
			throw new InvalidIODException(X_RAY_3D_RADIATION_DOSE_SR_IOD,cfe);
		}
		catch(InvalidModuleException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ime.getMessage());
			
				throw new InvalidIODException(X_RAY_3D_RADIATION_DOSE_SR_IOD,ime);
		}
		return true;
	}

	/**
	 *  Dumps the Name of IOD,Module and corresponding Attributes 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(X_RAY_3D_RADIATION_DOSE_SR_IOD + "\n");
		super.dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getSynchronizationModule().getClass().getSimpleName() + "\n");
		getSynchronizationModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getEnhancedGeneralEquipmentModule().getClass().getSimpleName() + "\n");
		getEnhancedGeneralEquipmentModule().dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getSRDocumentGeneralModule().getClass().getSimpleName() + "\n");
		getSRDocumentGeneralModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getSRDocumentContentModule().getClass().getSimpleName() + "\n");
		getSRDocumentContentModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getSOPCommonModule().getClass().getSimpleName() + "\n");
		getSOPCommonModule().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.SYNCHRONIZATION_MODULE.getValue()) && isModulePresent(SynchronizationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.ENHANCED_GENERAL_EQUIPMENT_MODULE.getValue()) && isModulePresent(EnhancedGeneralEquipmentModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SR_DOCUMENT_GENERAL_MODULE.getValue()) && isModulePresent(SRDocumentGeneralModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SR_DOCUMENT_CONTENT_MODULE.getValue()) && isModulePresent(SRDocumentContentModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SOP_COMMON_MODULE.getValue()) && isModulePresent(SOPCommonModule.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;
	}

}
