/*******************************************************************************
 *  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.minquirefile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.Locale;

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 contains methods to get creation or modification Date & Time.
*/
public class MInquireFile
{
	/**
	 * This function gives creation date & time and display .
	 * @throws IOException 
	 */
			public void processTime(String filePath) throws IOException
			{

				File file = new File(filePath);
					
				if (file.exists()) 
				{
						Path path = file.toPath();
						BasicFileAttributes attributes = Files.readAttributes(path,BasicFileAttributes.class);
						// Getting file creation time.
						FileTime time = attributes.creationTime();
						String dateTime = getStringFromFileTime(time);
						DicomLogger objDicomLogger = DicomLogger.getLogger();
						objDicomLogger.log("Last Modification Date & Time is :"+ dateTime);

				}else 
				{
					DicomLogger objDicomLogger = DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("warning","DICOMDIR.FileNotFound",new Locale(DicomConfig.createInstance().getLocale())));
				}
			}
				
			private static String getStringFromFileTime(FileTime time) 
			{

					// Getting milliseconds.
					long milliseconds = time.toMillis();
					Date date = new Date();
					// setting milliseconds.
					date.setTime(milliseconds);
					SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
					// Convert Date to String.
					String dateTime = dateFormat.format(date);
					return dateTime;

			}
	 
}
