/*******************************************************************************
 *  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.IDicomValue;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumContainerComponentMaterial;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
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 Container Component Sequence .
 */


public class ContainerComponentSequenceItem extends Item
{

	private static final long serialVersionUID = 1L;

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

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

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

	/**
	 * Retrieves pre-initialized object of Container Component Type Code 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 ContainerComponentTypeCodeSequence - instance of ContainerComponentTypeCodeSequence. 
	 */
	public ContainerComponentTypeCodeSequence getContainerComponentTypeCodeSequence()
	{
		ContainerComponentTypeCodeSequence objContainerComponentTypeCodeSequence = null;
		IDataElement objIDataElement =  getDataForTag(0x0008,0x1140);
		if(objIDataElement != null)
		{
			IDicomValue objIDicomValue = objIDataElement.getDicomValue();
			if(objIDicomValue != null)
				objContainerComponentTypeCodeSequence = (ContainerComponentTypeCodeSequence)objIDicomValue;
		}
		else
		{
			DicomV3Factory objDicomV3Factory = DicomV3Factory.createInstance();
			int iVRTransferSyntax = objITransferSyntax.getVRTransferSyntax();
			objIDataElement = null;
			objContainerComponentTypeCodeSequence = (ContainerComponentTypeCodeSequence)objDicomV3Factory.createSequence(0x0040,0xA170, objITransferSyntax, objIDictionaryReader);

			if(iVRTransferSyntax == DicomConstants.DCM_VR_IMPLICIT)
				objIDataElement = new ImplicitDE(0x0008,0x1140, objContainerComponentTypeCodeSequence.getValue(),objIDictionaryReader);
			else
				objIDataElement = new ExplicitDE(0x0008,0x1140, DicomConstants.DCM_DT_SQ, objContainerComponentTypeCodeSequence.getValue());
			objIDataElement.setTransferSyntax(objITransferSyntax);
			if(objIDataElement != null)
				addDataElement(objIDataElement);
		}
		return objContainerComponentTypeCodeSequence;
	}
	/**
	 * Sets Manufacturer.
	 * @param strManufacturer String value of Manufacturer.
	 */
	public void setManufacturer(String strManufacturer)
	{
		/* Accept the Manufacturer 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(0x0008,0x0070, strManufacturer,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x0070,DicomConstants.DCM_DT_LO, strManufacturer);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for Manufacturer.
	 * @return String - value of Manufacturer.
	 */
	public String getManufacturer()
	{
		Object objValue = getValue(0x0008,0x0070);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets ManufacturerModelName.
	 * @param strManufacturerModelName String value of ManufacturerModelName.
	 */
	public void setManufacturerModelName(String strManufacturerModelName)
	{
		/* Accept the ManufacturerModelName 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(0x0008,0x1090, strManufacturerModelName,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0008,0x1090,DicomConstants.DCM_DT_LO, strManufacturerModelName);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for Manufacturer Model Name.
	 * @return String - value of Manufacturer Model Name.
	 */
	public String getManufacturerModelName()
	{
		Object objValue = getValue(0x0008,0x1090);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets ContainerComponentID.
	 * @param strManufacturerModelName String value of ContainerComponentID.
	 */
	public void setContainerComponentID(String strContainerComponentID)
	{
		/* Accept the ContainerComponentID 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(0x0050,0x001B, strContainerComponentID,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0050,0x001B,DicomConstants.DCM_DT_LO, strContainerComponentID);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for Container ComponentID.
	 * @return String - value of Container ComponentID.
	 */
	public String getContainerComponentID()
	{
		Object objValue = getValue(0x0050,0x001B);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets the Container Component Length.
	 * @param dblContainerComponentLength Double value of coordinates
	 */
	public void setContainerComponentLength(double dblContainerComponentLength)
	{
		/*Accept the Container Component Length in double format of type FD.
		 * 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(0x0050,0x001C, dblContainerComponentLength,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0050,0x001C,DicomConstants.DCM_DT_FD, dblContainerComponentLength);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of Container Component Length.
	 * @return double - double value of Container Component Length
	 */
	public double getContainerComponentLength()
	{
		Object objValue = getValue(0x0050,0x001C);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the Container Component Width.
	 * @param dblContainerComponentWidth Double value of coordinates
	 */
	public void setContainerComponentWidth(double dblContainerComponentWidth)
	{
		/*Accept the Container Component Length in double format of type FD.
		 * 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(0x0050,0x0015, dblContainerComponentWidth,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0050,0x0015,DicomConstants.DCM_DT_FD, dblContainerComponentWidth);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of Container Component Width.
	 * @return double - double value of Container Component Width
	 */
	public double getContainerComponentWidth()
	{
		Object objValue = getValue(0x0050,0x0015);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the Container Component Diameter.
	 * @param dblContainerComponentDiameter Double value of coordinates
	 */
	public void setContainerComponentDiameter(double dblContainerComponentDiameter)
	{
		/*Accept the Container Component Diameter in double format of type FD.
		 * 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(0x0050,0x001D, dblContainerComponentDiameter,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0050,0x001D,DicomConstants.DCM_DT_FD, dblContainerComponentDiameter);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of Container ComponentDiameter.
	 * @return double - double value of Container ComponentDiameter
	 */
	public double getContainerComponentDiameter()
	{
		Object objValue = getValue(0x0050,0x001D);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Sets the Container ComponentThickness.
	 * @param dblContainerComponentThickness Double value of coordinates
	 */
	public void setContainerComponentThickness(double dblContainerComponentThickness)
	{
		/*Accept the Container Component Diameter in double format of type FD.
		 * 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(0x0050,0x0013, dblContainerComponentThickness,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0050,0x0013,DicomConstants.DCM_DT_FD, dblContainerComponentThickness);

		addDataElement(objIDataElement);
	}
	/**
	 * Retrieves the value of Container ComponentThickness.
	 * @return double - double value of Container ComponentThickness.
	 */
	public double getContainerComponentThickness()
	{
		Object objValue = getValue(0x0050,0x0013);
		if(objValue != null)
			return Double.parseDouble(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}
	/**
	 * Material of container component.
	 *Defined Terms:
	 *GLASS
	 *PLASTIC
	 *METAL
	 * @param enumContainerComponentMaterial enum value of Container Component Material.
	 */
	public void setContainerComponentMaterial(EnumContainerComponentMaterial enumContainerComponentMaterial)
	{
		/* Accept the Container Component Material 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(0x0050,0x001E, enumContainerComponentMaterial.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0050,0x001E,DicomConstants.DCM_DT_CS, enumContainerComponentMaterial.getValue());

		addDataElement(objIDataElement);


	}

	/**
	 * Return type of setup device used for Container Component Material. 
	 * @return String - value of Container Component Material.
	 */
	public String getContainerComponentMaterial()
	{
		Object objValue = getValue(0x0050,0x001E);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	/**
	 * Sets Container ComponentDescription.
	 * @param strContainerComponentDescription String value of Container ComponentDescription.
	 */
	public void setContainerComponentDescription(String strContainerComponentDescription)
	{
		/* Accept the Container ComponentDescription 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(0x0050,0x001E, strContainerComponentDescription,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0050,0x001E,DicomConstants.DCM_DT_LO, strContainerComponentDescription);

		addDataElement(objIDataElement);
	}

	/**
	 * Return user-defined label identifier for Container ComponentDescription.
	 * @return String - value of Container ComponentDescription.
	 */
	public String getRescaleType()
	{
		Object objValue = getValue(0x0050,0x001E);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Validates all the attributes in Container Component Sequence 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(0x0050,0x0012))
				throw new MandatoryMissingException("0050,0012");
			else
				getContainerComponentTypeCodeSequence().validateForModule();
			}
		catch(MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new InvalidItemException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
