/*******************************************************************************
 *  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.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.enums.EnumBitsStored;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLossyImageCompression;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidMacroException;
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 class contains macro which contains IOD attributes that describe the image related characteristics of the contributing image sources used to create a new SOP Instance (e.g., a volume SOP Instance).
 * @author saloni
 *
 */
public class ContributingImageSourcesMacro extends Macro
{
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public ContributingImageSourcesMacro(IDataSet objDataSet)
	{
		super(objDataSet);
	}
	
	/**
	 * This constructor constructs a Macro with specified DataSet and DictionaryReader
	 * @param objDataSet specifies the DataSet
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public ContributingImageSourcesMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}
	
	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public ContributingImageSourcesMacro(IItem objItem)
	{
		super(objItem);
	}
	
	/**
	 * This constructor constructs a Macro with the specified Item and DictionaryReader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public ContributingImageSourcesMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}
	
	/**
	 * Validates all the attributes in ContributingImageSourcesMacro
	 * @return boolean - Success -true, Failure -false.
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public boolean validate() throws InvalidMacroException
	{
		if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
			return validate(objIDataSet);
		else if(iMacroLevel == DicomConstants.DCM_ITEM_LEVEL_MACRO)
			return validate(objIItem);
		return false;
	}
	
	/**
	 * Validates all the attributes in ContributingImageSourcesMacro that are present in the DataSet
	 * @param objIDataSet collection of DataElements
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IDataSet objIDataSet) throws InvalidMacroException
	{
		try
		{ 
			if(!objIDataSet.isPresent(0x0028,0x0010))
				throw new MandatoryMissingException("0028,0010");
			if(!objIDataSet.isPresent(0x0028,0x0011))
				throw new MandatoryMissingException("0028,0011");
			if(!objIDataSet.isPresent(0x0028,0x0101))
				throw new MandatoryMissingException("0028,0101");
			if(objIDataSet.getDataForTag(0x0028,0x2110).getValue().toString().equals(EnumLossyImageCompression.LOSSY_COMPRESSED))
				if(!objIDataSet.isPresent(0x0028,0x2112))
					throw new ConditionFailedException ("0028,2112 is Required if Lossy Image Compression (0028,2110) is 01.");
			if(objIDataSet.getDataForTag(0x0028,0x2110).getValue().toString().equals(EnumLossyImageCompression.LOSSY_COMPRESSED))
				if(!objIDataSet.isPresent(0x0028,0x2114))
					throw new ConditionFailedException ("0028,2114 is Required if Lossy Image Compression (0028,2110) is 01.");
						
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(CodeSequenceMacro.class.getSimpleName() ,me);
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(CodeSequenceMacro.class.getSimpleName(),ce); 
		}
		return true;
	}
	
	/**
	 * Validates all the attributes in ContributingImageSourcesMacro that are present in the DataSet
	 * @param objIItem collection of DataElements in a sequence
	 * @return boolean - Success -true, Failure - false
	 * @throws InvalidMacroException throws InvalidMacroException If Mandatory or Conditional element fails.
	 */
	public static boolean validate(IItem objIItem) throws InvalidMacroException
	{
		try
		{  
			if(!objIItem.contains(0x0024,0x0306))
				if(!objIItem.contains(0x0028,0x0010))
					throw new MandatoryMissingException("0028,0010");
				if(!objIItem.contains(0x0028,0x0011))
					throw new MandatoryMissingException("0028,0011");
				if(!objIItem.contains(0x0028,0x0101))
					throw new MandatoryMissingException("0028,0101");
				if(objIItem.getDataForTag(0x0028,0x2110).getValue().toString().equals(EnumLossyImageCompression.LOSSY_COMPRESSED))
					if(!!objIItem.contains(0x0028,0x2112))
						throw new ConditionFailedException ("0028,2112 is Required if Lossy Image Compression (0028,2110) is 01.");
				if(objIItem.getDataForTag(0x0028,0x2110).getValue().toString().equals(EnumLossyImageCompression.LOSSY_COMPRESSED))
					if(!!objIItem.contains(0x0028,0x2114))
						throw new ConditionFailedException ("0028,2114 is Required if Lossy Image Compression (0028,2110) is 01.");
	
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(CodeSequenceMacro.class.getSimpleName(),me);
		}
		return true;
	}
	/**
	 * Sets Number of rows in the images.
	 * @param shRows short value of Number of rows in the images.
	 */
	public void setRows(short shRows)
	{
		/* Accept the Rows 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(0x0028,0x0010,shRows, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x0010,DicomConstants.DCM_DT_US, shRows);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Number of rows in the images.
	 * @return short - short value of Number of rows in the images.
	 */
	public short getRows()
	{
		Object objValue = getValue(0x0028,0x0010);
		if(objValue != null)
			return Short.parseShort(objValue.toString());

		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Number of Columns in the images.
	 * @param shColumns short value of Number of Columns in the images.
	 */
	public void setColumns(short shColumns)
	{
		/* Accept the Columns 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(0x0028,0x0011,shColumns, objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x0011,DicomConstants.DCM_DT_US, shColumns);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Number of Columns in the images.
	 * @return short - short value of Number of Columns in the images.
	 */
	public short getColumns()
	{
		Object objValue = getValue(0x0028,0x0011);
		if(objValue != null)
			return Short.parseShort(objValue.toString());

		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets Number of bits stored for each pixel sample. Each sample shall have the same
	 * number of bits stored.
	 * @param enumBitsStored specifies the number of bits stored
	 */
	public void setBitsStored(EnumBitsStored enumBitsStored)
	{
		/*Accept bits stored for each pixel in this image in short format 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(0x0028, 0x0101,enumBitsStored.getValue(),objIDictionaryReader);
		}
		else
		{
			objIDataElement = new ExplicitDE(0x0028, 0x0101, DicomConstants.DCM_DT_US,enumBitsStored.getValue());
		}
		if(objIDataElement != null)
			addDataElement(objIDataElement);
	}

	/**
	 * Retrieves the number of bits stored
	 * @return short - returns the number of bits stored
	 */
	public short getBitsStored()
	{
		Object objValue = getValue(0x0028, 0x0101);
		if(objValue != null)
			return Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * Sets whether the Source Images have undergone lossy compression(at a point in their lifetime).
	 * @param enumLossyImageCompression enum value of Lossy Image Compression
	 */
	public void setLossyImageCompression(EnumLossyImageCompression enumLossyImageCompression)
	{
		/* Accept the Lossy Image Compression 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,0x2110, enumLossyImageCompression.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x2110,DicomConstants.DCM_DT_CS, enumLossyImageCompression.getValue());

		addDataElement(objIDataElement);
	}
	/**
	 * Return whether the Source Images have undergone lossy compression(at a point in their lifetime).
	 * @return String - value of Lossy Image Compression
	 */
	public String getLossyImageCompression()
	{
		Object objValue = getValue(0x0028,0x2110);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Lossy Image Compression Ratio
	 * @param strLossyImageCompressionRatio String value of Lossy Image Compression Ratio
	 */
	public void setLossyImageCompressionRatio(String[] strLossyImageCompressionRatio)
	{
		/*Accept the Lossy Image Compression Ratio 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(0x0028,0x2112, strLossyImageCompressionRatio,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x2112,DicomConstants.DCM_DT_DS, strLossyImageCompressionRatio);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Lossy Image Compression Ratio
	 * @return String - value of Lossy Image Compression Ratio
	 */
	public String[] getLossyImageCompressionRatio()
	{
		Object objValue = getValue(0x0028,0x2112);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	/**
	 * Sets label for the lossy compression method(s) that have been applied tothe source images.
	 * @param strLossyImageCompressionMethod String Value component of Lossy Image CompressionMethod
	 */
	public void setLossyImageCompressionMethod(String strLossyImageCompressionMethod)
	{
		/*Accept the LossyImageCompressionMethod 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,0x2114, strLossyImageCompressionMethod,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x2114,DicomConstants.DCM_DT_CS, strLossyImageCompressionMethod);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves label for the lossy compression method(s) that have been applied tothe source images.
	 * @return String Array -  Value component of a Lossy Image CompressionMethod
	 */
	public String[] getLossyImageCompressionMethod()
	{
		if(getDataElement(0x0028,0x2114)!=null)
		{
			if(getDataElement(0x0028,0x2114).getValue()!=null)
			{
				Object objValue = getDataElement(0x0028,0x2114).getValue();
				if(objValue!=null)
				{
					if(objValue instanceof String)
					{
						String strTemp = (String)objValue;
						String[] strArr = new String[1];
						strArr[0] = strTemp;
						return strArr;
					}
					else
						return (String[])objValue;
				}
			}
		}
		return null;
	}

}
