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

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.GenericImplantTemplate2DDrawingsModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.GenericImplantTemplate3DModelsModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.GenericImplantTemplateDescriptionModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.GenericImplantTemplateMatingFeaturesModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.GenericImplantTemplatePlanningLandmarksModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.NormalizedIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SurfaceMeshModule;
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.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 Generic Implant Template Information Object Definition (IOD) specifies the description of a 2D- and/or 3D-template representing a rigid and passive implant.
 * @author saloni
 *
 */
public class GenericImplantTemplateIOD extends NormalizedIOD 
{

	/**
	 * The Constant GENERIC_IMPLANT_TEMPLATE_IOD.
	 */
	public static final String GENERIC_IMPLANT_TEMPLATE_IOD = "Generic Implant Template IOD";

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

	/**
	 * The obj Generic Implant Template Description module.
	 */
	GenericImplantTemplateDescriptionModule objGenericImplantTemplateDescriptionModule = null;
	
	/**
	 * The obj Generic Implant Template 2D Drawings module.
	 */
	GenericImplantTemplate2DDrawingsModule objGenericImplantTemplate2DDrawingsModule = null;
	
	/**
	 * The obj Generic Implant Template 3D Models module.
	 */
	GenericImplantTemplate3DModelsModule objGenericImplantTemplate3DModelsModule = null;
	
	/**
	 * The obj Generic Implant Template Mating Features module.
	 */
	GenericImplantTemplateMatingFeaturesModule objGenericImplantTemplateMatingFeaturesModule = null;
	
	/**
	 * The obj Generic Implant Template Planning Landmarks module.
	 */
	GenericImplantTemplatePlanningLandmarksModule objGenericImplantTemplatePlanningLandmarksModule = null;
	
	/**
	 * The obj Surface Mesh module.
	 */
	SurfaceMeshModule objSurfaceMeshModule = null;

	/**
	 * Instantiates a new Generic Implant Template IOD.
	 */
	public GenericImplantTemplateIOD()
	{
		super();
	}

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

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

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

	/**
	 * Returns the GenericImplantTemplateDescriptionModule with preinitializing it.
	 * 
	 * @return - instance of {@link GenericImplantTemplateDescriptionModule}.
	 */
	public GenericImplantTemplateDescriptionModule getGenericImplantTemplateDescriptionModule()
	{
		if(objGenericImplantTemplateDescriptionModule == null)
		{
			objGenericImplantTemplateDescriptionModule = new GenericImplantTemplateDescriptionModule(objIDataSet);
			return objGenericImplantTemplateDescriptionModule;
		}
		return objGenericImplantTemplateDescriptionModule;
	}
	
	/**
	 * Returns the GenericImplantTemplate2DDrawingsModule with preinitializing it.
	 * 
	 * @return - instance of {@link GenericImplantTemplate2DDrawingsModule}.
	 */
	public GenericImplantTemplate2DDrawingsModule getGenericImplantTemplate2DDrawingsModule()
	{
		if(objGenericImplantTemplate2DDrawingsModule == null)
		{
			objGenericImplantTemplate2DDrawingsModule = new GenericImplantTemplate2DDrawingsModule(objIDataSet);
			return objGenericImplantTemplate2DDrawingsModule;
		}
		return objGenericImplantTemplate2DDrawingsModule;
	}
	
	/**
	 * Returns the GenericImplantTemplate3DModelsModule with preinitializing it.
	 * 
	 * @return - instance of {@link GenericImplantTemplate3DModelsModule}.
	 */
	public GenericImplantTemplate3DModelsModule getGenericImplantTemplate3DModelsModule()
	{
		if(objGenericImplantTemplate3DModelsModule == null)
		{
			objGenericImplantTemplate3DModelsModule = new GenericImplantTemplate3DModelsModule(objIDataSet);
			return objGenericImplantTemplate3DModelsModule;
		}
		return objGenericImplantTemplate3DModelsModule;
	}
	
	/**
	 * Returns the GenericImplantTemplateMatingFeaturesModule with preinitializing it.
	 * 
	 * @return - instance of {@link GenericImplantTemplateMatingFeaturesModule}.
	 */
	public GenericImplantTemplateMatingFeaturesModule getGenericImplantTemplateMatingFeaturesModule()
	{
		if(objGenericImplantTemplateMatingFeaturesModule == null)
		{
			objGenericImplantTemplateMatingFeaturesModule = new GenericImplantTemplateMatingFeaturesModule(objIDataSet);
			return objGenericImplantTemplateMatingFeaturesModule;
		}
		return objGenericImplantTemplateMatingFeaturesModule;
	}
	
	/**
	 * Returns the GenericImplantTemplatePlanningLandmarksModule with preinitializing it.
	 * 
	 * @return - instance of {@link GenericImplantTemplatePlanningLandmarksModule}.
	 */
	public GenericImplantTemplatePlanningLandmarksModule getGenericImplantTemplatePlanningLandmarksModule()
	{
		if(objGenericImplantTemplatePlanningLandmarksModule == null)
		{
			objGenericImplantTemplatePlanningLandmarksModule = new GenericImplantTemplatePlanningLandmarksModule(objIDataSet);
			return objGenericImplantTemplatePlanningLandmarksModule;
		}
		return objGenericImplantTemplatePlanningLandmarksModule;
	}
	
	/**
	 * Returns the SurfaceMeshModule with preinitializing it.
	 * 
	 * @return - instance of {@link SurfaceMeshModule}.
	 */
	public SurfaceMeshModule getSurfaceMeshModule()
	{
		if(objSurfaceMeshModule == null)
		{
			objSurfaceMeshModule = new SurfaceMeshModule(objIDataSet);
			return objSurfaceMeshModule;
		}
		return objSurfaceMeshModule;
	}

	/**
	 * 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;
	}

	/* 
	 * For normalized IODs, the type(Mandatory/Optional) of a Module depends upon the
	 * Requirement Type which defines if this module is required by a SCP class or an
	 * SCP class. Hence,the validation of Modules in a Normalized IOD totally depends 
	 * upon the decision that the is it an SCU/SCP that needs the module.Also, Type of the Modules
	 * describes the type of the Attributes inside the Module. Ex: Mandatory Modules contain Attributes that are
	 * included in all SOP Instances employing this IOD.
	 * @return - returns validation status as success or failure 
	 * @throws InvalidModuleException - If validation of Module fails 	 
	 */

	/* (non-Javadoc)
	 * @see cdac.medinfo.sdk.dcm30_04.dicommodel.modules.IOD#validate()
	 */
	public boolean validate() throws InvalidIODException
	{
		super.validate();
		try
		{
			/*
			 * This condition checks for mandatory modules validation
			 */
			getGenericImplantTemplateDescriptionModule().validateModule();
			getSOPCommonModule().validateModule();
			
			if(isModulePresent(GenericImplantTemplate2DDrawingsModule.objSingleCollection,objIDataSet))
				getGenericImplantTemplate2DDrawingsModule().validateModule();
			if(isModulePresent(GenericImplantTemplate3DModelsModule.objSingleCollection,objIDataSet))
				getGenericImplantTemplate3DModelsModule().validateModule();
			if(isModulePresent(GenericImplantTemplateMatingFeaturesModule.objSingleCollection,objIDataSet))
				getGenericImplantTemplateMatingFeaturesModule().validateModule();
			if(isModulePresent(GenericImplantTemplatePlanningLandmarksModule.objSingleCollection,objIDataSet))
				getGenericImplantTemplatePlanningLandmarksModule().validateModule();
			if(isModulePresent(SurfaceMeshModule.objSingleCollection,objIDataSet))
				getSurfaceMeshModule().validateModule();
		}
		catch(InvalidModuleException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(ime.getMessage());
			
				throw new InvalidIODException(GENERIC_IMPLANT_TEMPLATE_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(GENERIC_IMPLANT_TEMPLATE_IOD+ "\n");
		super.dumpDICOMInfo(objIDicomStream);

		objIDicomStream.writeString("\t" + getSOPCommonModule().getClass().getSimpleName() + "\n");
		getSOPCommonModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getGenericImplantTemplateDescriptionModule().getClass().getSimpleName() + "\n");
		getGenericImplantTemplateDescriptionModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getGenericImplantTemplate2DDrawingsModule().getClass().getSimpleName() + "\n");
		getGenericImplantTemplate2DDrawingsModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getGenericImplantTemplate3DModelsModule().getClass().getSimpleName() + "\n");
		getGenericImplantTemplate3DModelsModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getGenericImplantTemplateMatingFeaturesModule().getClass().getSimpleName() + "\n");
		getGenericImplantTemplateMatingFeaturesModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getGenericImplantTemplatePlanningLandmarksModule().getClass().getSimpleName() + "\n");
		getGenericImplantTemplatePlanningLandmarksModule().dumpDICOMInfo(objIDicomStream);
		
		objIDicomStream.writeString("\t" + getSurfaceMeshModule().getClass().getSimpleName() + "\n");
		getSurfaceMeshModule().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.GENERIC_IMPLANT_TEMPLATE_DESCRIPTION_MODULE.getValue()) && isModulePresent(GenericImplantTemplateDescriptionModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.GENERIC_IMPLANT_TEMPLATE_2D_DRAWINGS_MODULE.getValue()) && isModulePresent(GenericImplantTemplate2DDrawingsModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.GENERIC_IMPLANT_TEMPLATE_3D_MODELS_MODULE.getValue()) && isModulePresent(GenericImplantTemplate3DModelsModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.GENERIC_IMPLANT_TEMPLATE_MATING_FEATURES_MODULE.getValue()) && isModulePresent(GenericImplantTemplateMatingFeaturesModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.GENERIC_IMPLANT_TEMPLATE_PLANNING_LANDMARKS_MODULE.getValue()) && isModulePresent(GenericImplantTemplatePlanningLandmarksModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.SURFACE_MESH_MODULE.getValue()) && isModulePresent(SurfaceMeshModule.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;
	}
}
