/*******************************************************************************
 *  Copyright 2017 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.hl7282.baselibs;

import cdac.medinfo.sdk.hl7282.baselibs.segmentfactory.DataElementFactory;
import cdac.medinfo.sdk.hl7282.commoninterface.IDataType;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidComponentTypeException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidDataTypeException;
import cdac.medinfo.sdk.hl7282.exceptions.MissingValueException;
import cdac.medinfo.sdk.hl7282.utils.EnumDataType;
import cdac.medinfo.sdk.hl7282.utils.IDataTypeMap;

/**
 * ICD - Insurance Certification Definition<p>
 * This data type specifies whether insurance certification is required for particular patient types, 
 * and the time window for obtaining the certification. 
 * <p>Represents an HL7 ICD data type. 
 * This data type has the following attributes:</p><p>
 *  1. Certification Patient Type (CWE)<b>Optional</b><br>
 *  2. Certification Required (ID)<b>Required</b><br>
 *  3. Date/Time Certification Required (DTM)<b>Optional</b><br><p>
 * This class provides accessors & mutators for the elements mentioned above.
 * This class override the equal method, to check whether some other object is "equal to" this one.
 * 
 * <p>User can also create Data Type through {@link DataElementFactory} 
 */
public class ICD extends CompositeDataType    
{
	

	/**
	 * Default Constructor for ICD
	 */
	public ICD()
	{
		super(EnumDataType.HL7_DT_ICD);
		 
	}

	/**
	 * Constructor  for ICD class which takes four arguments and 
	 * sets the value in the respective member variable.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */ 
	public ICD(int iMinLength, int iMaxLength, String strCLength,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_ICD);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}

	/**
	 * Constructor for ICD class which takes five arguments and 
	 * sets the value in the respective member variable.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iTableNo The Table number to which the value for the component is specified. 
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public ICD(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_ICD);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iTableNo = iTableNo;
		this.iComponentType = iComponentType;
	}

	/**
	 * Constructor for ICD class which takes four arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public ICD(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ICD);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for ICD class which takes five arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public ICD(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ICD);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for ICD class which takes six arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iTableNo The Table number to which the value for the component is specified. 
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public ICD(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_ICD);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		this.iTableNo = ITableNo;
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}	

	/**
	 *Sets the CertificationPatientType.
	 *@param strCertificationPatientType Specifies Patient Type 
	 */
	public void setCertificationPatientType(String strCertificationPatientType)
	{
		if(strCertificationPatientType!=null)
			setComponentType(1, strCertificationPatientType);
		else
			setCertificationPatientType((CWE)null);
	}
	
	/**
	 *Sets the CertificationPatientType.
	 *@param objCWE Specifies Patient Type
	 */
	public void setCertificationPatientType(CWE objCWE)
	{
		setComponentType(1,objCWE);
	}

	/**
	 *Sets the CertificationRequired.
	 *@param strCertificationRequired Specifies certification is required or not 
	 */
	public void setCertificationRequired(String strCertificationRequired)
	{
		if(strCertificationRequired!=null)
			setComponentType(2, strCertificationRequired);
		else
			setCertificationRequired((ID)null);
	}

	/**
	 *Sets the CertificationRequired.
	 *@param objID Specifies certification is required or not
	 */
	public void setCertificationRequired(ID objID)
	{
		setComponentType(2,objID);
	}

	/**
	 *Sets the DateTimeCertificationRequired.
	 *@param objDateTimeCertificationRequired Specifies Date/Time by which pre-certificationification must be obtained
	 */
	public void setDateTimeCertificationRequired(DTM objDateTimeCertificationRequired)
	{
		setComponentType(3,objDateTimeCertificationRequired);
	}

	/**
	 * Returns Certification Patient Type
	 * @return CWE - data type.
	 */
	public CWE getCertificationPatientType()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CWE)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Certification Required
	 * @return ID - data type.
	 */
	public ID getCertificationRequired()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (ID)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Date/Time Certification Required
	 * @return DTM - data type.
	 */
	public DTM getDateTimeCertificationRequired()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (DTM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * The Method validates the Input value,and if validated properly then returns true.
	 * Here the validetion is based on the compulsory fields for the Datatype. 
	 * @return boolean - true or false.
	 * @throws InvalidDataTypeException - if Invalid values is entered or Mandatory field is missing.
	 */
	public boolean validate() throws InvalidDataTypeException
	{
		super.validate();
		if(getComponentType(2)==null)
		{
			throw new MissingValueException(this.getClass().getSimpleName() + "Certification Required");
		}
		else
			return true;
	}

	/**
	 * This method checks the equality of Object.
	 * @param objObject object of Object class.
	 * @return boolean - shows success or failure of the comparison.
	 */
	public boolean equals(Object objObject)
	{
		if((objObject == null ) || (!(objObject instanceof ICD)))
			return false;

		if(this.getCertificationPatientType() == null)
		{
			if(((ICD)objObject).getCertificationPatientType() != null)
				return false;
		}
		else if(false == (this.getCertificationPatientType().equals(((ICD)objObject).getCertificationPatientType())))
			return false;

		if(this.getCertificationRequired() == null)
		{
			if(((ICD)objObject).getCertificationRequired() != null)
				return false;
		}
		else if(false == (this.getCertificationRequired().equals(((ICD)objObject).getCertificationRequired())))
			return false;

		if(this.getDateTimeCertificationRequired() == null)
		{
			if(((ICD)objObject).getDateTimeCertificationRequired() != null)
				return false;
		}
		else if(false == (this.getDateTimeCertificationRequired().equals(((ICD)objObject).getDateTimeCertificationRequired())))
			return false;

		return true;
	}
}
