/*******************************************************************************
 *  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.cservices.testcases.scu;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

import java.io.IOException;
import java.net.InetSocketAddress;


import cdac.medinfo.sdk.dcm30_04.baselibs.DataSet;
import cdac.medinfo.sdk.dcm30_04.baselibs.ImplicitDE;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;

import cdac.medinfo.sdk.dcm30_04.services.cfind.commons.CFindRSP;
import cdac.medinfo.sdk.dcm30_04.services.cfind.scu.CFindSCU;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DicomMessage;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

/**
 * This class tests a Stand Alone CFind SCU.
 * "Stand Alone" means that the User is required to write a minimal set of code to actually send a 
 * CFind Request. Once the required parameters are given to the CFindSCU object it is 
 * capable of making a connection to the SCP, establishing association, sending the CFind Request and receiving the 
 * DIMSE Response for the same. If User wants to handle the request sending scenarios by his/her code 
 * then please refer to sample code "CFind_SCU.java". 
 */
public class CFind_SCU_Standalone 
{
	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");

			/*
			 * Creates object of CFind_SCU_Standalone and start CFind operation.
			 */
			CFind_SCU_Standalone objCFindSCUTest = new CFind_SCU_Standalone();
			objCFindSCUTest.doCFind();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * This function initiates C-Find SCU and sends C-Find Request.
	 * @throws IOException signals IOException if IO Operation fails.
	 */
	public void doCFind() throws IOException
	{
		/* 
		 * Creates SCU with specified destination .
		 */
		InetSocketAddress addr = new InetSocketAddress("192.168.8.94",9000);
		CFindSCU objCFindSCU = new CFindSCU(addr);

		/*
		 * Send the C-Find DataSet with Affected SOP class UID 
		 */
		String strAffectedSOPClassUID = DicomConstants.DCM_STUDY_ROOT_QUERY_RETRIEVE_INFORMATION_MODEL_FIND;
		objCFindSCU.send("SDK","SDK",strAffectedSOPClassUID,getCFindDatasets());

		/*
		 * After the request is send user can get the response collection.
		 */
		System.out.println("CFind Operation is performed . Response Collection Size ==== " + objCFindSCU.getDIMSEResponse().size());

		ISingleCollection<DicomMessage> objSingleCollection = objCFindSCU.getDIMSEResponse();

		for(DicomMessage objDicomMessage : objSingleCollection)
		{
			CFindRSP objCFindRSP = new CFindRSP(objDicomMessage);
			System.out.println("C-Find Response Status :" + objCFindRSP.getStatus() );
			IDataSet objIDataSet2 = objDicomMessage.getDataSet();
			if(objIDataSet2 != null)
				display(objIDataSet2);
		}
	}

	/**
	 * This function retrieves the data set collection to be sent in C-Find request. 
	 * The data set contains the identifier.
	 * @return ISingleCollection<IDataSet> Data set collection which is to be sent in C-Move request.
	 * @throws IOException signals IOException if IO Operation fails.
	 */
	ISingleCollection<IDataSet> getCFindDatasets() throws IOException
	{
		ISingleCollection<IDataSet> objDataSetCollection = new SingleCollection<IDataSet>();
		IDataSet objIDataSet = new DataSet();
		IDictionaryReader objIDictionaryReader = objIDataSet.getDictionaryReader();
		IDataElement objIDataElement = new ImplicitDE(0x0008,0x0020,"19930325",objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objIDataElement = new ImplicitDE(0x0020,0x1208,null,objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objIDataElement = new ImplicitDE(0x0008,0x0030,null,objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objIDataElement = new ImplicitDE(0x0008,0x0050,null,objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objIDataElement = new ImplicitDE(0x0008,0x0052,"STUDY",objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objIDataElement = new ImplicitDE(0x0020,0x000d,null,objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objIDataElement = new ImplicitDE(0x0020,0x0010,null,objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objIDataElement = new ImplicitDE(0x0020,0x1206,null,objIDictionaryReader);
		objIDataSet.addDataElement(objIDataElement);
		objDataSetCollection.add(objIDataSet);
		return objDataSetCollection;
	}

	static int i = 0;

	/**
	 * Display the pending data set.
	 */
	private void display(IDataSet objIDataSet)
	{
		for(IDataElement objIDataElement : objIDataSet)
		{
			if(objIDataElement != null)
			{
				if(objIDataElement.getTag() != null)
				{
					System.out.println("Group No " + objIDataElement.getTag().getGroupNumber());
					System.out.println("Element No " + objIDataElement.getTag().getElementNumber());
					if(objIDataElement.getDicomValue() != null)
					{
						if(objIDataElement.getDicomValue().getValue() != null)
						{
							System.out.println("Value is " +objIDataElement.getDicomValue().getValue().toString());
						}
						else
						{
							System.out.println("Value is " + null);
						}
					}
				}

			}
		}
	}

}
