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

import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.Random;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.dicomparserserializers.DicomParser;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidDICOMFileException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidIODException;
import cdac.medinfo.sdk.dcm30_04.services.minquirefileset.MInquireFileSet;
import cdac.medinfo.sdk.dcm30_04.services.mwrite.MWrite;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
import cdac.medinfo.sdk.dcm30_04.utils.InvalidFilePathException;
import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;
import cdac.medinfo.sdk.dcm30_04.utils.StandardMethods;
/**
 * This class is implemented to use DIMSE_M-Services.
 * The MediaStorage_FSC supports the Media Storage Operations M-Write and M-INQUIRE FILE-SET
 * It checks available free space on disk.
 * It creates DICOM DIR File Set on Disk.
 * */
public class MediaStorage_FSC {
	/**
	 * This Function checks available free space on disk.
	 * It creates DICOM DIR File Set on Disk.
	 * @param strFileName,strStoragePath1.
	 * @throws IOException
	 * */

	public String process(String strFileName, String strStoragePath1) throws IOException

	{ 			
		String strStoragePath2;
		File objFile1 = new File(strFileName);
		Random ran = new Random();
		if(strStoragePath1 == null||strStoragePath1 == "" ||strStoragePath1 == "null"||strStoragePath1.isEmpty())
		{		
			strStoragePath2 = StandardMethods.getTempDirectoryStorage() + "CDAC-SDK/DCM-J/Storage"+"/DICOM"+ran.nextInt(1000);
		}

		else {
			File dir = new File(strStoragePath1);
			if(!dir.exists()||dir.isFile())
			{
				strStoragePath2 = StandardMethods.getTempDirectoryStorage() + "CDAC-SDK/DCM-J/Storage"+"/DICOM"+ran.nextInt(1000);
			}
			else
			{
				strStoragePath2 = 	strStoragePath1+"/DICOM"+ran.nextInt(1000);
			}

		}
		if(objFile1.isDirectory())
		{
			throw new InvalidDICOMFileException(ResourceBundleWrapper.getValue("exceptions", "InvalidDicomFile", new Locale(DicomConfig.createInstance().getLocale()))+"Directory Path Entered");

		}
		else
		{
			if(objFile1.exists())
			{
				File objFile = new File(strFileName);
				long fileSize = objFile.length()/1024;
				String strStoragePath  = StandardMethods.getTempDirectoryStorage();
				MInquireFileSet objMInquireFileSet = new MInquireFileSet();
				long space= objMInquireFileSet.processSpace(strStoragePath);
				/**
				 * This Part of code check the free space available on disk , if available so create DICOM-Dir File-Set.
				 */
				if(space>fileSize)
				{	
					/** Creates the object of corresponding IOD by calling its
					 *constructor which takes File Name as a parameter and give its DataSet.	
					 **/
					DicomParser objDicomParser = new DicomParser();
					IDataSet objIDataSet = objDicomParser.parseAsPart10File(strFileName, true);
					String strSOPClassUID = objIDataSet.getSOPClassUID();
					if(strSOPClassUID != null)
					{
						if(!strSOPClassUID.equals(EnumSOPClassUIDs.XRAYANGIOGRAPHICIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.MEDIASTORAGEDIRECTORYSTORAGE.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.ENHANCEDXAIMAGEIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.INTRAOCULARLENSCALCULATIONSIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.GRAYSCALESOFTCOPYIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.USIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.USMULTIFRAME.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.XAIMAGEIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.IMPLANTTEMPLATEGROUPIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.IMPLANTASSEMBLYTEMPLATEIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.GENERICIMPLANTTEMPLATEIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.COLORPALETTEIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.HANGINGPROTOCOLIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.RTBEAMSDELIVERYINSTRUCTIONIOD.getValue())						
								||strSOPClassUID.equals(EnumSOPClassUIDs.RTCONVENTIONALMACHINEVERIFICATIONINFORMATIONOBJECTDEFINITION.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.RTBEAMSDELIVERYINSTRUCTIONIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.RTIonPlanIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.RTIONMACHINEVERIFICATIONINFORMATIONOBJECTDEFINITION.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.BASICSTRUCTUREDDISPLAYIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.LEGACYCONVERTEDENHANCEDPETIMAGEIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.EnhancedPETImageIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.LEGACYCONVERTEDENHANCEDPETIMAGEIOD.getValue())						
								||strSOPClassUID.equals(EnumSOPClassUIDs.ENCAPSULATEDCDAIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.ENCAPSULATEDPDFIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.EnhancedCTImageIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.COLONCADSRIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.RADIOPHARMACEUTICALRADIATIONDOSESRIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.XRAYRADIATIONDOSESRIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.CHESTCADSRIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.KEYOBJECTSELECTIONDOCUMENTIOD.getValue())						
								||strSOPClassUID.equals(EnumSOPClassUIDs.MAMMOGRAPHYCADSRIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.PROCEDURELOGIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.EXTENSIBLESRIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.COMPREHENSIVE3DSRIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.COMPREHENSIVESRIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.ENHANCEDSRIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.BASICTEXTSRIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.CORNEALTOPOGRAPHYMAPIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.OPHTHALMICTHICKNESSMAPIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.OPHTHALMICVISUALFIELDSTATICPERIMETRYMEASUREMENTSIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.MACULARGRIDTHICKNESSANDVOLUMEREPORTIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.INTRAOCULARLENSCALCULATIONSIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.OPHTHALMICAXIALMEASUREMENTSIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.SPECTACLEPRESCRIPTIONREPORTIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.VISUALACUITYMEASUREMENTSIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.SUBJECTIVEREFRACTIONMEASUREMENTSIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.KERATOMETRYMEASUREMENTSIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.AUTOREFRACTIONMEASUREMENTSIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.LENSOMETRYMEASUREMENTSIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.VLWHOLESLIDEMICROSCOPYIMAGEIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.WIDEFIELDOPHTHALMICPHOTOGRAPHY3DCOORDINATESIMAGEIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.WIDEFIELDOPHTHALMICPHOTOGRAPHYSTEREOGRAPHICPROJECTIONIMAGEIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.OPHTHALMICTOMOGRAPHYIMAGEIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.STEREOMETRICRELATIONSHIP.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.OPHTHALMICPHOTOGRAPHY16BITIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.OPHTHALMICPHOTOGRAPHY8BITIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.VIDEOPHOTOGRAPHICIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.VLPHOTOGRAPHICIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.VLSLIDECOORDINATESMICROSCOPICIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.VIDEOMICROSCOPICIOD.getValue())
								||strSOPClassUID.equals(EnumSOPClassUIDs.VLMICROSCOPICIOD.getValue())||strSOPClassUID.equals(EnumSOPClassUIDs.VIDEOENDOSCOPICIOD.getValue()))

							/**
							 *  Creates the object of MWrite.
							 * call the function to create DICOM DIR File Set
							 */	
						{
							MWrite objMWrite = new MWrite();
							objMWrite.process(strStoragePath2, objIDataSet);
							DicomLogger objDicomLogger = DicomLogger.getLogger();
							objDicomLogger.log(ResourceBundleWrapper.getValue("status", "DicomDirFile", new Locale(DicomConfig.createInstance().getLocale())) );
						}
						else
						{
							throw new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "InvalidIOD.FileNotMatched", new Locale(DicomConfig.createInstance().getLocale())) + "Invalid IOD");
						}
					}
					else
					{
						throw new InvalidIODException(ResourceBundleWrapper.getValue("exceptions", "InvalidIOD.FileNotMatched", new Locale(DicomConfig.createInstance().getLocale())) + "Invalid IOD");
					}}	
				else
				{

					DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("status", "UnsufficientSpace", new Locale(DicomConfig.createInstance().getLocale())) );
				}

			}
			else
			{
				throw new InvalidFilePathException(ResourceBundleWrapper.getValue("exceptions", " InvalidFilePath", new Locale(DicomConfig.createInstance().getLocale())) + objFile1);
			}}
		return strStoragePath2;
	}	
}