/*******************************************************************************
 *  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.EnumFrameofReferenceTransformationType;
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 class contains methods to validate,set and get the attributes in {@link FrameofReferenceRelationshipSequence}.
 */
public class FrameofReferenceRelationshipSequenceItem extends Item  
{
	        

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


	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 * and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	FrameofReferenceRelationshipSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

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

	/**
	 * Validates all the attributes in FrameofReferenceRelationshipSequenceItem that are present in the DataSet
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidItemException throws InvalidItemException If Conditional element fails.
	 */
	public boolean validateForModule()  throws InvalidItemException
	{
		try
		{
			if(!contains(0x3006,0x00C2))
				throw new ConditionFailedException("value of conditional Element 0x3006,0x00C2 required if 0x3006,0x00C0 is present") ;
			if(!contains(0x3006,0x00C4))
				throw new ConditionFailedException("value of conditional Element 0x3006,0x00C4 required if 0x3006,0x00C0 is present") ;
			if(!contains(0x3006,0x00C6))
				throw new ConditionFailedException("value of conditional Element 0x3006,0x00C6 required if 0x3006,0x00C0 is present") ;
		}
		catch( ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}

	/**
	 * Sets Related Frame of Reference UID .Uniquely identifies Related Frame of Reference UID(3006,00C2) in Frame of Reference Relationship Sequence (3006,00C0) in Structure Set Module of RT Structure Set IOD.
	 * @param strRelatedFrameofReferenceUID String value of Related Frame of Reference UID.
	 */
	public void setRelatedFrameofReferenceUID(String strRelatedFrameofReferenceUID)
	{
		/*Accept the RelatedFrameofReferenceUID in String format of type UI.
		 * 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(0x3006,0x00C2, strRelatedFrameofReferenceUID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x00C2,DicomConstants.DCM_DT_UI, strRelatedFrameofReferenceUID);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Related Frame of Reference UID.
	 * @return String - value of Related Frame of Reference UID.
	 */
	public String getRelatedFrameofReferenceUID()
	{
		Object objValue = getValue(0x3006,0x00C2);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Type of Frame of Reference Transformation.
	 * @param enumFrameofReferenceTransformationType Enum value of Type of Frame of Reference Transformation.
	 */
	public void setFrameofReferenceTransformationType(EnumFrameofReferenceTransformationType enumFrameofReferenceTransformationType)
	{
		/*Accept the FrameofReferenceTransformationType 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(0x3006,0x00C4, enumFrameofReferenceTransformationType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x00C4,DicomConstants.DCM_DT_CS, enumFrameofReferenceTransformationType.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Frame of Reference Transformation Type.
	 * @return String - value of Frame of Reference Transformation Type.
	 */
	public String getFrameofReferenceTransformationType()
	{
		Object objValue = getValue(0x3006,0x00C4);
		if(objValue != null)
			return objValue.toString();
		return null;
	}


	/**
	 * Sets Frame of Reference Transformation Matrix
	 * @param strFrameofReferenceTransformationMatrix String array of Frame of Reference Transformation Matrix.
	 */
	public void setFrameofReferenceTransformationMatrix(String[] strFrameofReferenceTransformationMatrix)
	{
		/*Accept the FrameofReferenceTransformationMatrix 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(0x3006,0x00C6, strFrameofReferenceTransformationMatrix,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x00C6,DicomConstants.DCM_DT_DS, strFrameofReferenceTransformationMatrix);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Frame of Reference Transformation Matrix.
	 * @return String - value of Frame of Reference Transformation Matrix.
	 */
	public String[] getFrameofReferenceTransformationMatrix()
	{
		Object objValue = getValue(0x3006,0x00C6);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Sets Frame of Reference Transformation Comment .Uniquely identifies Frame of Reference Transformation Comment(3006,00C8) in Frame of Reference Relationship Sequence (3006,00C0) in Structure Set Module of RT Structure Set IOD.
	 * @param strFrameofReferenceTransformationComment String value of Frame of Reference Transformation Comment.
	 */
	public void setFrameofReferenceTransformationComment(String strFrameofReferenceTransformationComment)
	{
		/*Accept the FrameofReferenceTransformationComment 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(0x3006,0x00C8, strFrameofReferenceTransformationComment,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x3006,0x00C8,DicomConstants.DCM_DT_LO, strFrameofReferenceTransformationComment);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Frame of Reference Transformation Comment.
	 * @return String - value of Frame of Reference Transformation Comment.
	 */
	public String getFrameofReferenceTransformationComment()
	{
		Object objValue = getValue(0x3006,0x00C8);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
}
