/*******************************************************************************
 *  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.EnumFillMode;
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;

import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
/**
* This item class contains the methods to set, get and validate the attribute present in Fill Style Sequence .
 */
public class FillStyleSequenceItem extends Item   
{


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


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

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



	/**
	 * Validates all the attributes in FillStyleSequenceItem 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(0x0070,0x0257) && getDataForTag(0x0070, 0x0257).getValue().equals(EnumFillMode.STIPPELED))
				if(!contains(0x0070,0x0256))
					throw new ConditionFailedException("0x0070,0x0256") ;
			
			if(!contains(0x0070,0x0251))
				throw new MandatoryMissingException("0070,0251") ;
			if(!contains(0x0070,0x0284))
				throw new MandatoryMissingException("0070,0284") ;
			if(!contains(0x0070,0x0285))
				throw new MandatoryMissingException("0070,0285") ;
			if(!contains(0x0070,0x0257))
				throw new MandatoryMissingException("0070,0257") ;
		}
		catch( MandatoryMissingException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),ce); 
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}
	
	/**
	 * Sets Pattern On Color CIELab Value.
	 * A color triplet value used to encode the foreground.
	 * @param shPatternOnColorCIELabValue short value of Pattern On Color CIELab
	 */
	public void setPatternOnColorCIELabValue(short shPatternOnColorCIELabValue)
	{
		/* Accept the Pattern On Color CIELab Value 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(0x0070,0x0251,shPatternOnColorCIELabValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0251,DicomConstants.DCM_DT_US, shPatternOnColorCIELabValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Pattern On Color CIELab Value.
	 * A color triplet value used to encode the foreground.
	 * @return short - short value of Pattern On Color CIELab
	 */
	public short getPatternOnColorCIELabValue()
	{
		Object objValue = getValue(0x0070,0x0251);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Pattern Off Color CIELab Value.
	 * A color triplet value used to encode the color of parts of the line that are off, i.e., the background.
	 * @param shPatternOffColorCIELabValue short value of Pattern Off Color CIELab
	 */
	public void setPatternOffColorCIELabValue(short shPatternOffColorCIELabValue)
	{
		/* Accept the Pattern Off Color CIELab Value 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(0x0070,0x0252,shPatternOffColorCIELabValue,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0252,DicomConstants.DCM_DT_US, shPatternOffColorCIELabValue);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Pattern Off Color CIELab Value.
	 * A color triplet value used to encode the color of parts of the line that are off, i.e., the background.
	 * @return short - short value of Pattern Off Color CIELab
	 */
	public short getPatternOffColorCIELabValue()
	{
		Object objValue = getValue(0x0070,0x0252);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Pattern On Opacity.
	 * Encodes the foreground opacity.
	 * @param fPatternOnOpacity Float value of the Pattern On Opacity
	 */
	public void setPatternOnOpacity( float fPatternOnOpacity)
	{
		/*
		 * Accept the Pattern On Opacity 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(0x0070,0x0284, fPatternOnOpacity,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0284,DicomConstants.DCM_DT_FL, fPatternOnOpacity);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Pattern On Opacity.
	 * Encodes the foreground opacity.
	 * @return float - Float value of the Pattern On Opacity
	 */
	public float getPatternOnOpacity()
	{
		Object objValue = getValue(0x0070,0x0284);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets the Pattern Off Opacity.
	 * Encodes the background opacity.
	 * @param fPatternOffOpacity Float value of the Pattern Off Opacity
	 */
	public void setPatternOffOpacity( float fPatternOffOpacity)
	{
		/*
		 * Accept the Pattern Off Opacity 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(0x0070,0x0285, fPatternOffOpacity,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0285,DicomConstants.DCM_DT_FL, fPatternOffOpacity);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the Pattern Off Opacity.
	 * Encodes the background opacity.
	 * @return float - Float value of the Pattern Off Opacity
	 */
	public float getPatternOffOpacity()
	{
		Object objValue = getValue(0x0070,0x0285);
		if(objValue != null)
			return Float.parseFloat(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	
	/**
	 * Sets Fill Mode.
	 * The texture of the closed object to be displayed.
	 * Defined Terms:
	 * 1) SOLID.
	 * 2) STIPPELED.
	 * @param enumFillMode enum value of Fill Mode.
	 */
	public void setFillMode(EnumFillMode enumFillMode)
	{
		/* Accept the Fill 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(0x0070,0x0257, enumFillMode.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070,0x0257,DicomConstants.DCM_DT_CS, enumFillMode.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Return Fill Mode.
	 * The texture of the closed object to be displayed. 
	 * @return String - value of Fill Mode.
	 */
	public String getFillMode()
	{
		Object objValue = getValue(0x0070,0x0257);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Sets the Fill Pattern.
	 * @param vectFillPattern collection of byte array of Fill Pattern.
	 */
	public void setFillPattern(ISingleCollection<byte[]> vectFillPattern)
	{
		/*Accept the Signature in vector of byte array format of type OB.
		 * 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(0x0070, 0x0256, vectFillPattern,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0070, 0x0256,DicomConstants.DCM_DT_OB, vectFillPattern);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the Fill Pattern
	 * @return ISingleCollection<byte[]> - collection of byte array of Fill Pattern.
	 */
	@SuppressWarnings("unchecked")
	public ISingleCollection<byte[]> getFillPattern()
	{
		Object objValue = getValue(0x0070, 0x0256);
		if(objValue != null)
			return (ISingleCollection<byte[]>)objValue;
		return null;
	}
}
