/*******************************************************************************
 *  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.EnumWedgeType;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
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 Introduces sequence of treatment wedges present during delivered Beam.
 * The sequence may contain one or more items.
 */
public class RecordedWedgeSequenceItem extends Item  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	RecordedWedgeSequenceItem()
	{
		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 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	RecordedWedgeSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Sets the Identification number of the Wedge. The value of Wedge Number (300A,00D2) shall be unique within the wedge sequence.
	 * @param strWedgeNumber String value of Wedge Number.
	 */
	public void setWedgeNumber(String strWedgeNumber)
	{
		/*Accept the WedgeNumber 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(0x300A,0x00D2, strWedgeNumber,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00D2,DicomConstants.DCM_DT_IS, strWedgeNumber);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Identification number of the Wedge
	 * @return String - value of Wedge Number.
	 */
	public String getWedgeNumber()
	{
		Object objValue = getValue(0x300A,0x00D2);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets type of wedge defined for delivered Beam
	 * @param enumWedgeType Enum value of Wedge Type.
	 */
	public void setWedgeType(EnumWedgeType enumWedgeType)
	{
		/*Accept the WedgeType 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(0x300A,0x00D3, enumWedgeType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00D3,DicomConstants.DCM_DT_CS, enumWedgeType.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Wedge Type.
	 * @return String - value of Wedge Type.
	 */
	public String getWedgeType()
	{
		Object objValue = getValue(0x300A,0x00D3);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets user-supplied identifier for wedge.
	 * @param strWedgeID String value of Wedge ID.
	 */
	public void setWedgeID(String strWedgeID )
	{
		/*Accept the WedgeID 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(0x300A,0x00D4, strWedgeID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00D4,DicomConstants.DCM_DT_SH, strWedgeID);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Wedge ID.
	 * @return String - value of Wedge ID.
	 */
	public String getWedgeID()
	{
		Object objValue = getValue(0x300A,0x00D4);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets an identifier for the accessory intended to be read by a device such as a bar-code reader.
	 * @param strAccessoryCode String value of Accessory Code.
	 */
	public void setAccessoryCode(String strAccessoryCode )
	{
		/*Accept the AccessoryCode 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(0x300A,0x00F9, strAccessoryCode,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00F9,DicomConstants.DCM_DT_LO, strAccessoryCode);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Accessory Code.
	 * @return String - value of Accessory Code.
	 */
	public String getAccessoryCode()
	{
		Object objValue = getValue(0x300A,0x00F9);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Nominal wedge angle delivered.
	 * @param strWedgeAngle String value of Wedge Angle.
	 */
	public void setWedgeAngle(String strWedgeAngle)
	{
		/*Accept the WedgeAngle 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(0x300A,0x00D5, strWedgeAngle,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00D5,DicomConstants.DCM_DT_IS, strWedgeAngle);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Nominal wedge angle delivered.
	 * @return String - value of Wedge Angle.
	 */
	public String getWedgeAngle()
	{
		Object objValue = getValue(0x300A,0x00D5);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Orientation of wedge, i.e. orientation of IEC WEDGE FILTER coordinate system with respect to IEC BEAM LIMITING DEVICE coordinate system (degrees).
	 * @param strWedgeOrientation String value of Wedge Orientation.
	 */
	public void setWedgeOrientation(String strWedgeOrientation)
	{
		/*Accept the WedgeOrientation 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(0x300A,0x00D8, strWedgeOrientation,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x300A,0x00D8,DicomConstants.DCM_DT_DS, strWedgeOrientation);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Orientation of wedge.
	 * @return String - value of Wedge Orientation.
	 */
	public String getWedgeOrientation()
	{
		Object objValue = getValue(0x300A,0x00D8);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Validates all the attributes in RecordedWedgeSequenceItem that are present in the DataSet
	 * @return int - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			if(!contains(0x300A,0x00D3))
				throw new ConditionFailedException("value of conditional Element 300A,00D3 required if 3008,00B0 is not present") ;
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}

}
