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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomFileHeader;
import cdac.medinfo.sdk.dcm30_04.dicomparserserializers.DicomParser;
import cdac.medinfo.sdk.dcm30_04.dicomparserserializers.DicomSerializer;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.ICollectionIterator;

/**
 * This testcode parses the DataSet when a Dicom Compliant file is given as input.
 * It first check for the Dicom Header and then populates the DataSet.
 * After parsing it displays the Header and DataSet and then serializes the file at given location.
 */
public class ParserSerializer 
{

	DicomParser objDicomParser = null;

	/**
	 * It parses the file and displays the DataElements of File Header and DataSet.
	 * After parsing it serializes the file on given location.
	 */
	public IDataSet parse() throws IOException
	{
		/*
		 * Creates Dicom parser.
		 */
		objDicomParser = new DicomParser();
		IDataSet objIDataSet = null;
		IDicomFileHeader objDicomFileHeader = null;
		/*
		 * Accept the DICOM file from User to parse
		 */
		System.out.println("Enter the Dicom File name with complete path");
		BufferedReader b_reader =new BufferedReader( new InputStreamReader( System.in ) );
		String strFilePath = b_reader.readLine();

		/*
		 * Here DicomParser is being called to parse the file.
		 * Parsing process takes two parameters.
		 * strFilePath = path of file to parse.
		 * boolean = This boolean value decides whether to convert the TransferSyntax 
		 * or default TransferSyntax (same as given in file)will be taken. 
		 */
		objIDataSet = objDicomParser.parseAsPart10File(strFilePath, true);

		/*
		 * Get the File Header from Dicom Parser.
		 */
		objDicomFileHeader = objDicomParser.getFileHeader();

		/*
		 * Diplay DataElement of File Header.
		 */
		System.out.println("Displaying DataElements of File Header");
		if(objDicomFileHeader != null)
			display(objDicomFileHeader.iterator());

		/*
		 * Displays DataElement of DataSet.
		 */
		System.out.println("Displaying DataElements of DataSet");
		if(objIDataSet != null)
		{
			display(objIDataSet.iterator());
		}
		return objIDataSet;
	}

	/*
	 * Displays the DataElements of given Collection Iterator.
	 */
	private void display(ICollectionIterator<IDataElement> objIterator)
	{
		int iGroupNumber = 0;
		int iElementNumber = 0;
		System.out.println("Group Number"+"\t\t"+"Element Number");
		while(objIterator.hasNext())
		{
			IDataElement objIDataElement = objIterator.next();
			iGroupNumber = objIDataElement.getTag().getGroupNumber();
			iElementNumber = objIDataElement.getTag().getElementNumber();
			System.out.println( Integer.toHexString(iGroupNumber) +"\t\t"+ Integer.toHexString(iElementNumber) );
		}
	}

	/**
	 * Serializes the given DataSet with file Header if given, else appends default fileheader.
	 * @param objIDataSet object of DataSet to be serialized.
	 * @param objDicomFileHeader object of Dicom File header. 
	 */
	public void serialize(IDataSet objIDataSet, IDicomFileHeader objDicomFileHeader) throws IOException
	{
		/*
		 * path where to serialize the Dicom File.
		 */
		BufferedReader b_reader =new BufferedReader( new InputStreamReader( System.in ) );
		System.out.println("Enter the Dicom File name with complete path where to save the Serialzed File");
		String strFilePath = b_reader.readLine();

		/*
		 * Creates instance of Dicom Serializer.
		 */
		DicomSerializer objDicomSerializer = new DicomSerializer();

		/*
		 * Here DicomSerializer is being called to serialize the file.
		 * Parsing process takes two parameters.
		 * strFilePath - path of file to parse.
		 * objIDataSet - DataSet to be serialized.
		 * objDicomFileHeader - If null then dedfault will be used.
		 * boolean - This boolean value decides whether to convert the TransferSyntax 
		 * 							or default TransferSyntax (same as given in file)will be taken.  
		 */
		objDicomSerializer.serializeAsPart10File(strFilePath, objIDataSet, objDicomFileHeader,true);
	}


	public static void main(String args[])
	{
		try
		{
			/*
			 * Creates TestParseSerialize.
			 */
			ParserSerializer objTestParseSerialize = new ParserSerializer();

			DicomConfig objDicomConfig = DicomConfig.createInstance();

//			enable logging,specify the path of the directory where log files are created.
			//User can specify any directory in place of 'c:\DCM_Logs'.
			//If no directory is defined by user to create logs then it is created in User Temp DIR.
			objDicomConfig.enableLogging(EnumLoggingLevel.INFO, "C:\\DCM_Logs");
			/*
			 * Get the data set after parsing the file
			 */
			IDataSet objIDataSet = objTestParseSerialize.parse();

			/*
			 * Serialize the parsed dataset at specified location.
			 */
			objTestParseSerialize.serialize(objIDataSet,objTestParseSerialize.objDicomParser.getFileHeader());
			
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
