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

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.RTIonMachineVerificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NonImageIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.RTGeneralMachineVerificationModule;
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.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 RT Ion Machine Verification IOD describes the attributes that are required by an external Machine Parameter Verifier(MPV) when performing verification of a ion radiation therapy treatment, prior to delivery.
 * @author saloni
 *
 */
public class RTIonMachineVerificationIOD extends NonImageIOD
{
	/**
	 * The Constant RT ION MACHINE VERIFICATION IOD.
	 */
	public static final String RT_ION_MACHINE_VERIFICATION_INFORMATION_OBJECT_DEFINITION = "RT Ion Machine Verification IOD";

	
	/**
	 * The obj sop common module.
	 */
	SOPCommonModule objSOPCommonModule = null;	
	
	/**
	 * The obj RT General Machine Verification module.
	 */
	RTGeneralMachineVerificationModule objRTGeneralMachineVerificationModule = null;

	/**
	 * The obj RT Ion Machine Verification module.
	 */
	RTIonMachineVerificationModule objRTIonMachineVerificationModule = null;
	/**
	 * Instantiates a new RTIonMachineVerificationIOD
	 */
	public RTIonMachineVerificationIOD()
	{
		super();
	}

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

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

	/**
	 * Creates RTIonMachineVerificationIOD IOD from a DICOM file containing RTIonMachineVerificationIOD 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 RTIonMachineVerificationIOD(String strFileName)throws IOException,InvalidIODException
	{
		super(strFileName);
		try
		{
			if(objIDataSet != null)
			{
				String strSOPClassUID = objIDataSet.getSOPClassUID();
				if(strSOPClassUID != null)
				{
					if(!strSOPClassUID.equals(EnumSOPClassUIDs.RTIONMACHINEVERIFICATIONINFORMATIONOBJECTDEFINITION.getValue()))
					{
						throw new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "InvalidIOD.FileNotMatched", new Locale(DicomConfig.createInstance().getLocale())) + RT_ION_MACHINE_VERIFICATION_INFORMATION_OBJECT_DEFINITION);
					}
				}
				strSOPClassUID = null;
			}
		}
		catch(InvalidIODException iie)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(iie.getMessage());
			}
			throw iie;
		}
	}
	
	/**
	 * Retrieves pre-initialized instance of SOPCommonModule. 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 SOPCommonModule - instance of {@link SOPCommonModule}.
	 */
	public SOPCommonModule getSOPCommonModule() 
	{
		if(objSOPCommonModule == null)
		{
			objSOPCommonModule = new SOPCommonModule(objIDataSet);
			return objSOPCommonModule;
		}
		return objSOPCommonModule;
	}
	/**
	 * Retrieves pre-initialized instance of RTGeneralMachineVerificationModule. 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 RTGeneralMachineVerificationModule - instance of {@link RTGeneralMachineVerificationModule}.
	 */
	public RTGeneralMachineVerificationModule getRTGeneralMachineVerificationModule() 
	{
		if(objRTGeneralMachineVerificationModule == null)
		{
			objRTGeneralMachineVerificationModule = new RTGeneralMachineVerificationModule(objIDataSet);
			return objRTGeneralMachineVerificationModule;
		}
		return objRTGeneralMachineVerificationModule;
	}
	/**
	 * Retrieves pre-initialized instance of RTIonMachineVerificationModule. 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 RTIonMachineVerificationModule - instance of {@link RTIonMachineVerificationModule}.
	 */
	public RTIonMachineVerificationModule getRTIonMachineVerificationModule() 
	{
		if(objRTIonMachineVerificationModule == null)
		{
			objRTIonMachineVerificationModule = new RTIonMachineVerificationModule(objIDataSet);
			return objRTIonMachineVerificationModule;
		}
		return objRTIonMachineVerificationModule;
	}
	/**
	 * This method validates Non-Image IOD RTIonMachineVerificationIOD 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 - status of validation as success or failure
	 * 
	 * @throws InvalidIODException  throws when the IOD is invalid.
	 */
	public boolean validate() throws InvalidIODException
	{
		try
		{
			super.validate();

			/*
			 * Nothing to validate here
			 */
				
		}
		catch(ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(cfe.getMessage());
			throw new InvalidIODException(RT_ION_MACHINE_VERIFICATION_INFORMATION_OBJECT_DEFINITION,cfe);
		}
		catch(InvalidModuleException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ime.getMessage());
			
				throw new InvalidIODException(RT_ION_MACHINE_VERIFICATION_INFORMATION_OBJECT_DEFINITION,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(RT_ION_MACHINE_VERIFICATION_INFORMATION_OBJECT_DEFINITION + "\n");
		super.dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getSOPCommonModule().getClass().getSimpleName() + "\n");
		getSOPCommonModule().dumpDICOMInfo(objIDicomStream);

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

		objIDicomStream.writeString("\t" + getRTIonMachineVerificationModule().getClass().getSimpleName() + "\n");
		getRTIonMachineVerificationModule().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.RT_GENERAL_MACHINE_VERIFICATION_MODULE.getValue()) && isModulePresent(RTGeneralMachineVerificationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.RT_ION_MACHINE_VERIFICATION_MODULE.getValue()) && isModulePresent(RTIonMachineVerificationModule.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;
	}
}
