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

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

/**
 * This class contains Macro that describes attributes of the Performed Procedure Step Summary.
 * @author saloni
 *
 */
public class PerformedProcedureStepSummaryMacro extends Macro 
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public PerformedProcedureStepSummaryMacro(IDataSet objDataSet)
	{
		super(objDataSet);
	}

	/**
	 * This constructor constructs a Macro with specified DataSet and DictionaryReader
	 * @param objDataSet specifies the DataSet
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public PerformedProcedureStepSummaryMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public PerformedProcedureStepSummaryMacro(IItem objItem)
	{
		super(objItem);
	}

	/**
	 * This constructor constructs a Macro with the specified Item and DictionaryReader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public PerformedProcedureStepSummaryMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Validates all the attributes in PersonIdentificationMacro
	 * @return boolean - Success -true, Failure -false.
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate() throws InvalidMacroException
	{
		if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
			return validate(objIDataSet);
		else if(iMacroLevel == DicomConstants.DCM_ITEM_LEVEL_MACRO)
			return validate(objIItem);
		return false;
	}

	/**
	 * Validates all the attributes in PersonIdentificationMacro that are present in the DataSet
	 * @param objIItem collection of DataElements in a sequence
	 * @return int - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate(IItem objIItem) throws InvalidMacroException
	{
		return true;
	}

	/**
	 * Validates all the attributes in PersonIdentificationMacro that are present in the DataSet
	 * @param objIDataSet collection of DataElements
	 * @return int - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		return true;
	}
	
	/**
	 * Sets the Performed Procedure Step ID.
	 * @param strPerformedProcedureStepID
	 */
	public void setPerformedProcedureStepID(String strPerformedProcedureStepID)
	{
		/*Accept the Performed Procedure Step ID in String format of type SH.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040, 0x0253, strPerformedProcedureStepID,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0x0253,DicomConstants.DCM_DT_SH, strPerformedProcedureStepID);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Performed Procedure Step ID
	 * @return String - value of Performed Procedure Step ID
	 */
	public String getPerformedProcedureStepID()
	{
		Object objValue = getValue(0x0040, 0x0253);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Performed Procedure Step StartDate
	 * @param strPerformedProcedureStepStartDate String value of Performed Procedure Step StartDate
	 */
	public void setPerformedProcedureStepStartDate(String strPerformedProcedureStepStartDate)
	{
		/* Accept the Performed Procedure Step StartDate of type DA.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0x0244,strPerformedProcedureStepStartDate,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0x0244,DicomConstants.DCM_DT_DA, strPerformedProcedureStepStartDate);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Performed Procedure Step StartDate
	 * @return String - value of Performed Procedure Step StartDate
	 */
	public String getPerformedProcedureStepStartDate()
	{
		Object objValue = getValue(0x0040,0x0244);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Performed Procedure Step StartTime
	 * @param strPerformedProcedureStepStartTime String of Performed Procedure Step StartTime
	 */
	public void setPerformedProcedureStepStartTime(String strPerformedProcedureStepStartTime)
	{
		/* Accept Performed Procedure Step StartTime of type TM.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0x0245,strPerformedProcedureStepStartTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0x0245,DicomConstants.DCM_DT_TM,strPerformedProcedureStepStartTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Performed Procedure Step StartTime
	 * @return String - value of Performed Procedure Step StartTime
	 */
	public String getPerformedProcedureStepStartTime()
	{
		Object objValue = getValue(0x0040,0x0245);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Performed Procedure Step EndDate
	 * @param strPerformedProcedureStepEndDate String value of Performed Procedure Step EndDate
	 */
	public void setPerformedProcedureStepEndDate(String strPerformedProcedureStepEndDate)
	{
		/* Accept the Performed Procedure Step EndDate of type DA.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0x0250,strPerformedProcedureStepEndDate,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0x0250,DicomConstants.DCM_DT_DA, strPerformedProcedureStepEndDate);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Performed Procedure Step EndDate
	 * @return String - value of Performed Procedure Step EndDate
	 */
	public String getPerformedProcedureStepEndDate()
	{
		Object objValue = getValue(0x0040,0x0250);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Performed Procedure Step EndTime
	 * @param strPerformedProcedureStepEndTime String of Performed Procedure Step EndTime
	 */
	public void setPerformedProcedureStepEndTime(String strPerformedProcedureStepEndTime)
	{
		/* Accept Performed Procedure Step StartTime of type TM.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0x0251,strPerformedProcedureStepEndTime,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0x0251,DicomConstants.DCM_DT_TM,strPerformedProcedureStepEndTime);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Performed Procedure Step EndTime
	 * @return String - value of Performed Procedure Step EndTime
	 */
	public String getPerformedProcedureStepEndTime()
	{
		Object objValue = getValue(0x0040,0x0251);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Performed Procedure StepDescription
	 * @param strPerformedProcedureStepDescription String value of Performed Procedure StepDescription
	 */
	public void setPerformedProcedureStepDescription(String strPerformedProcedureStepDescription)
	{
		/* Accept the Performed Procedure StepDescription in String format of type LO.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040,0x0254, strPerformedProcedureStepDescription,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040,0x0254,DicomConstants.DCM_DT_LO, strPerformedProcedureStepDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return Performed Procedure StepDescription
	 * @return String - value of Performed Procedure StepDescription.
	 */
	public String getPerformedProcedureStepDescription()
	{
		Object objValue = getValue(0x0040,0x0254);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Retrieves pre-initialized object of Performed Protocol Code Sequence.If the module contains the sequence object it returns the same otherwise it creates the sequence adds to the module with proper initialization and then returns 	the object of sequence.
	 * @return PerformedProtocolCodeSequence - instance of PerformedProtocolCodeSequence. 
	 */
	public PerformedProtocolCodeSequence getPerformedProtocolCodeSequence()
	{
		PerformedProtocolCodeSequence objPerformedProtocolCodeSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0040, 0x0260); 
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objPerformedProtocolCodeSequence = (PerformedProtocolCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objPerformedProtocolCodeSequence = (PerformedProtocolCodeSequence)objDicomV3Factory.createSequence(0x0040, 0x0260, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0040, 0x0260, objPerformedProtocolCodeSequence.getValue(), objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0040, 0x0260, DicomConstants.DCM_DT_SQ, objPerformedProtocolCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objPerformedProtocolCodeSequence;
	}
	
	/**
	 * Sets the User-defined comments on the Performed ProcedureStep.
	 * @param strCommentsOnThePerformedProcedureStep User-defined comments on the Performed ProcedureStep.
	 */
	public void setCommentsOnThePerformedProcedureStep(String strCommentsOnThePerformedProcedureStep)
	{
		/*Accept the CommentsOnThePerformedProcedureStep in String format of type ST.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0040, 0x0280, strCommentsOnThePerformedProcedureStep,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0040, 0x0280,DicomConstants.DCM_DT_ST, strCommentsOnThePerformedProcedureStep);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the User-defined comments on the Performed ProcedureStep.
	 * @return String value of Radiation Dose for User-defined comments on the Performed ProcedureStep.
	 */
	public String getCommentsOnThePerformedProcedureStep()
	{
		Object objValue = getValue(0x0040, 0x0280);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
