/*******************************************************************************
 *  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.EnumRecommendedViewingMode;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSkipFrameRangeFlag;
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.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 Frame Display Sequence .
 */

public class FrameDisplaySequenceItem extends Item  
{

	private static final long serialVersionUID = 1L;


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


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

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

	/**
	 * Validates all the attributes in Frame Display Sequence Item 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(0x0008,0x2142))
				throw new MandatoryMissingException("0008,2142");
			if(!contains(0x0008,0x2143))
				throw new MandatoryMissingException("0008,2143");
			if(!contains(0x0008,0x9460))
				throw new MandatoryMissingException("0008,9460");
			if(!contains(0x0008,0x9459))
				throw new MandatoryMissingException("0008,9459");
			if(contains(0x0028,0x1090) && getDataForTag(0x0028,0x1090).getValue().equals(EnumRecommendedViewingMode.SUBTRACTION_WITH_MASK_IMAGES) )
				if(!contains(0x0028,0x9478))
					throw new ConditionFailedException("0x0028,0x9478");
			
		}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	
	/**
	 * Sets Start Trim.
	 * The Frame Number of the first frame of the set of frames to be displayed in this Item.
	 * @param strStartTrim String value of Start Trim.
	 */
	public void setStartTrim(String strStartTrim )
	{
		/*Accept the Start Trim 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,0x2142, strStartTrim ,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x2142,DicomConstants.DCM_DT_IS, strStartTrim );

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Start Trim.
	 * The Frame Number of the first frame of the set of frames to be displayed in this Item.
	 * @return String - value of Start Trim.
	 */
	public String getStartTrim()
	{
		Object objValue = getValue(0x0008,0x2142);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Stop Trim.
	 * The Frame Number of the last frame of the set of frames to be displayed in this Item.
	 * @param strStopTrim String value of Stop Trim.
	 */
	public void setStopTrim(String strStopTrim )
	{
		/*Accept the Stop Trim 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,0x2143, strStopTrim ,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x2143,DicomConstants.DCM_DT_IS, strStopTrim );

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Stop Trim.
	 * The Frame Number of the last frame of the set of frames to be displayed in this Item.
	 * @return String - value of Stop Trim.
	 */
	public String getStopTrim()
	{
		Object objValue = getValue(0x0008,0x2143);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets Skip Frame Range Flag.
	 * Defined Terms:
	 * 1) DISPLAY.
	 * 2) SKIP.
	 * @param enumSkipFrameRangeFlag enum value of Skip Frame Range Flag.
	 */
	public void setSkipFrameRangeFlag(EnumSkipFrameRangeFlag enumSkipFrameRangeFlag)
	{
		/* Accept the Skip Frame Range Flag 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(0x0008,0x9460, enumSkipFrameRangeFlag.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x9460,DicomConstants.DCM_DT_CS, enumSkipFrameRangeFlag.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return type of Skip Frame Range Flag. 
	 * @return String - value of Skip Frame Range Flag.
	 */
	public String getSkipFrameRangeFlag()
	{
		Object objValue = getValue(0x0008,0x9460);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Recommended Display Frame Rate in Float.
	 * @param fRecommendedDisplayFrameRateinFloat Float value of Recommended Display Frame Rate in Float
	 */
	public void setRecommendedDisplayFrameRateinFloat( float fRecommendedDisplayFrameRateinFloat)
	{
		/*
		 * Accept the Recommended Display Frame Rate in Float format of type FL.
		 * 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,0x9459, fRecommendedDisplayFrameRateinFloat,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x9459,DicomConstants.DCM_DT_FL, fRecommendedDisplayFrameRateinFloat);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Recommended Display Frame Rate in Float.
	 * @return float - Float value of the Recommended Display Frame Rate in Float
	 */
	public float getRecommendedDisplayFrameRateinFloat()
	{
		Object objValue = getValue(0x0008,0x9459);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Recommended Viewing Mode.
	 * Specifies the recommended viewing protocol(s).
	 * Defined Terms:
	 * 1) SUB.
	 * 2) NAT.
	 * @param enumRecommendedViewingMode enum value of Setup Device Type.
	 */
	public void setRecommendedViewingMode(EnumRecommendedViewingMode enumRecommendedViewingMode)
	{
		/* Accept the Recommended Viewing Mode 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(0x0028,0x1090, enumRecommendedViewingMode.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x1090,DicomConstants.DCM_DT_CS, enumRecommendedViewingMode.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return type of Recommended Viewing Mode. 
	 * Specifies the recommended viewing protocol(s).
	 * @return String - value of Recommended Viewing Mode.
	 */
	public String getRecommendedViewingMode()
	{
		Object objValue = getValue(0x0028,0x1090);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the Display Filter Percentage.
	 * @param fDisplayFilterPercentage Float value of the Display Filter Percentage
	 */
	public void setDisplayFilterPercentage( float fDisplayFilterPercentage)
	{
		/*
		 * Accept the Display Filter Percentage in String format of type FL.
		 * 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,0x9411, fDisplayFilterPercentage,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x9411,DicomConstants.DCM_DT_FL, fDisplayFilterPercentage);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Display Filter Percentage.
	 * @return float - Float value of the Display Filter Percentage
	 */
	public float getDisplayFilterPercentage()
	{
		Object objValue = getValue(0x0028,0x9411);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Mask Visibility Percentage.
	 * @param fMaskVisibilityPercentage Float value of the Mask Visibility Percentage
	 */
	public void setMaskVisibilityPercentage( float fMaskVisibilityPercentage)
	{
		/*
		 * Accept the Mask Visibility Percentage in String format of type FL.
		 * 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,0x9478, fMaskVisibilityPercentage,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0028,0x9478,DicomConstants.DCM_DT_FL, fMaskVisibilityPercentage);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Mask Visibility Percentage.
	 * @return float - Float value of the Mask Visibility Percentage
	 */
	public float getMaskVisibilityPercentage()
	{
		Object objValue = getValue(0x0028,0x9478);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
}
