package cdac.medinfo.sdk.dcm30_04.services.wadors;

import java.io.File;
import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.dicomparserserializers.DicomParser;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;
import cdac.medinfo.sdk.dcm30_04.tools.dcm2xml.DCM2XMLConverter;
/**
 * This class provides the implementation of Wado_RE web service
 * It accepts the request from the browser and generates the response as dicom file
 * for the metadata it generates the file in xml format
 */

@Path("/studies")
public class Wado_RESTful {

/**
 * It accept the studyInstanceUID and retrieve the DICOM file from the service 
 * @param studyInstanceUID
 * @return DICOM file of the studyInstanceUID
 */
	@GET
	@Path("/{studyInstanceUID}")
	@Produces(MediaType.APPLICATION_OCTET_STREAM)	
	//RetrieveStudy
	public Response getFile(@PathParam("studyInstanceUID") String studyInstanceUID)
	{	
		try {
			RetriveDataServer retriveDataServer = new RetriveDataServer();
			System.out.println(studyInstanceUID);
			String filePath = retriveDataServer.getStudyInstanceUID(studyInstanceUID,null,null);
			System.out.println(filePath);
			if(filePath !=null)
			{
				File file = new File(filePath+".dcm");
				ResponseBuilder rb = Response.ok(file);
				rb.header("Content-Disposition", "attachment; filename=" + file.getName());
				Response response = rb.build();
				return response;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * It accept the request from the browser and gives the studyInstanceUID metadata XML file 
	 * @param studyInstanceUID
	 * @return metadata xml file
	 * @throws Exception
	 */
		@GET
		@Path("/{studyInstanceUID}/metadata")
		@Produces(MediaType.APPLICATION_XML)
		
	
		public Response getFileMetadata(@PathParam("studyInstanceUID") String studyInstanceUID) throws Exception
		{	
			try {
				RetriveDataServer retriveDataServer = new RetriveDataServer();
				System.out.println(studyInstanceUID);
				String filePath = retriveDataServer.getStudyInstanceUID(studyInstanceUID,null,null);
				filePath = filePath+".dcm";
				if(filePath != null)
				{
					DCM2XMLConverter objDCM2XMLConverter = new DCM2XMLConverter();
					DicomParser objDicomParser = new DicomParser();
					try {
						IDataSet objSRCDataSet = objDicomParser.parseAsPart10File(filePath,true);
						if(objSRCDataSet != null)
						{		
							objDCM2XMLConverter.serializeXMLDoc(objSRCDataSet,  retriveDataServer.path);
						}
						
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}		
					}
					String xmlFilePath = retriveDataServer.path+studyInstanceUID+".xml";
					File file = new File(xmlFilePath);
					ResponseBuilder rb = Response.ok(file);
					rb.header("Content-Disposition", "attachment; filename=" + file.getName());
					Response response = rb.build();
					return response;
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			
			return null;
			
		}
	/**
	 * It accepts the request from browser with the value of the studyInstanceUID and seriesInstanceUID
	 * It generates the dicom file of the respective request.
	 * @param studyInstanceUID
	 * @param seriesInstanceUID
	 * @return
	 */

	@GET
	@Path("/{studyInstanceUID}/series/{seriesInstanceUID}")
	@Produces(MediaType.APPLICATION_OCTET_STREAM)
	//RetrieveSeries
	public Response getFile(@PathParam("studyInstanceUID") String studyInstanceUID , @PathParam("seriesInstanceUID") String seriesInstanceUID)
	{
		RetriveDataServer retriveDataServer = new RetriveDataServer();
		try {
			String filePath = retriveDataServer.getStudyInstanceUID(studyInstanceUID,seriesInstanceUID,null);
			System.out.println(filePath);
			if(filePath !=null)
			{
				File file = new File(filePath+".dcm");
				ResponseBuilder rb = Response.ok(file);
				rb.header("Content-Disposition", "attachment; filename=" + file.getName());
				Response response = rb.build();
				return response;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * It accepts the request from browser with the value of the studyInstanceUID and seriesInstanceUID 
	 * It generates the metadata XML format file
	 * @param studyInstanceUID
	 * @param seriesInstanceUID
	 * @return metadata file
	 * @throws Exception
	 */
		@GET
		@Path("/{studyInstanceUID}/series/{seriesInstanceUID}/metadata")
		@Produces(MediaType.APPLICATION_XML)
		
		public Response getSeriesMetadata(@PathParam("studyInstanceUID") String studyInstanceUID , @PathParam("seriesInstanceUID") String seriesInstanceUID) throws Exception
		{	
			try {
				RetriveDataServer retriveDataServer = new RetriveDataServer();
				System.out.println(studyInstanceUID);
				String filePath = retriveDataServer.getStudyInstanceUID(studyInstanceUID,seriesInstanceUID,null);
				filePath = filePath+".dcm";
				if(filePath != null)
				{
					DCM2XMLConverter objDCM2XMLConverter = new DCM2XMLConverter();
					DicomParser objDicomParser = new DicomParser();
					try {
						IDataSet objSRCDataSet = objDicomParser.parseAsPart10File(filePath,true);
						if(objSRCDataSet != null)
						{		
							objDCM2XMLConverter.serializeXMLDoc(objSRCDataSet,  retriveDataServer.path);
						}
						
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}		
					}
					String xmlFilePath = retriveDataServer.path+studyInstanceUID+".xml";
					File file = new File(xmlFilePath);
					ResponseBuilder rb = Response.ok(file);
					rb.header("Content-Disposition", "attachment; filename=" + file.getName());
					Response response = rb.build();
					return response;
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	/**
	 * It accepts the request from browser with the value of studyInstanceUID,seriesInstanceUID and sopInstanceUID
	 * It generates the file in the DICOM format
	 * @param studyInstanceUID
	 * @param seriesInstanceUID
	 * @param sopInstanceUID
	 * @return
	 */

	@GET  
	@Path("/{studyInstanceUID}/series/{seriesInstanceUID}/instances/{sopInstanceUID}")
	@Produces(MediaType.APPLICATION_OCTET_STREAM)
	//RetrieveInstance
	public Response getFile(@PathParam("studyInstanceUID") String studyInstanceUID , @PathParam("seriesInstanceUID") String seriesInstanceUID , @PathParam("sopInstanceUID") String sopInstanceUID)
	{
		RetriveDataServer retriveDataServer = new RetriveDataServer();
		try {
			System.out.println(sopInstanceUID);
			String filePath = retriveDataServer.getStudyInstanceUID(studyInstanceUID,seriesInstanceUID,sopInstanceUID);
			System.out.println(filePath);
			if(filePath !=null)
			{
				File file = new File(filePath+".dcm");
				ResponseBuilder rb = Response.ok(file);
				rb.header("Content-Disposition", "attachment; filename=" + file.getName());
				Response response = rb.build();
				return response;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
/**
 * It accepts the request with the value of studyInstanceUID,seriesInstanceUID and sopInstanceUID and keyword metadata
 * It generates the respective metadata file of the sopInstanceUID
 * @param studyInstanceUID
 * @param seriesInstanceUID
 * @param sopInstanceUID
 * @return xml file of the matadata
 * @throws Exception
 */
		@GET
		@Path("/{studyInstanceUID}/series/{seriesInstanceUID}/instances/{sopInstanceUID}/metadata")
		@Produces(MediaType.APPLICATION_XML)
		
		public Response getInstanceMetadata(@PathParam("studyInstanceUID") String studyInstanceUID , @PathParam("seriesInstanceUID") String seriesInstanceUID , @PathParam("sopInstanceUID") String sopInstanceUID) throws Exception
		{	
			try {
				RetriveDataServer retriveDataServer = new RetriveDataServer();
				System.out.println(studyInstanceUID);
				String filePath = retriveDataServer.getStudyInstanceUID(studyInstanceUID,seriesInstanceUID,sopInstanceUID);
				filePath = filePath+".dcm";
				if(filePath != null)
				{
					DCM2XMLConverter objDCM2XMLConverter = new DCM2XMLConverter();
					DicomParser objDicomParser = new DicomParser();
					try {
						IDataSet objSRCDataSet = objDicomParser.parseAsPart10File(filePath,true);
						if(objSRCDataSet != null)
						{		
							objDCM2XMLConverter.serializeXMLDoc(objSRCDataSet,  retriveDataServer.path);
						}
						
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}		
					}
					String xmlFilePath = retriveDataServer.path+studyInstanceUID+".xml";
					File file = new File(xmlFilePath);
					ResponseBuilder rb = Response.ok(file);
					rb.header("Content-Disposition", "attachment; filename=" + file.getName());
					Response response = rb.build();
					return response;
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	
	/**
	 * It accepts the request for the retrieval of the frame data 
	 * @param studyInstanceUID
	 * @param seriesInstanceUID
	 * @param sopInstanceUID
	 * @param frameList
	 * @return the dicom file of the respective sopInstanceUID with the requested frames
	 */
	
	@GET  
	@Path("/{studyInstanceUID}/series/{seriesInstanceUID}/instances/{sopInstanceUID}/frames/{frameList}")
	@Produces(MediaType.APPLICATION_OCTET_STREAM)
	//RetrieveFrames
	public Response getFile(@PathParam("studyInstanceUID") String studyInstanceUID , @PathParam("seriesInstanceUID") String seriesInstanceUID , @PathParam("sopInstanceUID") String sopInstanceUID,@PathParam("frameList") String frameList )
	{
		RetriveFrameData retriveFrameData = new RetriveFrameData();
		try {
			  String[] stringArray = frameList.split(",");
		      int[] intArray = new int[stringArray.length];
		      for (int i = 0; i < stringArray.length; i++) {
		         String numberAsString = stringArray[i];
		         intArray[i] = Integer.parseInt(numberAsString);
		      }
			String filePath = retriveFrameData.retriveFrameService(studyInstanceUID,seriesInstanceUID,sopInstanceUID,intArray);
			System.out.println(filePath);
			if(filePath !=null)
			{
				File file = new File(filePath+".dcm");
				ResponseBuilder rb = Response.ok(file);
				rb.header("Content-Disposition", "attachment; filename=" + file.getName());
				Response response = rb.build();
				return response;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
