/*******************************************************************************
 *  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.EnumCollimatorShape;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
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 items of X Ray Collimator Dimensions Macro.
 *
 */

public class XRayCollimatorDimensionsMacro extends Macro
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	XRayCollimatorDimensionsMacro objXRayCollimatorDimensionsMacro = null;

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

	/**
	 * This constructor constructs a Macro with the specified Item
	 * @param objItem specifies instance of Item class
	 */
	public XRayCollimatorDimensionsMacro(IItem objItem)
	{
		super(objItem);
	}

	/**
	 * This constructor constructs a Macro with the specified Item and Dictionary Reader
	 * @param objItem specifies instance of Item class
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	public XRayCollimatorDimensionsMacro(IItem objItem,IDictionaryReader objDictionaryReader)
	{
		super(objItem, objDictionaryReader);
	}
	
	/**
	 * This method returns the XRayCollimatorDimensionsMacro with proper initialization.
	 * @return - instance of {@link XRayCollimatorDimensionsMacro} 
	 */
	public XRayCollimatorDimensionsMacro getXRayCollimatorDimensionsMacro()
	{
		if(objXRayCollimatorDimensionsMacro == null)
		{
			if(iMacroLevel == DicomConstants.DCM_MODULE_LEVEL_MACRO)
				objXRayCollimatorDimensionsMacro = new XRayCollimatorDimensionsMacro(objIDataSet);
			else 
				objXRayCollimatorDimensionsMacro = new XRayCollimatorDimensionsMacro(objIItem);

			return objXRayCollimatorDimensionsMacro;
		}
		return objXRayCollimatorDimensionsMacro;
	}
	/**
	 * Sets Location of the left edge of the rectangular collimator with respect to pixels in the image given as
	 * column.Required if Collimator Shape (0018,1700) is RECTANGULAR.
	 * 
	 * @param strCollimatorLeftVerticalEdge String value of Collimator Left Vertical Edge
	 */
	public void setCollimatorLeftVerticalEdge(String strCollimatorLeftVerticalEdge)
	{
		/* Accept Collimator Left Vertical Edge 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x1702, strCollimatorLeftVerticalEdge ,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1702,DicomConstants.DCM_DT_IS, strCollimatorLeftVerticalEdge );
		
		addDataElement(objIDataElement);
	}

	/**
	 * Return Location of the left edge of the rectangular collimator with respect to pixels in the image given as
	 * column.
	 * 
	 * @return String - String value of Collimator Left Vertical Edge
	 */
	public String getCollimatorLeftVerticalEdge()
	{
		Object objValue = getValue(0x0018,0x1702);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Location of the right edge of the rectangular collimator with respect to pixels in the image given as column.
	 * Required if Collimator Shape (0018,1700) is RECTANGULAR.
	 * 
	 * @param strCollimatorRightVerticalEdge String value of Collimator Right Vertical Edge
	 */
	public void setCollimatorRightVerticalEdge(String strCollimatorRightVerticalEdge)
	{
		/*Accept Collimator Right Vertical Edge 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x1704, strCollimatorRightVerticalEdge ,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1704,DicomConstants.DCM_DT_IS, strCollimatorRightVerticalEdge );
		addDataElement(objIDataElement);
	}

	/**
	 * Return Location of the Right edge of the rectangular Collimator with respect to pixels in the image given as column.
	 * 
	 * @return String - String value of Collimator Right Vertical Edge.
	 */
	public String getCollimatorRightVerticalEdge()
	{
		Object objValue = getValue(0x0018,0x1704);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets location of the upper edge of the rectangular Collimator with respect to pixels in the image given as row.
	 * Required if Collimator Shape (0018,1600) is RECTANGULAR.
	 * 
	 * @param strCollimatorUpperHorizontalEdge  String value of Collimator Upper Horizontal Edge
	 */
	public void setCollimatorUpperHorizontalEdge(String strCollimatorUpperHorizontalEdge)
	{
		/*Accept Collimator Upper Horizontal Edge 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x1706, strCollimatorUpperHorizontalEdge ,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1706,DicomConstants.DCM_DT_IS, strCollimatorUpperHorizontalEdge );
		
		addDataElement(objIDataElement);
	}

	/**
	 * Return location of the upper edge of the rectangular Collimator with respect to pixels in the image given as row.
	 * 
	 * @return String - String value of Collimator Upper Horizontal Edge.
	 */
	public String getCollimatorUpperHorizontalEdge()
	{
		Object objValue = getValue(0x0018,0x1706);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets location of the Lower edge of the rectangular Collimator with respect to pixels in the image given as row.
	 * Required if Collimator Shape (0018,1600) is RECTANGULAR.
	 * 
	 * @param strCollimatorLowerHorizontalEdge the str collimator lower horizontal edge
	 */
	public void setCollimatorLowerHorizontalEdge(String strCollimatorLowerHorizontalEdge)
	{
		/*Accept Collimator Lower Horizontal Edge 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x1708, strCollimatorLowerHorizontalEdge ,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1708,DicomConstants.DCM_DT_IS, strCollimatorLowerHorizontalEdge );
		
		addDataElement(objIDataElement);
	}

	/**
	 * Return location of the lower edge of the rectangular Collimator with respect to pixels in the image given as row.
	 * 
	 * @return String - String value of Collimator Lower Horizontal Edge
	 */
	public String getCollimatorLowerHorizontalEdge()
	{
		Object objValue = getValue(0x0018,0x1708);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Location of the center of the circular Collimator with respect to pixels in the image given as row and column.
	 * Required if Collimator Shape (0018,1600) is CIRCULAR.
	 * @param arrStrCenterOfCircularCollimator the arr str center of circular collimator
	 */
	public void setCenterOfCircularCollimator(String arrStrCenterOfCircularCollimator)
	{
		/*Accept Center of Circular Collimator 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018, 0x1710, arrStrCenterOfCircularCollimator,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018, 0x1710,DicomConstants.DCM_DT_IS, arrStrCenterOfCircularCollimator);
		
		addDataElement(objIDataElement);
	}

	/**
	 * Return Location of the center of the circular Collimator with respect to pixels in the image given as row and column.
	 * 
	 * @return String[] - String array of Center Of Circular Collimator
	 */
	public String[] getCenterOfCircularCollimator()
	{
		if(getDataElement(0x0018, 0x1710)!=null)
		{
			if(getDataElement(0x0018, 0x1710).getValue()!=null)
			{
				Object objValue = getDataElement(0x0018, 0x1710).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;
	}

	/**
	 * Required if Collimator Shape (0018,1600) is CIRCULAR. Radius of the circular Collimator
	 * with respect to pixels in the image given as a number of pixels along the row direction.
	 * 
	 * @param strRadiusOfCircularCollimator the str radius of circular collimator
	 */
	public void setRadiusOfCircularCollimator(String strRadiusOfCircularCollimator)
	{
		/* Accept Radius of Circular Collimator 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 Hash Map
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
			objIDataElement = new ImplicitDE(0x0018,0x1712, strRadiusOfCircularCollimator ,objIDictionaryReader);
		else
			objIDataElement = new ExplicitDE(0x0018,0x1712,DicomConstants.DCM_DT_IS, strRadiusOfCircularCollimator );
		
		addDataElement(objIDataElement);
	}

	/**
	 * Return Location of the center of the circular Collimator with respect to pixels in the image given as row and column.
	 * 
	 * @return String - String value of Radius Of Circular Collimator.
	 */
	public String getRadiusOfCircularCollimator()
	{
		Object objValue = getValue(0x0018,0x1712);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets the row and column coordinates of theother vertices of the polygon Collimator.
	 * 
	 * @param arrStrVerticesOfthePolygonalCollimator the arr str vertices ofthe polygonal collimator
	 */
	public void setVerticesOfPolygonalCollimator(String arrStrVerticesOfthePolygonalCollimator)
	{
		/*Accept Vertices of the Polygonal Collimator 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 Hash Map
		 */		IDataElement objIDataElement = null;
			if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0018, 0x1720, arrStrVerticesOfthePolygonalCollimator,objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0018, 0x1720,DicomConstants.DCM_DT_IS, arrStrVerticesOfthePolygonalCollimator);
		
		addDataElement(objIDataElement);
	}

	/**
	 * Return Location of the center of the circular Collimator with respect to pixels in the image given as row and column.
	 * 
	 * @return String[] - String array of Vertices Of Polygonal Collimator.
	 */
	public String[] getVerticesOfPolygonalCollimator()
	{
		if(getDataElement(0x0018, 0x1720)!=null)
		{
			if(getDataElement(0x0018, 0x1720).getValue()!=null)
			{
				Object objValue = getDataElement(0x0018, 0x1720).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;
	}


	/**
	 * Validates all the attributes in XRayCollimatorDimensionsMacro
	 * @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 XRayCollimatorDimensionsMacro 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.getDataForTag(0x0018, 0x1700).getValue().equals(EnumCollimatorShape.RECTANGULAR))
				{
					if(!objIDataSet.isPresent(0x0018,0x1702))
						throw new ConditionFailedException("0018,1702 is Required if Collimator Shape (0018,1700) is RECTANGULAR.");
					if(!objIDataSet.isPresent(0x0018,0x1704))
						throw new ConditionFailedException("0018,1704 is Required if Collimator Shape (0018,1700) is RECTANGULAR.");
					if(!objIDataSet.isPresent(0x0018,0x1706))
						throw new ConditionFailedException("0018,1706 is Required if Collimator Shape (0018,1700) is RECTANGULAR.");
					if(!objIDataSet.isPresent(0x0018,0x1708))
						throw new ConditionFailedException("0018,1708 is Required if Collimator Shape (0018,1700) is RECTANGULAR.");
				}
				if(objIDataSet.getDataForTag(0x0018, 0x1700).getValue().equals(EnumCollimatorShape.CIRCULAR))
				{
					if(!objIDataSet.isPresent(0x0018,0x1710))
						throw new ConditionFailedException("0018,1710 is Required if Collimator Shape (0018,1700) is CIRCULAR.");
					if(!objIDataSet.isPresent(0x0018,0x1712))
						throw new ConditionFailedException("0018,1712 is Required if Collimator Shape (0018,1700) is CIRCULAR.");
				}
				if(objIDataSet.getDataForTag(0x0018, 0x1700).getValue().equals(EnumCollimatorShape.POLYGONAL))
				{
					if(!objIDataSet.isPresent(0x0018,0x1720))
						throw new ConditionFailedException("0018,1720 is Required if Collimator Shape (0018,1700) is POLYGONAL.");
				}
		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(ImageSOPInstanceReferenceMacro.class.getSimpleName(),ce); 
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(ImageSOPInstanceReferenceMacro.class.getSimpleName(),me);
		}catch( InvalidMacroException ime)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ime.getMessage());
			}
			throw new InvalidItemException(ImageSOPInstanceReferenceMacro.class.getSimpleName(),ime);
		}


		return true;
	}

	/**
	 * Validates all the attributes in XRayCollimatorDimensionsMacro 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.getDataForTag(0x0018, 0x1700).getValue().equals(EnumCollimatorShape.RECTANGULAR))
			{
				if(!objIItem.contains(0x0018,0x1702))
					throw new ConditionFailedException("0018,1702 is Required if Collimator Shape (0018,1700) is RECTANGULAR.");
				if(!objIItem.contains(0x0018,0x1704))
					throw new ConditionFailedException("0018,1704 is Required if Collimator Shape (0018,1700) is RECTANGULAR.");
				if(!objIItem.contains(0x0018,0x1706))
					throw new ConditionFailedException("0018,1706 is Required if Collimator Shape (0018,1700) is RECTANGULAR.");
				if(!objIItem.contains(0x0018,0x1708))
					throw new ConditionFailedException("0018,1708 is Required if Collimator Shape (0018,1700) is RECTANGULAR.");
			}
			if(objIItem.getDataForTag(0x0018, 0x1700).getValue().equals(EnumCollimatorShape.CIRCULAR))
			{
				if(!objIItem.contains(0x0018,0x1710))
					throw new ConditionFailedException("0018,1710 is Required if Collimator Shape (0018,1700) is CIRCULAR.");
				if(!objIItem.contains(0x0018,0x1712))
					throw new ConditionFailedException("0018,1712 is Required if Collimator Shape (0018,1700) is CIRCULAR.");
			}
			if(objIItem.getDataForTag(0x0018, 0x1700).getValue().equals(EnumCollimatorShape.POLYGONAL))
			{
				if(!objIItem.contains(0x0018,0x1720))
					throw new ConditionFailedException("0018,1720 is Required if Collimator Shape (0018,1700) is POLYGONAL.");
			}
	}
	catch(ConditionFailedException ce)
	{
		if(DicomConfig.createInstance().isLoggingOn())
		{
			DicomLogger.getLogger().log(ce.getMessage());
		}
		throw new ValidationException(ImageSOPInstanceReferenceMacro.class.getSimpleName(),ce); 
	}
	catch( MandatoryMissingException me)
	{
		if(DicomConfig.createInstance().isLoggingOn())
		{
			DicomLogger.getLogger().log(me.getMessage());
		}
		throw new ValidationException(ImageSOPInstanceReferenceMacro.class.getSimpleName(),me);
	}catch( InvalidMacroException ime)
	{
		if(DicomConfig.createInstance().isLoggingOn())
		{
			DicomLogger.getLogger().log(ime.getMessage());
		}
		throw new InvalidItemException(ImageSOPInstanceReferenceMacro.class.getSimpleName(),ime);
	}


	return true;
	}
}
