/*******************************************************************************
 *  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.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.enums.EnumVOILUTFunction;
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 specifies the default set of attributes encapsulated in the VOI LUT Macro.
 *
 */

public class VOILUTMacro extends Macro
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * This constructor constructs a Macro with specified DataSet.
	 * @param objDataSet Collection of DataElements 
	 */
	public VOILUTMacro(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 VOILUTMacro(IDataSet objDataSet,IDictionaryReader objDictionaryReader)
	{
		super(objDataSet,objDictionaryReader);
	}

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public VOILUTMacro(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 VOILUTMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}

	/**
	 * Retrieves pre-initialized object of VOI LUT Sequence If the module contains the sequence object
	 * it returns the same otherwise it creates the sequence adds to the module with proper initialization
	 * and then returns the object of sequence.
	 * @return VOILUTSequence - instance of VOILUTSequence. 
	 */
	public VOILUTSequence getVOILUTSequence()
	{
		VOILUTSequence objVOILUTSequence = null;
		IDataElement objIDataElement =  getDataElement(0x0028, 0x3010); //getDataElement
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objVOILUTSequence = (VOILUTSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objVOILUTSequence = (VOILUTSequence)objDicomV3Factory.createSequence(0x3002, 0x0050, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0028, 0x3010, objVOILUTSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0028, 0x3010, DicomConstants.DCM_DT_SQ, objVOILUTSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objVOILUTSequence;
	}

	/**
	 * Sets Window Center.
	 * @param strWindowCenter String value of Window Center.
	 */
	public void setWindowCenter(String[] strWindowCenter)
	{
		/*Accept the WindowCenter 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,0x1050, strWindowCenter,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x1050,DicomConstants.DCM_DT_DS, strWindowCenter);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Window Center.
	 * @return String - value of Window Center.
	 */
	public String[] getWindowCenter()
	{
		Object objValue = getValue(0x0028,0x1050);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	/**
	 * Sets Window Width.
	 * @param strWindowWidth String value of Window Width.
	 */
	public void setWindowWidth(String[] strWindowWidth)
	{
		/*Accept the WindowWidth 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,0x1051, strWindowWidth,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x1051,DicomConstants.DCM_DT_DS, strWindowWidth);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves Window Width.
	 * @return String - value of Window Width.
	 */
	public String[] getWindowWidth()
	{
		Object objValue = getValue(0x0028,0x1051);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Sets Window Center & WidthExplanation.
	 * @param strArrWindowCenterWidthExplanation String value of Window Center & WidthExplanation.
	 */
	public void setWindowCenterWidthExplanation(String[] strArrWindowCenterWidthExplanation)
	{
		/* Accept the Window Center & WidthExplanation 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(0x0028,0x1055, strArrWindowCenterWidthExplanation,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x1055,DicomConstants.DCM_DT_LO, strArrWindowCenterWidthExplanation);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for Window Center & WidthExplanation.
	 * @return String - value of Window Center & WidthExplanation.
	 */
	public String[] getWindowCenterWidthExplanation()
	{
		Object objValue = getValue(0x0028,0x1055);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}
	/**
	 * Describes a VOI LUT function to apply to the values of Window Center(0028,1050) and Window Width (0028,1051).
	 *See Section C.11.2.1.3 for further explanation.
	 *Defined Terms:
	 *LINEAR
	 *SIGMOID
	 * @param enumVOILUTFunction enum value of VOILUT Function.
	 */
	public void setVOILUTFunction(EnumVOILUTFunction enumVOILUTFunction)
	{
		/* Accept the VOILUT Function 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,0x1056, enumVOILUTFunction.getValue(),objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0028,0x1056,DicomConstants.DCM_DT_CS, enumVOILUTFunction.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return type of VOILUT Function. 
	 * @return String - value of VOILUT Function.
	 */
	public String getVOILUTFunction()
	{
		Object objValue = getValue(0x0028,0x1056);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in VOI LUT Macro
	 * @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 VOI LUT Macro 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,0x1050))
				if(!objIDataSet.isPresent(0x0028,0x3010))
					throw new ConditionFailedException("value for conditional Element 0x0028,0x3010 required if value of 0x0028,0x1050 is absent");
			if(!objIDataSet.isPresent(0x0028,0x3010))
				if(!objIDataSet.isPresent(0x0028,0x1050))
					throw new ConditionFailedException("value for conditional Element 0x0028,0x1050 required if value of 0x0028,0x3010 is absent");
			if(objIDataSet.isPresent(0x0028,0x1050))
				if(!objIDataSet.isPresent(0x0028,0x1051))
					throw new ConditionFailedException("value for conditional Element 0x0028,0x1051 required if value of 0x0028,0x1050 is sent");
			
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(VOILUTMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(VOILUTMacro.class.getSimpleName(),me);
		}
		return true;
	}

	/**
	 * Validates all the attributes in VOI LUT Macro 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(0x0028,0x1050))
				if(!objIItem.contains(0x0028,0x3010))
					throw new ConditionFailedException("value for conditional Element 0x0028,0x3010 required if value of 0x0028,0x1050 is absent");
			if(!objIItem.contains(0x0028,0x3010))
				if(!objIItem.contains(0x0028,0x1050))
					throw new ConditionFailedException("value for conditional Element 0x0028,0x1050 required if value of 0x0028,0x3010 is absent");
			if(objIItem.contains(0x0028,0x1050))
				if(!objIItem.contains(0x0028,0x1051))
					throw new ConditionFailedException("value for conditional Element 0x0028,0x1051 required if value of 0x0028,0x1050 is sent");
				
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(VOILUTMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidMacroException(TemplateIdentificationMacro.class.getSimpleName(),me);
		}
		return true;
	}
}
