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

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.DirectoryInformationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.FileSetIdentificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.IOD;
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;
/**
 * This class represents Basic Directory Information Object Definition.
 * The instance members suggest that these are the modules contained in the Basic Directory IOD.
 * This class exhibits different functional behavior of the CT IOD.
 */
public class BasicDirectoryIOD extends IOD
{
	

	/**
	 * The Constant BASIC_FILM_BOX_IOD.
	 */
	public static final String BASIC_DIRECTORY_IOD = "Basic Directory IOD";

	/**
	 * The File Set Identification Module.
	 */
	FileSetIdentificationModule objFileSetIdentificationModule = null;

	/**
	 * The Directory Information Module
	 */
	DirectoryInformationModule objDirectoryInformationModule = null;


	/**
	 * Instantiates a new basic directory iod.
	 */
	public BasicDirectoryIOD()
	{
		super();
	}

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

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

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

	/**
	 * Returns the BasicFilmSessionPresentationModule with preinitializing it.
	 * @return instance of {@link FileSetIdentificationModule}.
	 */
	public FileSetIdentificationModule getFileSetIdentificationModule() 
	{
		if(objFileSetIdentificationModule == null)
		{
			objFileSetIdentificationModule = new FileSetIdentificationModule(objIDataSet);
			return objFileSetIdentificationModule;
		}
		return objFileSetIdentificationModule;
	}

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

	public boolean validate() throws InvalidIODException
	{
		getFileSetIdentificationModule().validateModule();
		return true;
	}

	/**
	 *  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(BASIC_DIRECTORY_IOD+ "\n");

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

		objIDicomStream.writeString("\t" + getDirectoryInformationModule().getClass().getSimpleName() + "\n");
		getDirectoryInformationModule().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.FILE_SET_IDENTIFICATION_MODULE.getValue()) && isModulePresent(FileSetIdentificationModule.objSingleCollection))
				return true;
			else if(strModuleName.equalsIgnoreCase(EnumModuleNames.DIRECTORY_INFORMATION_MODULE.getValue()) && isModulePresent(DirectoryInformationModule.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;
	}
}
