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

//TODO: Auto-generated Javadoc
/**
 * This class specifies the Attributes that identify and describe the piece of equipment that
 * produces a Series of Composite Instances.
 */
public class GeneralEquipmentModule extends Module   
{
	/**
	 * The obj single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;

	/**
	 * Instantiates a new general equipment module.
	 * 
	 * @param objIDataSet the obj i data set
	 */
	public GeneralEquipmentModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		//Data Element Hash Map
		objSingleCollection.add(new Tag(0x0008,0x0070));
		objSingleCollection.add(new Tag(0x0008,0x0080));
		objSingleCollection.add(new Tag(0x0008,0x0081));
		objSingleCollection.add(new Tag(0x0008,0x1010));
		objSingleCollection.add(new Tag(0x0008,0x1040));
		objSingleCollection.add(new Tag(0x0008,0x1090));
		objSingleCollection.add(new Tag(0x0018,0x1000));
		objSingleCollection.add(new Tag(0x0018,0x1020));
		objSingleCollection.add(new Tag(0x0018,0x1008));
		objSingleCollection.add(new Tag(0x0018,0x1050));
		objSingleCollection.add(new Tag(0x0018,0x1200));
		objSingleCollection.add(new Tag(0x0018,0x1201));
		objSingleCollection.add(new Tag(0x0028,0x0120));	
	}

	/**
	 * 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 true, if validate module
	 * 
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{			
			/*
			 * Validation Rules for Mandatory Elements
			 */
			if(!isPresent(0x0008, 0x0070))
				throw new MandatoryMissingException("0008, 0070");
			
			if(isPresent(0x0028,0x0121))
				if(isPresent(0x7FE0,0x0010) || isPresent(0x0028,0x7FE0))
					if(!isPresent(0x0028,0x0120))
						throw new ConditionFailedException("0x0028,0x0120");
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

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

		}
		catch(ConditionFailedException ce)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ce.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),ce); 
		}
		return true;
	}

	/**
	 * The method is used to set Manufacturer of the equipment that produced the composite instances.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param strManufacturer :The name of the Manufacturer of the equipment
	 * 
	 * 
	 */
	public void setManufacturer(String strManufacturer)
	{
		/*Accept Manufacturer of the equipment that produced the composite instances 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 Hash Map
		 */
		addDataElement(0x0008, 0x0070, DicomConstants.DCM_DT_LO, strManufacturer);
	}

	/**
	 * The method is used to set Institution where the equipment that produced the composite instances is located.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param strInstitutionName :The name of Institution
	 * 
	 * 
	 */
	public void setInstitutionName(String strInstitutionName)
	{
		/*Accept name of Institution where the equipment that produced the composite instances is located 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 Hash Map
		 */
		addDataElement(0x0008, 0x0080, DicomConstants.DCM_DT_LO, strInstitutionName);
	}

	/**
	 * The method is used to set Mailing address of the institution where
	 * the equipment that produced the composite instances is located.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param strInstitutionAddress :The address of Institution
	 * 
	 * 
	 */
	public void setInstitutionAddress(String strInstitutionAddress)
	{
		/*Accept Mailing address of the institution where the equipment that produced 
		 * the composite instances is located 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 Hash Map
		 */
		addDataElement(0x0008, 0x0081, DicomConstants.DCM_DT_ST, strInstitutionAddress);
	}

	/**
	 * The method is used to set User defined name identifying the machine that produced the composite instances.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param strStationName the str station name
	 * 
	 * 
	 */
	public void setStationName(String strStationName)
	{
		/*Accept User defined name identifying the machine that produced the composite instances 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 Hash Map
		 */
		addDataElement(0x0008, 0x1010, DicomConstants.DCM_DT_SH, strStationName);
	}

	/**
	 * The method is used to set Department in the institution where
	 * the equipment that produced the composite instances is located.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param strInstitutionalDepartmentName :The string value of Institutional Department Name
	 * 
	 * 
	 */
	public void setInstitutionalDepartmentName(String strInstitutionalDepartmentName)
	{
		/*Accept name of Institution where the equipment that produced the composite instances is located 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 Hash Map
		 */
		addDataElement(0x0008, 0x1040, DicomConstants.DCM_DT_LO, strInstitutionalDepartmentName);
	}

	/**
	 * The method is used to Manufacturers model name of the equipment that produced the composite instances.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param strManufacturerModelName :Manufacturer Model Name
	 * 
	 * 
	 */
	public void setManufacturerModelName(String strManufacturerModelName)
	{
		/*Accept Manufacturers model name of the equipment that produced 
		 * the composite instances 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 Hash Map
		 */
		addDataElement(0x0008, 0x1090, DicomConstants.DCM_DT_LO, strManufacturerModelName);
	}

	/**
	 * The method is used to set Manufacturers serial number of the equipment that produced the composite instances.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param strDeviceSerialNumber :String value of Device Serial Number
	 * 
	 * 
	 */
	public void setDeviceSerialNumber(String strDeviceSerialNumber)
	{
		/*Accept Manufacturers serial number of the equipment that produced 
		 * the composite instances 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 Hash Map
		 */
		addDataElement(0x0018, 0x1000, DicomConstants.DCM_DT_LO, strDeviceSerialNumber);
		
	}

	/**
	 * This method is used to set Manufacturers designation of software version of the equipment that produced the composite instances.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param arrStrSoftwareVersions :StrSoftware Versions
	 * 
	 * 
	 */
	public void setSoftwareVersions(String[] arrStrSoftwareVersions)
	{
		/*Accept Manufacturers designation of software version of the equipment that produced the composite instances.
		 * in String Array format of type PN.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * 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, 0x1020, DicomConstants.DCM_DT_LO, arrStrSoftwareVersions);
	}
	
	/**
	 * Sets Gantry ID.
	 * Identifier of the gantry or positioner.
	 * @param strGantryID String value of Gantry ID.
	 */
	public void setGantryID(String strGantryID)
	{
		/* Accept Gantry ID 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 Hash Map
		 */
		addDataElement(0x0018, 0x1008, DicomConstants.DCM_DT_LO, strGantryID);
	}


	/**
	 * The method is used to set The inherent limiting resolution in mm of the acquisition equipment
	 * for high contrast objects for the data gathering and reconstruction technique chosen.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param strSpatialResolution the str spatial resolution
	 * 
	 * 
	 */
	public void setSpatialResolution(String strSpatialResolution)
	{
		/*Accept The inherent limiting resolution in mm of the acquisition equipment 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, 0x1050, DicomConstants.DCM_DT_DS, strSpatialResolution);
	}

	/**
	 * This method is used to set Date when the image acquisition device calibration was last changed in any way.
	 * Multiple entries may be used for additional calibrations at other times.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param arrStrDateOfLastCalibration :Date Of Last Calibration
	 * 
	 * 
	 */
	public void setDateOfLastCalibration(String[] arrStrDateOfLastCalibration)
	{
		/*Accept Date when the image acquisition device calibration was last changed in any way. 
		 * Multiple entries may be used for additional calibrations at other times.
		 * in String Array format of type PN.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * 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, 0x1200, DicomConstants.DCM_DT_DA, arrStrDateOfLastCalibration);
	}

	/**
	 * The Method is used to set Time when the image acquisition device calibration was last changed in any way.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param arrStrTimeOfLastCalibration :Time Of Last Calibration
	 * 
	 * 
	 */
	public void setTimeOfLastCalibration(String[] arrStrTimeOfLastCalibration)
	{
		/*Accept Time when the image acquisition device calibration was last changed in any way.
		 * in String Array format of type PN.
		 * Read the Transfer Syntax and check to create ImplicitDE or ExplicitDE .
		 * 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, 0x1201, DicomConstants.DCM_DT_TM, arrStrTimeOfLastCalibration);
	}

	/**
	 * Value of pixels added to non-rectangular image to pad to rectangular format.
	 * The function accepts a String Value and creates the
	 * DE based on the transfer syntax and populates the DE with right values.
	 * This De is then added to the Module HashMap
	 * 
	 * @param sPixelPaddingValue : Pixel Padding Value
	 * 
	 * 
	 */
	/*
	 * NOTE - for tag (0028,0120) the Data type specidfied is SS/US. For the time being it is taken as SS here.
	 */
	public void setPixelPaddingValue(short sPixelPaddingValue)
	{
		/*Accept Value of pixels added to non-rectangular image to pad to rectangular format
		 *  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(0x0028, 0x0120, DicomConstants.DCM_DT_SS, sPixelPaddingValue);
	}

	/**
	 * Gets the manufacturer.
	 * 
	 * @return the manufacturer
	 */
	public String getManufacturer()
	{
		Object objValue = objIDataSet.getValue(0x0008, 0x0070);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the institution name.
	 * 
	 * @return the institution name
	 */
	public String getInstitutionName()
	{
		Object objValue = objIDataSet.getValue(0x0008, 0x0080);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the institution address.
	 * 
	 * @return the institution address
	 */
	public String getInstitutionAddress()
	{
		Object objValue = objIDataSet.getValue(0x0008, 0x0081);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the station name.
	 * 
	 * @return the station name
	 */
	public String getStationName()
	{
		Object objValue = objIDataSet.getValue(0x0008, 0x1010);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the institutional department name.
	 * 
	 * @return the institutional department name
	 */
	public String getInstitutionalDepartmentName()
	{
		Object objValue = objIDataSet.getValue(0x0008, 0x1040);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the manufacturer model name.
	 * 
	 * @return the manufacturer model name
	 */
	public String getManufacturerModelName()
	{
		Object objValue = objIDataSet.getValue(0x0008, 0x1090);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the device serial number.
	 * 
	 * @return the device serial number
	 */
	public String getDeviceSerialNumber()
	{
		Object objValue = objIDataSet.getValue(0x0018, 0x1000);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the software versions.
	 * 
	 * @return the software versions
	 */
	public String[] getSoftwareVersions()
	{
		Object objValue = objIDataSet.getValue(0x0018, 0x1020);
		if(objValue != null)
			return (String[])objValue;
		else
			return null;
	}

	/**
	 * Gets the Gantry ID.
	 * Identifier of the gantry or positioner.
	 * @return the Gantry ID
	 */
	public String getGantryID()
	{
		Object objValue = objIDataSet.getValue(0x0018, 0x1008);
		if(objValue != null)
			return objValue.toString();
		return null;
	}
	
	/**
	 * Gets the spatial resolution.
	 * 
	 * @return the spatial resolution
	 */
	public String getSpatialResolution()
	{
		Object objValue = objIDataSet.getValue(0x0018, 0x1050);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Gets the date of last calibration.
	 * 
	 * @return the date of last calibration
	 */
	public String[] getDateOfLastCalibration()
	{
		Object objValue = objIDataSet.getValue(0x0018, 0x1200);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Gets the time of last calibration.
	 * 
	 * @return the time of last calibration
	 */
	public String[] getTimeOfLastCalibration()
	{
		Object objValue = objIDataSet.getValue(0x0018, 0x1201);
		if(objValue != null)
			return (String[]) objValue;
		return null;
	}

	/**
	 * Retrieves the pixel padding value.
	 * 
	 * @return short - returns the pixel padding value
	 */
	public short getPixelPaddingValue()
	{
		Object objValue = objIDataSet.getValue(0x0028, 0x0120);
		if(objValue != null)
			Short.parseShort(objValue.toString());
		return DicomConstants.DCM_NO_VALUE;
	}

	/**
	 * This method returns the custom collection containing the list of DataElements belonging to the CT
	 * Image Module specified by DICOM Standard.
	 * 
	 * @return - the customized collection containing the dataelement tags
	 * that beling to this module
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
