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



import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;
import cdac.medinfo.sdk.dcm30_04.enums.EnumImageLaterality;
import cdac.medinfo.sdk.dcm30_04.enums.EnumImplantPresent;
import cdac.medinfo.sdk.dcm30_04.enums.EnumOrganExposed;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPartialView;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPositionerType;

import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
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;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

/**
 \* This module contains IOD Attributes that describe a Digital Mammography X-Ray Image
 * including its acquisition and positioning.
 * 
 * 
 */
public class MammographyImageModule extends Module  
{


	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;

	

	/**
	 * Instantiates a new mammography image module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public MammographyImageModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0018,0x1110));
		objSingleCollection.add(new Tag(0x0018,0x1111));
		objSingleCollection.add(new Tag(0x0018,0x1508));
		objSingleCollection.add(new Tag(0x0018,0x1510));
		objSingleCollection.add(new Tag(0x0018,0x1511));
		objSingleCollection.add(new Tag(0x0020,0x0062));
		objSingleCollection.add(new Tag(0x0028,0x1300));
		objSingleCollection.add(new Tag(0x0028,0x1350));
		objSingleCollection.add(new Tag(0x0028,0x1351));
		objSingleCollection.add(new Tag(0x0040,0x0318));
	}

	/**
	 * This method validates each Attribute in the Module according to the Type
	 * of the Attribute.Attributes are of 5 types.
	 * Type 1,Type 2 are Mandatory:
	 * Mandatory Attributes have to present in the Module.
	 * Type 1C,2C are Conditional:
	 * Conditional Attributes' presence depend on a condition.
	 * If the condition is satisfied the Attribute has to be present.
	 * Type 3 are Optional
	 * Optional Attributes can be added as and when required.
	 * 
	 * @return - validation status as success or failure
	 * 
	 * @throws InvalidModuleException - when the module is invalid
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{			
			if(!isPresent(0x0018, 0x1508))
				throw new MandatoryMissingException("Missing Mandatory Element 0018, 1508");
			else if(!isPresent(0x0020, 0x0062))
				throw new MandatoryMissingException("Missing Mandatory Element 0020, 0062");
			else if(!isPresent(0x0040, 0x0318))
				throw new MandatoryMissingException("Missing Mandatory Element 0040, 0318");
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),mme);

		}
		return true;
	}
	/**
	 * Sets the Value Type Defined in Terms of TEXT,NUMERIC,CODE,DATE,TIME,PNAME
	 * @param strImageType Specifies type of the value encoded in this Item
	 */
	public void setImageType(String strImageType)
	{
		/*Accept the Value Type 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
		 */

		addDataElement(0x0008, 0x0008,DicomConstants.DCM_DT_CS, strImageType);
	}
	/**
	 * Retrieves type of the value encoded in this Item
	 * @return String - Type of the value encoded in this Item
	 */
	public String getImageType()
	{
		Object objValue = getValue(0x0008, 0x0008);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets Positioner Type. Enumerated Values: MAMMOGRAPHIC,NONE.
	 * 
	 * @param enumPositionerType - Enum value of Positioner Type.
	 */
	public void setPositionerType(EnumPositionerType enumPositionerType)
	{
		/* Accept Positioner Type 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 Hash Map
		 */
		addDataElement(0x0018, 0x1508, DicomConstants.DCM_DT_CS, enumPositionerType.getValue());
	}

	/**
	 * Sets Positioner Type.
	 * 
	 * @return String - String value of Positioner Type.
	 */
	public String getPositionerType()
	{Object objectValue = getValue(0x0018,0x1508);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Distance in mm from source to isocenter.
	 * 
	 * @param strDistanceSourceToPatient - String value of Distance of Source To Patient
	 */
	public void setDistanceSourceToPatient(String strDistanceSourceToPatient)
	{
		/*Accept Distance in mm from source to isocenter 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 Hash Map
		 */
		addDataElement(0x0018, 0x1111, DicomConstants.DCM_DT_DS, strDistanceSourceToPatient);
	}

	/**
	 * Retrieves Distance in mm from source to isocenter.
	 * 
	 * @return String value of Distance of Source To Patient
	 */
	public String getDistanceSourceToPatient()
	{
		Object objectValue = getValue(0x0018, 0x1111);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Distance in mm from transmission source to the detector face. Required if Image
	 * Type (0008,0008) Value 4 is TRANSMISSION, Value 3 is not TOMO, and a sequence Item is present.
	 * 
	 * @param strDistanceSourceToDetector - String value of Distance Source to Detector
	 * 
	 * @return the int
	 */
	public void setDistanceSourceToDetector(String strDistanceSourceToDetector)
	{
		/*Accept the Distance Source to Detector 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
		 */
		addDataElement(0x0018,0x1110, DicomConstants.DCM_DT_DS, strDistanceSourceToDetector);
	}

	/**
	 * Return Distance in mm from transmission source to the detector face.
	 * 
	 * @return String - String value of Distance Source to Detector
	 */
	public String getDistanceSourceToDetector()
	{Object objectValue = getValue(0x0018,0x1110);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets position of the X-Ray beam about the patient from the RAO to LAO direction
	 * where movement from RAO to vertical is positive, if Positioner Type (0018,1508) is
	 * CARM.
	 * 
	 * @param strPositionerPrimaryAngle - String value of Positioner Primary Angle
	 */
	public void setPositionerPrimaryAngle(String strPositionerPrimaryAngle)
	{
		/* Accept Positioner Primary Angle 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 Hash Map
		 */
		addDataElement(0x0018, 0x1510, DicomConstants.DCM_DT_DS, strPositionerPrimaryAngle);
	}

	/**
	 * Sets position of the X-Ray beam about the patient from the RAO to LAO direction
	 * where movement from RAO to vertical is positive, if Positioner Type (0018,1508) is
	 * CARM.
	 * 
	 * @return String - String value of Positioner Primary Angle
	 */
	public String getPositionerPrimaryAngle()
	{Object objectValue = getValue(0x0018,0x1510);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Position of the X-Ray beam about the patient from the CAU to CRA direction
	 * where movement from CAU to vertical is positive, if Positioner Type (0018,1508) is
	 * CARM.
	 * 
	 * @param strPositionerSecondaryAngle - String value of Positioner Secondary Angle
	 */
	public void setPositionerSecondaryAngle(String strPositionerSecondaryAngle)
	{
		/* Accept Positioner Secondary Angle 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 Hash Map
		 */
		addDataElement(0x0018, 0x1511, DicomConstants.DCM_DT_DS, strPositionerSecondaryAngle);
	}

	/**
	 * Retrievs position of the X-Ray beam about the patient from the CAU to CRA direction
	 * where movement from RAO to vertical is positive, if Positioner Type (0018,1508) is
	 * CARM.
	 * 
	 * @return String - String value of Positioner Secondary Angle
	 */
	public String getPositionerSecondaryAngle()
	{Object objectValue = getValue(0x0018,0x1511);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Laterality of (possibly paired) body part (as described in Anatomic Region Sequence(0008,2218)) examined.
	 * Enumerated Values:
	 * R = right
	 * L = left
	 * U = unpaired
	 * B = both left and right
	 * 
	 * @param enumImageLaterality the enum image laterality
	 */
	public void setImageLaterality(EnumImageLaterality enumImageLaterality)
	{
		/*Accept Image Laterality 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 Hash Map
		 */
		addDataElement(0x0020, 0x0062, DicomConstants.DCM_DT_CS, enumImageLaterality.getValue());
	}

	/**
	 * Retrieves Laterality of (possibly paired) body part (as described in Anatomic Region Sequence(0008,2218)) examined.
	 * 
	 * @return String value of Image Laterality.
	 */
	public String getImageLaterality()
	{
		Object objectValue = getValue(0x0020, 0x0062);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Organ to which Organ Dose (0040,0316) applies. Defined Terms:BREAST,GONADS,BONE MARROW,FETUS,LENS.
	 * 
	 * @param enumOrganExposed the enum organ exposed
	 */
	public void setOrganExposed(EnumOrganExposed enumOrganExposed)
	{
		/* Accept Organ Exposed 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 Hash Map
		 */
		addDataElement(0x0040,0x0318, DicomConstants.DCM_DT_DS, enumOrganExposed.getValue());
	}

	/**
	 * Retrieves Organ to which Organ Dose (0040,0316) applies.
	 * 
	 * @return String - String value of Organ Dose.
	 */
	public String getOrganExposed()
	{Object objectValue = getValue(0x0040,0x0318);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets whether or not an implant is present. Enumerated Values: YES,NO.
	 * 
	 * @param enumImplantPresent - enum value of Implant Present.
	 */
	public void setImplantPresent(EnumImplantPresent enumImplantPresent)
	{
		/* Accept Implant Present 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 Hash Map
		 */
		addDataElement(0x0028,0x1300, DicomConstants.DCM_DT_CS, enumImplantPresent.getValue());
	}

	/**
	 * Retrieves whether or not an implant is present.
	 * 
	 * @return String - String value of Implant Present.
	 */
	public String getImplantPresent()
	{Object objectValue = getValue(0x0028,0x1300);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets value that indicates whether this image is a partial view, that is a subset of a single view of the breast.
	 * Enumerated Values: YES, NO.
	 * 
	 * @param enumPartialView - enum value of Partial View.
	 */
	public void setPartialView(EnumPartialView enumPartialView)
	{
		/* Accept Partial View 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 Hash Map
		 */
		addDataElement(0x0028,0x1350, DicomConstants.DCM_DT_CS, enumPartialView.getValue());
	}

	/**
	 * Retrieves value that indicates whether this image is a partial view.
	 * 
	 * @return String - String value of Partial View.
	 */
	public String getPartialView()
	{Object objectValue = getValue(0x0028,0x1350);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}

	/**
	 * Sets Free text description of the portion of the breast captured in a partial view image.
	 * 
	 * @param strPartialViewDescription - String value of Partial View Description.
	 */
	public void setPartialViewDescription(String strPartialViewDescription)
	{
		/* Accept Partial View Description in String format of type ST.
		 * 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
		 */
		addDataElement(0x0028,0x1351, DicomConstants.DCM_DT_ST, strPartialViewDescription);
	}

	/**
	 * Retrieves Free text description of the portion of the breast captured in a partial view image.
	 * 
	 * @return String - String value of Partial View Description.
	 */
	public String getPartialViewDescription()
	{Object objectValue = getValue(0x0028,0x1351);
		if(objectValue!=null)
			return objectValue.toString();
		return null;
	}


	/**
	 * Returns the custom collection containing the list of DataElements belonging to the CT
	 * Image Module specified by DICOM Standard.
	 * 
	 * @return collection - custom collection containing the list of DataElements belonging to the CT Image Module
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
