/*******************************************************************************
 *  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.exceptions.ValidationException;
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 item class contains the methods to set, get and validate the attribute present in Plane Position (Slide) Sequence.
 */


public class PlanePositionSlideSequenceItem extends Item
{
	

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

	/**
	 * This constructor creates the item with default TransferSyntax 
	 */
	PlanePositionSlideSequenceItem()
	{
		super();
	}


	/**
	 * This constructor creates the item with specified TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	PlanePositionSlideSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates PlanePositionSlideSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	PlanePositionSlideSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}
	
	/*
	 * (non-Javadoc) This mathod validates the contents of the Item
	 * @see cdac.medinfo.sdk.dcm30_04.commoninterface.IItem#validate()
	 */
	/**
	 * Validates all the attributes in PlanePositionSlideSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try{
			if(!contains(0x0048,0x021E))
				throw new MandatoryMissingException("0048,021E");
			if(!contains(0x0048,0x021F))
				throw new MandatoryMissingException("0048,021F");
			if(!contains(0x0040,0x072A))
				throw new MandatoryMissingException("0040,072A");
			if(!contains(0x0040,0x073A))
				throw new MandatoryMissingException("0040,073A");
			if(!contains(0x0040,0x074A))
				throw new MandatoryMissingException("0040,074A");
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
	
	/**
	 * Sets the Column Position In Total Image Pixel Matrix.
	 * @param lColumnPositionInTotalImagePixelMatrix Long array of Column Position In Total Image Pixel Matrix.
	 */
	public void setColumnPositionInTotalImagePixelMatrix(Long[] lColumnPositionInTotalImagePixelMatrix)
	{
		/* 
		 * Accept the Column Position In Total Image Pixel Matrix in String format of type SL.
		 * 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,0x021E, lColumnPositionInTotalImagePixelMatrix,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0048,0x021E,DicomConstants.DCM_DT_SL, lColumnPositionInTotalImagePixelMatrix);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Column Position In Total Image Pixel Matrix.
	 * @return Long[] - Long array of Column Position In Total Image Pixel Matrix.
	 */
	public Long[] getColumnPositionInTotalImagePixelMatrix()
	{
		Object objValue = getValue(0x0048,0x021E);
		if(objValue != null)
			return (Long[])objValue;
		return null;
	}
	
	/**
	 * Sets the Row Position In Total Image Pixel Matrix.
	 * @param lRowPositionInTotalImagePixelMatrix Long array of Row Position In Total Image Pixel Matrix.
	 */
	public void setRowPositionInTotalImagePixelMatrix(Long[] lRowPositionInTotalImagePixelMatrix)
	{
		/* 
		 * Accept the Row Position In Total Image Pixel Matrix in String format of type SL.
		 * 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,0x021F, lRowPositionInTotalImagePixelMatrix,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0048,0x021F,DicomConstants.DCM_DT_SL, lRowPositionInTotalImagePixelMatrix);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Row Position In Total Image Pixel Matrix.
	 * @return Long[] - Long array of Row Position In Total Image Pixel Matrix.
	 */
	public Long[] getRowPositionInTotalImagePixelMatrix()
	{
		Object objValue = getValue(0x0048,0x021F);
		if(objValue != null)
			return (Long[])objValue;
		return null;
	}
	
	/**
	 * Sets X offset in Slide Coordinate System.
	 * The X offset in millimeters from the Origin of the Slide CoordinateSystem.
	 * @param strXoffsetinSlideCoordinateSystem String value of X offset in Slide Coordinate System.
	 */
	public void setXoffsetinSlideCoordinateSystem(String strXoffsetinSlideCoordinateSystem)
	{
		/*Accept the X offset in Slide Coordinate System 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,0x072A, strXoffsetinSlideCoordinateSystem,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0x072A,DicomConstants.DCM_DT_DS, strXoffsetinSlideCoordinateSystem);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves X offset in Slide Coordinate System.
	 * The X offset in millimeters from the Origin of the Slide CoordinateSystem.
	 * @return String - value of X offset in Slide Coordinate System.
	 */
	public String getXoffsetinSlideCoordinateSystem()
	{
		Object objValue = getValue(0x0040,0x072A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Y offset in Slide Coordinate System.
	 * The Y offset in millimeters from the Origin of the Slide CoordinateSystem.
	 * @param strYoffsetinSlideCoordinateSystem String value of Y offset in Slide Coordinate System.
	 */
	public void setYoffsetinSlideCoordinateSystem(String strYoffsetinSlideCoordinateSystem)
	{
		/*Accept the Y offset in Slide Coordinate System 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,0x073A, strYoffsetinSlideCoordinateSystem,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0x073A,DicomConstants.DCM_DT_DS, strYoffsetinSlideCoordinateSystem);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Y offset in Slide Coordinate System.
	 * The Y offset in millimeters from the Origin of the Slide CoordinateSystem.
	 * @return String - value of Y offset in Slide Coordinate System.
	 */
	public String getYoffsetinSlideCoordinateSystem()
	{
		Object objValue = getValue(0x0040,0x073A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Z offset in Slide Coordinate System.
	 * The Z offset in μm from the Origin of the Slide Coordinate System,
	 * nominally the surface of the glass slide substrate.
	 * @param strZoffsetinSlideCoordinateSystem String value of Z offset in Slide Coordinate System.
	 */
	public void setZoffsetinSlideCoordinateSystem(String strZoffsetinSlideCoordinateSystem)
	{
		/*Accept the Z offset in Slide Coordinate System 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 Z offset in Slide Coordinate System.
	 * The Z offset in μm from the Origin of the Slide Coordinate System,
	 * nominally the surface of the glass slide substrate.
	 * @return String - value of Z offset in Slide Coordinate System.
	 */
	public String getZoffsetinSlideCoordinateSystem()
	{
		Object objValue = getValue(0x0040,0x074A);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
