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

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.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.VisitAdmissionModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.VisitDischargeModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.VisitIdentificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.VisitRelationshipModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.VisitSchedulingModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.VisitStatusModule;
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 Visit Information Object Definition is an abstraction of the information that describes a patient
 * attendance to a healthcare provider.The Visit IOD is tailored by Service Class Definitions that use services which
 * facilitate the exchange of visit-related information between DICOM Application Entities.
 * 
 * 
 */
public class VisitIOD extends NormalizedIOD 
{
	

	/**
	 * The Constant VISIT_IOD.
	 */
	public static final String VISIT_IOD = "Visit IOD";

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

	/**
	 * The obj visit relationship module.
	 */
	VisitRelationshipModule objVisitRelationshipModule = null;

	/**
	 * The obj visit identification module.
	 */
	VisitIdentificationModule objVisitIdentificationModule = null;

	/**
	 * The obj visit admission module.
	 */
	VisitAdmissionModule objVisitAdmissionModule = null;

	/**
	 * The obj visit status module.
	 */
	VisitStatusModule objVisitStatusModule = null;

	/**
	 * The obj visit discharge module.
	 */
	VisitDischargeModule objVisitDischargeModule = null;

	/**
	 * The obj visit scheduling module.
	 */
	VisitSchedulingModule objVisitSchedulingModule = null;

	/**
	 * Instantiates a new visit iod.
	 */
	public VisitIOD()
	{
		super();
	}

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

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

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


	/**
	 * 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 VisitAdmissionModule with preinitializing it.
	 * 
	 * @return - instance of {@link VisitAdmissionModule}
	 */
	public VisitAdmissionModule getVisitAdmissionModule()
	{
		if(objVisitAdmissionModule == null)
		{
			objVisitAdmissionModule = new VisitAdmissionModule(objIDataSet);
			return objVisitAdmissionModule;
		}
		return objVisitAdmissionModule;
	}

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

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

	/**
	 * This method returns the VisitIdentificationModule with preinitializing it.
	 * 
	 * @return - instance of {@link VisitRelationshipModule}
	 */
	public VisitRelationshipModule getVisitRelationshipModule()
	{
		if(objVisitRelationshipModule == null)
		{
			objVisitRelationshipModule = new VisitRelationshipModule(objIDataSet);
			return objVisitRelationshipModule;
		}
		return objVisitRelationshipModule;
	}

	/**
	 * validates the module according to the rules specified by DICOM Standard.
	 * 
	 * @return boolean - returns the validation status as success or failure
	 */
	public boolean validateModule()
	{
		return true;
	}


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

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

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

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

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

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

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

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

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

		objIDicomStream.writeString("\t" + getVisitSchedulingModule().getClass().getSimpleName() + "\n");
		getVisitSchedulingModule().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.VISIT_RELATIONSHIP_MODULE.getValue()) && isModulePresent(VisitRelationshipModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.VISIT_IDENTIFICATION_MODULE.getValue()) && isModulePresent(VisitIdentificationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.VISIT_ADMISSION_MODULE.getValue()) && isModulePresent(VisitAdmissionModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.VISIT_STATUS_MODULE.getValue()) && isModulePresent(VisitStatusModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.VISIT_DISCHARGE_MODULE.getValue()) && isModulePresent(VisitDischargeModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.VISIT_SCHEDULING_MODULE.getValue()) && isModulePresent(VisitSchedulingModule.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;
	}
}
