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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.services.basicmediastorageapplicationprofiles.MediaStorage_FSR;
import cdac.medinfo.sdk.dcm30_04.services.dicomdir.DICOMDIR;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.DICOMDIRException;
import cdac.medinfo.sdk.dcm30_04.services.minquirefile.MInquireFile;
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 is implemented to use DIMSE_M-Services.
 * The MediaStorage_uncompress_FSC supports the Media Storage Operations M-Read and M-Inquire File.
 * It uncompresses DICOM DIR File-Set.
 * */
public class MediaStorageUncompresse_FSR {
	/**
	 * This function Uncompress DicomDir File Set.
	 * It fetches data from DicomDir File-Set.
	 * @param strFileName,strStoragePath
	 * @throws IOException
	 * */
	public IDataSet process(String strStoragePath, String destDir) throws DICOMDIRException, IOException
	{	
		IDataSet objIDataSet;	
		String addressnew = unzip(strStoragePath,destDir);
		MediaStorage_FSR objMediaStorage_FSR = new MediaStorage_FSR ();
		DICOMDIR objDICOMDIR = new DICOMDIR(addressnew);
		if(objDICOMDIR.isDICOMDIRExist())
		{
			objIDataSet = objMediaStorage_FSR.process(addressnew);
			MInquireFile objMInquireFile= new MInquireFile();
			objMInquireFile.processTime(strStoragePath);
			return objIDataSet;
		}
		else
		{
			DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("warning", "DICOMDIR.FileNotFound", new Locale(DicomConfig.createInstance().getLocale())));
			objIDataSet= null;
		}
		return objIDataSet;
	}
	/**
	 * This function unzip DicomDir File Set.
	 *  @param zipFilePath,destDir.
	 * */
	public static  String unzip(String zipFilePath, String destDir) 
	{		
		String fileName1;
		File dir = new File(destDir);
		// create output directory if it doesn't exist
		if(!dir.exists()) dir.mkdirs();
		FileInputStream objFileInputStream;
		//buffer for read and write data to file
		byte[] buffer = new byte[1024];
		fileName1 = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));

		try {
			objFileInputStream = new FileInputStream(zipFilePath);
			ZipInputStream objZipInputStream = new ZipInputStream(objFileInputStream);
			ZipEntry objZipEntry = objZipInputStream.getNextEntry();
			fileName1 = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));
			while(objZipEntry!= null)
			{
				String fileName = objZipEntry.getName();
				File newFile = new File(destDir + File.separator + fileName);
				//create directories for sub directories in zip
				new File(newFile.getParent()).mkdirs();
				FileOutputStream fos = new FileOutputStream(newFile);
				int len;
				while ((len = objZipInputStream.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
				fos.close();
				//close this ZipEntry
				objZipInputStream.closeEntry();
				objZipEntry = objZipInputStream.getNextEntry();
			}
			//close last ZipEntry
			objZipInputStream.closeEntry();
			objZipInputStream.close();
			objZipInputStream.close();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return fileName1;
	}
}