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



import java.util.Locale;

import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumCommandField;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumErrorComment;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.ICommandElement;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.IStatus;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.InvalidDICOMMessageException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;


/**
 * The class DIMSE Request specifies common attributes of DIMSERequest for all services.
 */
public class DIMSEResponse extends DicomMessage 
{

	/**
	 * Sets Group Length i.e The even number of bytes from the end of the value field to the beginning of the next group
	 * @param lGroupLength value of group length
	 */
	public void setCommandGroupLength(int lCommandGroupLength)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0000,lCommandGroupLength);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves the Group Length .
	 * The even number of bytes from the end of the value field to the beginning of the next group
	 * @return int - value of Group Length
	 */
	public int getCommandGroupLength()
	{
		Object objValue = getValue(0x0000,0x0000);
		if(objValue != null)
			return Integer.parseInt(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Affected SOP Class UID . 
	 * SOP Class UID associated with the operation.
	 * @param strAffectedSOPClassUID value of affected SOP Class UID.
	 */
	public void setAffectedSOPClassUID(String strAffectedSOPClassUID)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0002,strAffectedSOPClassUID);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves Affected SOP Class UID . 
	 * SOP Class UID associated with the operation.
	 * @return String - value of Affected SOP Class UID .
	 */
	public String getAffectedSOPClassUID()
	{
		Object objValue = getValue(0x0000,0x0002);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Command Field. 
	 * This field distinguishes the DIMSE-C operation conveyed by this Message. 
	 * The value of this field shall be set to 8030H for the C-ECHO-RSP Message.
	 * @param enumCommandField the enum command field
	 */ 
	public void setCommandField(EnumCommandField enumCommandField)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0100,enumCommandField.getValue());
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves Command Field.
	 * @return short - value of command field
	 */
	public short getCommandField()
	{
		Object objValue = getValue(0x0000,0x0100);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * This field indicates that no Data Set is present in the Message and shall be set to a value of 0101H.
	 * @param shDataSetType value of DataSet type.
	 */
	public void setCommandDataSetType(short shDataSetType)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0800,shDataSetType);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves DataSet type.
	 * @return short - value of DataSet type.
	 */
	public short getCommandDataSetType()
	{
		Object objValue = getValue(0x0000,0x0800);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets Message ID Being Responded To. 
	 * Shall be set to the value of the Message ID (0000,0110) field used in associated C-ECHO RQ Message.
	 * @param shMessageIDBeingRespondedTo value of Message ID Being Responded To.
	 */
	public void setMessageIDBeingRespondedTo(short shMessageIDBeingRespondedTo)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0120,shMessageIDBeingRespondedTo);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves Message ID Being Responded To.
	 * @return short - value of Message ID Being Responded To.
	 */
	public short getMessageIDBeingRespondedTo()
	{
		Object objValue = getValue(0x0000,0x0120);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * This Indicates the status of the response. It shall have a value of Success.
	 * @param enumStatus the enum status
	 */
	public void setStatus(IStatus enumStatus)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0900,enumStatus.getValue());
		objICommandSet.addCommandElement(objICommandElement);
	}
	
	/**
	 * This Indicates the status of the response. It shall have a value of Success.
	 * @param short the value of status
	 */
	public void setStatus(short shStatus)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000, 0x0900,shStatus);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves the status of the Response.
	 * @return short - value of status.
	 */
	public short getStatus()
	{
		Object objValue = getValue(0x0000,0x0900);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets collection of OffendingElements which does not support Dicom Protocol rules.
	 * @param objITagCollection collection of OffendingElements
	 */
	public void setOffendingElement(ISingleCollection<ITag> objITagCollection)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000,0x0901,objITagCollection);
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves the status of the response.
	 * @return short - value of status.
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<ITag> getOffendingElement()
	{
		Object objValue = getValue(0x0000,0x0901);
		if(objValue != null)
			return (ISingleCollection<ITag>)objValue;
		return null;
	}

	/**
	 * Sets ErrorComment . An application specific text description of the error detected.
	 * @param enumErrorComment value of ErrorComment.
	 */
	public void setErrorComment(EnumErrorComment enumErrorComment)
	{
		ICommandElement objICommandElement = null;
		objICommandElement = new CommandElement(0x0000,0x0902,enumErrorComment.getValue());
		objICommandSet.addCommandElement(objICommandElement);
	}

	/**
	 * Retrieves ErrorComment . An application specific text description of the error detected.
	 * @return String - value of ErrorComment .
	 */
	public String getErrorComment()
	{
		Object objValue = getValue(0x0000,0x0902);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Validates command set for their mandatory attributes which are common in all services response.
	 * @return true, if validation is successful.
	 * @throws InvalidDICOMMessageException If validation of mandatory fields fails.
	 */
	public boolean validate() throws InvalidDICOMMessageException
	{
		super.validate();
		try
		{	
			//if DataSet is present then set DataSetType to DataSet Present otherwise not present.
			if(objIDataSet != null && false == objIDataSet.isEmpty())
				setCommandDataSetType(DicomConstants.DCM_DATASET_PRESENT);
			else
				setCommandDataSetType(DicomConstants.DCM_DATASET_NOT_PRESENT);
			
			if(!objICommandSet.isPresent(0x0000,0x0120))			// Message ID Being Responded
				throw new MandatoryMissingException("0000,0120");
			else
			{
				if(objICommandSet.getValue(0x0000,0x0120) == null)
					throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions", "NullValue",new Locale(DicomConfig.createInstance().getLocale())) + 0x0000 + "," + 0x0120);
			}
			
			if(!objICommandSet.isPresent(0x0000,0x0900))		// Status
				throw new MandatoryMissingException("0000,0900");
			else
			{
				if(objICommandSet.getValue(0x0000,0x0900) == null)
					throw new InvalidDICOMMessageException(ResourceBundleWrapper.getValue("exceptions", "NullValue",new Locale(DicomConfig.createInstance().getLocale())) + 0x0000 + "," + 0x0900);
			}
			
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

			throw new InvalidDICOMMessageException(this.getClass().getSimpleName(),mme);

		}	
		return true;
	}
}