/*******************************************************************************
 *  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.utils.EnumDataType;
import cdac.medinfo.sdk.hl7282.utils.IDataTypeMap;

/**
 * NA - Numeric Array<p>
 * This data type type is used to represent a series (array) of
 * numeric values. A field of this type may contain a one-dimensional array
 *(vector or row) of numbers. 
 * <p>Represents an HL7 NA data type. 
 * This data type has the following attributes:</p><p>
 *  1. Value1 (NM)<b>Optional</b><br>
 *  2. Value2 (NM)<b>Optional</b><br>
 *  3. Value3 (NM)<b>Optional</b><br>
 *  4. Value4 (NM)<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 NA extends CompositeDataType    
{
	
	/**
	 * Default Constructor for NA
	 */
	public NA()
	{
		super(EnumDataType.HL7_DT_NA);
		 
	}

	/**
	 * Constructor  for NA 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 NA(int iMinLength, int iMaxLength, String strCLength,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_NA);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}

	/**
	 * Constructor for NA 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 NA(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_NA);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iTableNo = iTableNo;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());		
		}
	}
	/**
	 * Constructor for NA class which takes two 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 NA(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_NA);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for NA 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 NA(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_NA);
		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 NA 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 NA(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_NA);
		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 value of single element.
	 * @param strValue1 value of single element. 
	 */
	public void setValue1(String strValue1)
	{
		if(strValue1!=null)
			setComponentType(1, strValue1);
		else
			setValue1((NM)null);
		
	}

	/**
	 * Sets the value of single element.
	 * @param objNM value of single element. 
	 */
	public void setValue1(NM objNM)
	{
		setComponentType(1,objNM);
	}

	/**
	 * Sets the value of single element.
	 * @param strValue2 value of single element. 
	 */
	public void setValue2(String strValue2)
	{
		if(strValue2!=null)
			setComponentType(2, strValue2);
		else
			setValue1((NM)null);
	}

	/**
	 * Sets the value of single element.
	 * @param objNM value of single element. 
	 */
	public void setValue2(NM objNM)
	{
		setComponentType(2,objNM);
	}

	/**
	 * Sets the value of single element.
	 * @param strValue3 value of single element. 
	 */
	public void setValue3(String strValue3)
	{
		if(strValue3!=null)
			setComponentType(3, strValue3);
		else
			setValue1((NM)null);
	}

	/**
	 * Sets the value of single element.
	 * @param objNM value of single element. 
	 */
	public void setValue3(NM objNM)
	{
		setComponentType(3,objNM);
	}

	/**
	 * Sets the value of single element.
	 * @param strValue4 value of single element. 
	 */
	public void setValue4(String strValue4)
	{
		if(strValue4!=null)
			setComponentType(4, strValue4);
		else
			setValue1((NM)null);
	}

	/**
	 * Sets the value of single element.
	 * @param objNM value of single element. 
	 */
	public void setValue4(NM objNM)
	{
		setComponentType(4,objNM);
	}
	/**
	 * Returns object of Value1.
	 * @return NM representation of value1.
	 */
	public NM getValue1()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns object of Value2.
	 * @return NM representation of value2.
	 */
	public NM getValue2()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns object of Value3.
	 * @return NM representation of value3.
	 */
	public NM getValue3()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}
	/**
	 * Returns object of Value4.
	 * @return NM representation of value4.
	 */
	public NM getValue4()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)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();
//		/*
//		 * This function validates for the condition,
//		 * that at list one element should be present in the vector.
//		 */
//		if(getComponentType(1)!=null||getComponentType(2)!=null||getComponentType(3)!=null)
//		{
//			return true;
//		}
//		else 
//			return false;
//	}

	//	/**
	//	 * Read data from stream
	//	 * @param objIHL7Stream Object of HL7Stream
	//	 * @param objIDelimiter object of class Delimiter
	//	 * @return integer - Number of bytes read.
	//	 *				   - Fail if data read is not valid.
	//	 * @throws DelimiterMissingException - if Delimeter is not present.
	//	 * @throws IOException - if IO operation fails. 
	//	 */
	//	public int read(IHL7Stream objIHL7Stream,IDelimiter objIDelimiter) throws DelimiterMissingException,InvalidDataTypeException,IOException
	//	{
	//		char cSeperator = '\0';
	//		int iReadBytes = 0;
	//		char cReadChar = '\0';
	//		ISingleCollection<NM> objValues = null;
	//		try
	//		{
	//			if(iComponentType == '\0')
	//				return HL7Constants.HL7_FUNCTION_FAIL;
	//			if(iComponentType == HL7Constants.HL7_COMPONENT)
	//			{
	//				cSeperator = objIDelimiter.getComponentSeparator();
	//				if(cSeperator == '\0')
	//				{
	//					String strMessage = "Component separator is missing";
	//					throw new DelimiterMissingException(strMessage);
	//				}
	//			}
	//			else if(iComponentType == HL7Constants.HL7_SUBCOMPONENT)
	//			{
	//				cSeperator = objIDelimiter.getSubComponentSeparator();
	//				if(cSeperator == '\0')
	//				{
	//					String strMessage = "Component separator is missing";
	//					throw new DelimiterMissingException(strMessage);
	//				}
	//			}		
	//			do
	//			{
	//				NM tempValue = new NM(16);
	//				int iRetVal = tempValue.read(objIHL7Stream, objIDelimiter);
	//				if(iRetVal == 0)
	//					tempValue = null;
	//				else if(iRetVal == HL7Constants.HL7_FUNCTION_FAIL)
	//				{
	//					tempValue = null;
	//					return iRetVal;
	//				}
	//				else
	//				{
	//					iReadBytes += iRetVal;
	//					if(objValues == null)
	//						objValues = new SingleCollection<NM>();
	//					objValues.add(tempValue);
	//					tempValue = null;
	//				}
	//				cReadChar = '\0';
	//				cReadChar = objIHL7Stream.readChar();
	//			}while(cReadChar == cSeperator);
	//			//Reset Stream to Delimiter 
	//			objIHL7Stream.reset();
	//			if(iReadBytes > 0)
	//			{
	//				if(objValues != null)
	//					setValues(objValues);
	//				objValues = null;
	//				if(!validate())
	//					return HL7Constants.HL7_FUNCTION_FAIL;
	//			}
	//			return iReadBytes;
	//		}
	//		catch(InvalidDataTypeException ex)
	//		{
	//			throw new InvalidDataTypeException(ex.getMessage(),ex);
	//		}
	//	}
	/**
	 * 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 NA)))
			return false;

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

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

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

		if(this.getValue4() == null)
		{
			if(((NA)objObject).getValue4() != null)
				return false;
		}
		else if(false == (this.getValue4().equals(((NA)objObject).getValue4())))
			return false;
		
		return true;
	}
}
