package cdac.medinfo.sdk.dcm30_04.services.colorpalette.testcases.scu;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.dicommodel.colorpalette.ColorPaletteIOD;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPDUType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.services.association.AAbort;
import cdac.medinfo.sdk.dcm30_04.services.association.AReleaseRQorRP;
import cdac.medinfo.sdk.dcm30_04.services.association.AssociateRQ;
import cdac.medinfo.sdk.dcm30_04.services.association.DefaultAssociation;
import cdac.medinfo.sdk.dcm30_04.services.association.RQPresContext;
import cdac.medinfo.sdk.dcm30_04.services.cstore.commons.CStoreRQ;
import cdac.medinfo.sdk.dcm30_04.services.cstore.commons.CStoreRSP;
import cdac.medinfo.sdk.dcm30_04.services.cstore.scu.CStoreSCU;
import cdac.medinfo.sdk.dcm30_04.services.scu.commons.SCUSession;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

public class ColorPalete_CStore_SCU {

	public static void main(String[] args) {

		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 instance of CStore_SCU
		 */
		ColorPalete_CStore_SCU objColorPalette_CStore_SCU = new ColorPalete_CStore_SCU();

		/*
		 * Starts C-Store SCU.
		 */
		objColorPalette_CStore_SCU.start();

	}

	/**
	 * This function performs the CStore operation.
	 */
	public void start() {
		// Current SCU Session
		SCUSession objSession = null;

		try {
			// Enter the Palette Color DICOM File with full path
			String strFilePath = null;
			System.out.println("Enter the  Palette Color Dicom File with complete path");
			BufferedReader b_reader = new BufferedReader(new InputStreamReader(System.in));
			strFilePath = b_reader.readLine();

			// Creates the object of corresponding IOD by calling its
			// constructor which takes File Name as a parameter and give its
			// DataSet.
			ColorPaletteIOD objColorPaletteIOD = new ColorPaletteIOD(strFilePath);

			IDataSet objIDataSet = objColorPaletteIOD.getDataSet();

			// Create SCUSession object
			objSession = new SCUSession();

			/*
			 * Creating RQPresContext for which C-Store is to be performed.
			 */
			ISingleCollection<RQPresContext> objRQPresContextCol = new SingleCollection<RQPresContext>();

			// RQ Pres Context for X-Ray
			RQPresContext objRQPresContextXRay = new RQPresContext();
			objRQPresContextXRay.setAbstractSyntaxName(EnumSOPClassUIDs.COLORPALETTEIOD.getValue());
			objRQPresContextXRay.addTransferSyntax(DicomConstants.TS_EXPLICIT_VR_LITTLE_ENDIAN);
			objRQPresContextCol.add(objRQPresContextXRay);

			/*
			 * Load Default Association for RQPresContextProvided.
			 */
			DefaultAssociation objDefaultAssociation = new DefaultAssociation("SDK");
			objDefaultAssociation.loadAssociation(objRQPresContextCol);

			// Create Association RQ
			AssociateRQ objAssociateRQ = objDefaultAssociation.getAssociationRQ("SDK");

			// Start SCU Session by providing the IP address and Port of SCP.
			objSession.start("192.168.8.94", 9000);

			// Create CStore SCU object using the current SCU Session.
			CStoreSCU objCStoreSCU = new CStoreSCU(objSession);

			// Send the Association Request
			if (objSession.sendAssociationMessage(objAssociateRQ)) {
				// When association is successfully established the
				// "objSession.sendAssociationMessage()" method returns true.

				System.out.println("Association established");

				// Create Default CStore RQ by giving the parsed DataSet. This
				// method uses
				// default values for the attributes in the request. These
				// attributes can be
				// modified once the request object is created, before sending.
				CStoreRQ objCStoreRQ = CStoreRQ.getDefaultRQ(objIDataSet);

				System.out.println("CStore Request initialized");

				// Send the C-Store-RQ and wait for a C-Store-RSP.

				if (objCStoreSCU.sendMessage(objCStoreRQ))
					System.out.println("CStore Response received");
				else
					System.out.println("CStore Operation not performed");

				// Get the Response Size collection from C-Store SCU.
				System.out.println("Response Collection Size ==== " + objCStoreSCU.getDIMSEResponse().size());

				// Printing Status of C-Store Request.
				if (objCStoreSCU.getDIMSEResponse().size() > 0) {
					CStoreRSP objCStoreRSP = new CStoreRSP(objCStoreSCU.getDIMSEResponse().get(0));
					System.out.println("C-Store Response Status is :" + Integer.toHexString(objCStoreRSP.getStatus()));
				}

				// Create A-Release-RQ to release the association
				AReleaseRQorRP objAReleaseRQorRP = new AReleaseRQorRP(EnumPDUType.RELEASE_RQ);

				// Send the A-Release-RQ
				if (objSession.sendAssociationMessage(objAReleaseRQorRP)) {
					System.out.println("Association is released");
					// When successful A-Release-RP is received, close the
					// Session.
					// Please note that instead of closing the session we can
					// create
					// another association and send more requests. Also it is
					// possible
					// to create multiple C-Store-RQs and send them one by one.
					objSession.close();
				}

				// In case an ABORT is received from SCP during the C-Store
				// operation
				if (objSession.getDIMSEAssociation() instanceof AAbort) {
					System.out.println("Abort Arrives. Closing SCU session.");
					objSession.close();
				}
			} else {
				System.out.println("Association cannot be established");
				objSession.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			objSession.close();
		}
	}

}
