/*******************************************************************************
 *  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.services.mediastorageapplicationprofile.testcases.filesetreader;

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

import cdac.medinfo.sdk.dcm30_04.baselibs.SQ;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;
import cdac.medinfo.sdk.dcm30_04.services.generalpurposecompressioninterchangeprofiles.MediaStorageUncompresse_FSR;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.ICollectionIterator;
/**
 * This class is implemented to use MediaStorage_uncompresse_FSR.
 * The Basic_Media_Storage_Test_FSR creates object of MediaStorage_uncompresse_FSR.
 * It calls Process Method of MediaStorage_uncompresse_FSR.
 * It demonstrates basic functionality of MediaStorage_uncompresse_FSR class, uncompresses DicomDir File-Set, Fetches Dicom-Dir File Set from Media Storage Devices.
 * This class also creates object of Basic_Media_Storage_Test_FSR class to call display() function .
 * */
public class UncompressMediaStorageTest_FSR 
{
	public static void main(String[] args) 
	{
		try
		{
			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");
			System.out.println("Enter the DICOM DIR File Set Location");
			BufferedReader b_reader = new BufferedReader(new InputStreamReader(System.in));
			String strStoragePath=b_reader.readLine();
			System.out.println("strStoragePath" + strStoragePath);
			String destDir = null;
			if (strStoragePath == null|| strStoragePath.isEmpty())
			{
				System.out.println("Path cannot be Null  ");
			}
			else
			{
				File dir = new File(strStoragePath);
				if (!dir.exists())
				{
					System.out.println("Invalid File Path ");
				}
				else
				{

					String fileExtension=strStoragePath.substring(strStoragePath.lastIndexOf(".")+1);
					String zipName = "zip";
					if(!fileExtension.equalsIgnoreCase(zipName))
					{
						System.out.println("Invalid File Extension ");
					}
					else
					{
						try
						{
							destDir = strStoragePath.substring(0,strStoragePath.lastIndexOf(File.separator));
						}
						catch(java.lang.StringIndexOutOfBoundsException ex)
						{
							System.out.println("Invalid File Path ");

						}
						//Creates object of MediaStorage_uncompresse_FSR class,
						//calls process() of MediaStorage_uncompresse_FSR.
						MediaStorageUncompresse_FSR objMediaStorage_uncompresse_FSR = new MediaStorageUncompresse_FSR();
						IDataSet  objIdataset= objMediaStorage_uncompresse_FSR.process(strStoragePath,destDir);

						if (objIdataset== null)
						{
							System.out.println("Invalid File Path ");
						}
						else
						{
							display(objIdataset);
						}}}}

		}
		catch (IOException e)
		{

			e.printStackTrace();
		}
	}

	/**
	 * Displays the DataElements of Dataset
	 */
	public static void display(IDataSet  objIdataset)
	{
		System.out.println(objIdataset.getSOPClassUID());
		ICollectionIterator<IDataElement> objIDataSetIterator = objIdataset.iterator();
		System.out.println("Tag     Value");
		while(objIDataSetIterator.hasNext())
		{
			IDataElement objDataElement = objIDataSetIterator.next();				
			if(!(objDataElement.getDicomValue() instanceof SQ))
			{

				System.out.println(objDataElement.getTag().getGroupNumber()+"    "+
						objDataElement.getTag().getElementNumber()+"    "+
						objDataElement.getDicomValue().toString());
			}
			else  
			{
				System.out.println("else part");
				displaySequence((SQ)objDataElement.getDicomValue(),"> ");					
			}
		}
	}
	/**
	 * Displays the Dicom Data SQ.
	 */
	public static void displaySequence(SQ objSQDataElement,String ss)

	{
		ICollectionIterator<IItem> objSQItemIterator = objSQDataElement.iterator();
		int i = 1;
		while(objSQItemIterator.hasNext())
		{
			System.out.println("ITEM :"+ i++ );
			IItem objItem = objSQItemIterator.next();
			ICollectionIterator<IDataElement> objDataElementIterator = objItem.iterator();
			while(objDataElementIterator.hasNext())
			{
				IDataElement objDataElement = objDataElementIterator.next();

				if(!(objDataElement.getDicomValue() instanceof SQ))
				{
					System.out.println(ss +objDataElement.getTag().getGroupNumber()+"    "+
							objDataElement.getTag().getElementNumber()+"    "+
							objDataElement.getDicomValue().toString());						 
				}
				else
				{
					displaySequence((SQ)objDataElement.getDicomValue(),ss+ss);
				}	 
			}
		}
	}
}
