/*******************************************************************************
 *  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.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;


/**
 * This class is for identification of correspondence between areas of Pixel Data(07FE,0010) and frames of higher resolution images.
 */
public class ReferencedImageNavigationSequenceItem extends SOPInstanceReferenceMacroDependentItem
{
	


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax.
	 */
	ReferencedImageNavigationSequenceItem()
	{
		super();
	}

	/*
	 * This constructor calls the respective superclass constructor
	 * This constructor should throw exception if null TS is tried to be set
	 */
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax. 
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax. 
	 */
	ReferencedImageNavigationSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates ReferencedImageNavigationSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader.
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax.
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used. 
	 */
	ReferencedImageNavigationSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	/**
	 * Sets Referenced Frame Number within Referenced Image. Only asingle value shall be present.
	 * @param strReferencedFrameNumber String Value component of a Name/Value pair when the Concept implied by Concept Name
	 */
	public void setReferencedFrameNumber(String strReferencedFrameNumber)
	{
		/*Accept the ReferencedFrameNumber in String format of type IS.
		 * 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(0x0008, 0x1160, strReferencedFrameNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008, 0x1160,DicomConstants.DCM_DT_CS, strReferencedFrameNumber);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Referenced Frame Number within Referenced Image. Only asingle value shall be present.
	 * @return String Array -  Value of ReferencedFrameNumber
	 */
	public String[] getReferencedFrameNumber()
	{
		if(getDataForTag(0x0008, 0x1160)!=null)
		{
			if(getDataForTag(0x0008, 0x1160).getValue()!=null)
			{
				Object objValue = getDataForTag(0x0008, 0x1160).getValue();
				if(objValue!=null)
				{
					if(objValue instanceof String)
					{
						String strTemp = (String)objValue;
						String[] strArr = new String[1];
						strArr[0] = strTemp;
						return strArr;
					}
					else
						return (String[])objValue;
				}
			}
		}
		return null;
	}
	public void setTopLeftHandCornerOfLocalizerArea(short[] arrShTopLeftHandCornerOfLocalizerArea)
	{
		/*Accept the TopLeftHandCornerOfLocalizerArea in Array of short format of type US.
		 * 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(0x0048, 0x0201, arrShTopLeftHandCornerOfLocalizerArea,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0048, 0x0201,DicomConstants.DCM_DT_US, arrShTopLeftHandCornerOfLocalizerArea);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Top Left Hand Corner of Localizer image pixel areacorresponding to referenced frame,specified as column\row.Column is the horizontal offset and row is the vertical offset.
	 * @return short Array - TopLeftHandCornerOfLocalizerArea.
	 */
	public short[] getTopLeftHandCornerOfLocalizerArea()
	{
		Object objValue = getValue(0x0048, 0x0201);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
	public void setBottomRightHandCornerOfLocalizerArea(short[] arrShBottomRightHandCornerOfLocalizerArea)
	{
		/*Accept the BottomRightHandCornerOfLocalizerArea in Array of short format of type US.
		 * 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(0x0048, 0x0202, arrShBottomRightHandCornerOfLocalizerArea,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0048, 0x0202,DicomConstants.DCM_DT_US, arrShBottomRightHandCornerOfLocalizerArea);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Top Left Hand Corner of Localizer image pixel areacorresponding to referenced frame,specified as column\row.Column is the horizontal offset and row is the vertical offset.
	 * @return short Array - TopLeftHandCornerOfLocalizerArea.
	 */
	public short[] getBottomRightHandCornerOfLocalizerArea()
	{
		Object objValue = getValue(0x0048, 0x0202);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}

	/**
	 * Sets Pixel Spacing in Referenced Image,specified as a numeric pair- adjacent row spacing \ adjacent column spacing, in mm.
	 * @param strPixelSpacing String value of Pixel Spacing.
	 */
	public void setPixelSpacing(String[] strPixelSpacing)
	{
		/*Accept the PixelSpacing in String format of type DS.
		 * 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(0x0028,0x0030, strPixelSpacing,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x0030,DicomConstants.DCM_DT_DS, strPixelSpacing);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Pixel Spacing in Referenced Image,specified as a numeric pair- adjacent row spacing \ adjacent column spacing, in mm.
	 * @return String - value of Pixel Spacing.
	 */
	public String[] getPixelSpacing()
	{
		Object objValue = getValue(0x0028,0x0030);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	/**
	 * Sets the  Z offset of the referenced frame from the Slide CoordinatesSystem reference plane, in microns
	 * @param strZOffsetInSlideCoordinateSystem String value of Z Offset in Slide CoordinateSystem.
	 */
	public void setZOffsetInSlideCoordinateSystem(String strZOffsetInSlideCoordinateSystem)
	{
		/*Accept the ZOffsetInSlideCoordinateSystem in String format of type DS.
		 * 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,0x074A, strZOffsetInSlideCoordinateSystem,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0x074A,DicomConstants.DCM_DT_DS, strZOffsetInSlideCoordinateSystem);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the  Z offset of the referenced frame from the Slide CoordinatesSystem reference plane, in microns
	 * @return String - value of Z Offset in Slide CoordinateSystem
	 */
	public String getZOffsetInSlideCoordinateSystem()
	{
		Object objValue = getValue(0x0040,0x074A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the number of samples (color planes) containing information inthe referenced image.
	 * @param shSamplesPerPixel short value of Samples per Pixel
	 */
	public void setSamplesPerPixel(short shSamplesPerPixel)
	{
		/* Accept the Samples per Pixel of type US.
		 * 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(0x0028,0x0002,shSamplesPerPixel,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x0002,DicomConstants.DCM_DT_US, shSamplesPerPixel);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the number of samples (color planes) containing information inthe referenced image.
	 * @return short - short value of Samples per Pixel
	 */
	public short getSamplesPerPixel()
	{
		Object objValue = getValue(0x0028,0x0002);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the optical path used in acquiring the referenced Frameby reference to an Item in the Optical Path Sequence(0048,0105) of the current SOP Instance.
	 * @param strOpticalPathIdentifier
	 */
	public void setOpticalPathIdentifier(String strOpticalPathIdentifier)
	{
		/*Accept the Optical Path Identifier in String format of type SH.
		 * 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(0x0048, 0x0106, strOpticalPathIdentifier,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0048, 0x0106,DicomConstants.DCM_DT_SH, strOpticalPathIdentifier);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the optical path used in acquiring the referenced Frameby reference to an Item in the Optical Path Sequence(0048,0105) of the current SOP Instance.
	 * @return String - value of Optical Path Identifier.
	 */
	public String getOpticalPathIdentifier()
	{
		Object objValue = getValue(0x0048, 0x0106);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Validates all the attributes in ReferencedImageNavigationSequenceItem that are present in the DataSet.
	 * @return boolean - returns validation success as success or Failure
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try{
			if(!contains(0x0008, 0x1160))
				throw new MandatoryMissingException("0008,1160");
			if(!contains(0x0048, 0x0201))
				throw new MandatoryMissingException("0048,0201");
			if(!contains(0x0048, 0x0202))
				throw new MandatoryMissingException("0048,0202");
			if(!contains(0x0028, 0x0030))
				throw new MandatoryMissingException("0028,0030");
			if(!contains(0x0040, 0x074A))
				throw new MandatoryMissingException("0040,074A");
			if(!contains(0x0028, 0x0002))
				throw new MandatoryMissingException("0028,0002)");
			if(!contains(0x0048, 0x0106))
				throw new MandatoryMissingException("0048,0106");

		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}

	
}
