/*******************************************************************************
 *  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.enums.EnumExposureControlSensingRegionShape;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
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 item class contains the methods to set, get and validate the attribute present in Exposure Control Sensing Regions Sequence .
 */

public class ExposureControlSensingRegionsSequenceItem extends Item
{
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	ExposureControlSensingRegionsSequenceItem()
	{
		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 
	 */
	ExposureControlSensingRegionsSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates ExposureControlSensingRegionsSequenceItem with specified TransferSyntax and
	 * specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	ExposureControlSensingRegionsSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Validates all the attributes in FieldOfViewSequenceItem 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(0x0018,0x9435))
				throw new MandatoryMissingException("0018,9435");
			if((getDataForTag(0x0018, 0x9435).getValue().toString().equals(EnumExposureControlSensingRegionShape.RECTANGULAR)))
				if(!contains(0x0018,0x9436))
					throw new ConditionFailedException("value for conditional Element 0x0018,0x9436 required if value of 0018,9435 is RECTANGULAR");
			if((getDataForTag(0x0018, 0x9435).getValue().toString().equals(EnumExposureControlSensingRegionShape.RECTANGULAR)))
				if(!contains(0x0018,0x9437))
					throw new ConditionFailedException("value for conditional Element 0x0018,0x9437 required if value of 0018,9435 is RECTANGULAR");
			if((getDataForTag(0x0018, 0x9435).getValue().toString().equals(EnumExposureControlSensingRegionShape.RECTANGULAR)))
				if(!contains(0x0018,0x9438))
					throw new ConditionFailedException("value for conditional Element 0x0018,0x9438 required if value of 0018,9435 is RECTANGULAR");
			if((getDataForTag(0x0018, 0x9435).getValue().toString().equals(EnumExposureControlSensingRegionShape.RECTANGULAR)))
				if(!contains(0x0018,0x9439))
					throw new ConditionFailedException("value for conditional Element 0x0018,0x9439 required if value of 0018,9435 is RECTANGULAR");
			if((getDataForTag(0x0018, 0x9435).getValue().toString().equals(EnumExposureControlSensingRegionShape.CIRCULAR)))
				if(!contains(0x0018,0x9440))
					throw new ConditionFailedException("value for conditional Element 0x0018,0x9440 required if value of 0018,9435 is CIRCULAR");
			if((getDataForTag(0x0018, 0x9435).getValue().toString().equals(EnumExposureControlSensingRegionShape.CIRCULAR)))
				if(!contains(0x0018,0x9441))
					throw new ConditionFailedException("value for conditional Element 0x0018,0x9441 required if value of 0018,9435 is CIRCULAR");
			if((getDataForTag(0x0018, 0x9435).getValue().toString().equals(EnumExposureControlSensingRegionShape.POLYGONAL)))
				if(!contains(0x0018,0x9442))
					throw new ConditionFailedException("value for conditional Element 0x0018,0x9442 required if value of 0018,9435 is POLYGONAL");

			}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(mme.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),mme);
		}
		catch( ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(cfe.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),cfe);
		}
		return true;
	}
	/**
	 * Sets the shape of the Exposure Control Sensing Region.
	 *Enumerated Values:
	 *RECTANGULAR
	 *CIRCULAR
	 *POLYGONAL
	 * @param enumExposureControlSensingRegionShape enum value of Field Of View Shape.
	 */
	public void setExposureControlSensingRegionShape(EnumExposureControlSensingRegionShape enumExposureControlSensingRegionShape)
	{
		/* Accept the ExposureControlSensingRegionShape in String format of type CS.
		 * 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(0x0018,0x9435, enumExposureControlSensingRegionShape.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9435,DicomConstants.DCM_DT_CS, enumExposureControlSensingRegionShape.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Return type of Exposure Control Sensing Region Shape. 
	 * @return String - value of ExposureControlSensingRegionShape.
	 */
	public String getExposureControlSensingRegionShape()
	{
		Object objValue = getValue(0x0018,0x9435);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the location of the left edge of the rectangular ExposureControl Sensing Region expressed as effective pixel column.
	 * @param shExposureControlSensingRegionLeftVerticalEdge short value of Exposure Control SensingRegion Left Vertical Edge.
	 */
	public void setExposureControlSensingRegionLeftVerticalEdge(short shExposureControlSensingRegionLeftVerticalEdge)
	{
		/* Accept the Exposure Control SensingRegion Left Vertical Edge of type SS.
		 * 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(0x0018,0x9436,shExposureControlSensingRegionLeftVerticalEdge,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9436,DicomConstants.DCM_DT_FL, shExposureControlSensingRegionLeftVerticalEdge);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the location of the left edge of the rectangular ExposureControl Sensing Region expressed as effective pixel column.
	 * @return short - short value of Exposure Control SensingRegion Left Vertical Edge.
	 */
	public short getExposureControlSensingRegionLeftVerticalEdge()
	{
		Object objValue = getValue(0x0018,0x9436);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the location of the right edge of the rectangular ExposureControl Sensing Region expressed as effective pixel column.
	 * @param shExposureControlSensingRegionRightVerticalEdge short value of Exposure Control SensingRegion Right Vertical Edge.
	 */
	public void setExposureControlSensingRegionRightVerticalEdge(short shExposureControlSensingRegionRightVerticalEdge)
	{
		/* Accept the Exposure Control SensingRegion Right Vertical Edge of type SS.
		 * 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(0x0018,0x9437,shExposureControlSensingRegionRightVerticalEdge,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9437,DicomConstants.DCM_DT_FL, shExposureControlSensingRegionRightVerticalEdge);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the location of the right edge of the rectangular ExposureControl Sensing Region expressed as effective pixel column.
	 * @return short - short value of Exposure Control SensingRegion Right Vertical Edge.
	 */
	public short getExposureControlSensingRegionRightVerticalEdge()
	{
		Object objValue = getValue(0x0018,0x9437);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the location of the upper edge of the rectangular ExposureControl Sensing Region expressed as effective pixel column.
	 * @param shExposureControlSensingRegionUpperHorizontalEdge short value of Exposure Control SensingRegion Upper Vertical Edge.
	 */
	public void setExposureControlSensingRegionUpperHorizontalEdge(short shExposureControlSensingRegionUpperHorizontalEdge)
	{
		/* Accept the Exposure Control SensingRegion Upper Vertical Edge of type SS.
		 * 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(0x0018,0x9438,shExposureControlSensingRegionUpperHorizontalEdge,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9438,DicomConstants.DCM_DT_FL, shExposureControlSensingRegionUpperHorizontalEdge);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the location of the upper edge of the rectangular ExposureControl Sensing Region expressed as effective pixel column.
	 * @return short - short value of Exposure Control SensingRegion Upper Vertical Edge.
	 */
	public short getExposureControlSensingRegionUpperHorizontalEdge()
	{
		Object objValue = getValue(0x0018,0x9438);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the location of the lower edge of the rectangular ExposureControl Sensing Region expressed as effective pixel column.
	 * @param shExposureControlSensingRegionLowerHorizontalEdge short value of Exposure Control SensingRegion lower Vertical Edge.
	 */
	public void setExposureControlSensingRegionLowerHorizontalEdge(short shExposureControlSensingRegionLowerHorizontalEdge)
	{
		/* Accept the Exposure Control SensingRegion Upper Vertical Edge of type SS.
		 * 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(0x0018,0x9439,shExposureControlSensingRegionLowerHorizontalEdge,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9439,DicomConstants.DCM_DT_FL, shExposureControlSensingRegionLowerHorizontalEdge);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the location of the lower edge of the rectangular ExposureControl Sensing Region expressed as effective pixel column.
	 * @return short - short value of Exposure Control SensingRegion Lower Vertical Edge.
	 */
	public short getExposureControlSensingRegionLowerHorizontalEdge()
	{
		Object objValue = getValue(0x0018,0x9439);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the Location of the center of the circular Exposure ControlSensing Region expressed as effective pixel row and column.
	 * @param sCenterOfCircularExposureControlSensingRegion short array of Center of Circular ExposureControl Sensing Region. 
	 */
	public void setCenterOfCircularExposureControlSensingRegion(short[] sCenterOfCircularExposureControlSensingRegion)
	{
		/* Accept the Center of Circular ExposureControl Sensing Region in short Array format of type US or SS.
		 * 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(0x0018, 0x9440, sCenterOfCircularExposureControlSensingRegion,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x9440,DicomConstants.DCM_DT_US, sCenterOfCircularExposureControlSensingRegion);
		addDataElement(objIDataElement);
	}
	/**
	 * Returns the Location of the center of the circular Exposure ControlSensing Region expressed as effective pixel row and column
	 * @return short[] - short array of Center of Circular ExposureControl Sensing Region.
	 */
	public short[] getCenterOfCircularExposureControlSensingRegion()
	{
		Object objValue = getValue(0x0018, 0x9440);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
	/**
	 * Sets the Radius of the circular Exposure Control Sensing Regionexpressed as effective number of pixels along the row direction.
	 * @param shRadiusOfCircularExposureControlSensingRegion short value of Radius of Circular ExposureControl Sensing Region
	 */
	public void setWaveformBitsStored(short shRadiusOfCircularExposureControlSensingRegion)
	{
		/* Accept the RadiusOfCircularExposureControlSensingRegion 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(0x0018,0x9441,shRadiusOfCircularExposureControlSensingRegion,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0018,0x9441,DicomConstants.DCM_DT_US, shRadiusOfCircularExposureControlSensingRegion);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the Radius of the circular Exposure Control Sensing Regionexpressed as effective number of pixels along the row direction.
	 * @return short - short value of Radius of Circular ExposureControl Sensing Region
	 */
	public short getRadiusOfCircularExposureControlSensingRegion()
	{
		Object objValue = getValue(0x0018,0x9441);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the Vertices of the PolygonalExposure Control Sensing Region
	 * @param sVerticesOfThePolygonalExposureControlSensingRegion short array of Vertices of the PolygonalExposure Control Sensing Region. 
	 */
	public void setVerticesOfThePolygonalExposureControlSensingRegion(short[] sVerticesOfThePolygonalExposureControlSensingRegion)
	{
		/* Accept the Vertices of the PolygonalExposure Control Sensing Region in short Array format of type US or SS.
		 * 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(0x0018, 0x9442, sVerticesOfThePolygonalExposureControlSensingRegion,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x9442,DicomConstants.DCM_DT_US, sVerticesOfThePolygonalExposureControlSensingRegion);
		addDataElement(objIDataElement);
	}
	/**
	 * Returns the Vertices of the PolygonalExposure Control Sensing Region.
	 * @return short[] - short array of Vertices of the PolygonalExposure Control Sensing Region.
	 */
	public short[] getVerticesOfThePolygonalExposureControlSensingRegion()
	{
		Object objValue = getValue(0x0018, 0x9442);
		if(objValue != null)
			return (short[])objValue;
		return null;
	}
}
