/*******************************************************************************
 *  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 java.io.IOException;

import cdac.medinfo.sdk.hl7282.baselibs.segmentfactory.DataElementFactory;
import cdac.medinfo.sdk.hl7282.commoninterface.IDelimiter;
import cdac.medinfo.sdk.hl7282.commoninterface.IHL7Stream;
import cdac.medinfo.sdk.hl7282.exceptions.DelimiterMissingException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidDataTypeException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidDelimiterException;
import cdac.medinfo.sdk.hl7282.utils.EnumDataType;
import cdac.medinfo.sdk.hl7282.utils.HL7Constants;

/**
 * The class is Extended from PrimitiveDataType.
 * Used when data type is unknown. 
 * <p>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 UN extends PrimitiveDataType    
{
	

	String strBuffer;
	protected int iValueLength;
	protected Object value;

	/**
	 * Constructor for UN class. 
	 */
	public UN()
	{
		super(EnumDataType.HL7_DT_UN);
		this.iDataType = HL7Constants.HL7_DT_UN;
	}

	/**
	 * Constructor for UN class which takes three argument 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.
	 */
	public UN(int iMinLength, int iMaxLength, String strCLength)
	{
		super(EnumDataType.HL7_DT_UN);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iDataType = HL7Constants.HL7_DT_UN;
	}

	/**
	 * Constructor for UN 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 iTableNo The Table number to which the value for the component is specified. 
	 */
	public UN(int iMinLength, int iMaxLength, String strCLength,int iTableNo)
	{
		super(EnumDataType.HL7_DT_UN);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iTableNo = iTableNo;
		this.iDataType = HL7Constants.HL7_DT_UN;
	}


	/**
	 * Read data from String and also it take care of repeated values of this DataType
	 * @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 Delimiter is absent.
	 *@throws InvalidDelimiterException - if Delimiter is not valid
	 *@throws IOException - if IO Exception fails.
	 */
	public int read(String strSegmentStream, IDelimiter objIDelimiter) 
			throws DelimiterMissingException, InvalidDelimiterException, InvalidDataTypeException, IOException 
	{
		int iReadBytes = 0;
		strBuffer = "";
		char cTemp = '\0';
		int i=0;
		do
		{		
			cTemp = strSegmentStream.charAt(i++);
//			if((char)objIDelimiter.getRepetitionSeparator() != cTemp &&(char)objIDelimiter.getFieldSeparator()!= cTemp && cTemp != (char)'\r')
//			{
				strBuffer += cTemp;
//			}

			//Read till RepetationSeparator found and add it to byteBuffer.
//			if((char)objIDelimiter.getRepetitionSeparator()== cTemp || (char)'\r' == cTemp)
//			{				
//				return iReadBytes = strBuffer.length();
//			}
		}while((char)objIDelimiter.getFieldSeparator() != cTemp && (char)'\r' != cTemp && (char)objIDelimiter.getRepetitionSeparator() != cTemp);

		return iReadBytes = strBuffer.length();
	}

	/**
	 * Writes the unknown data to the stream from the byte buffer
	 * This function writes the Data Value to the Stream
	 * @param objIHL7Stream  stream onto which the FD DataType is to serialized
	 * @param objIDelimiter object of class Delimiter
	 * @return HL7_FUNCTION_SUCCESS -on successful write
	 * @throws DelimiterMissingException thrown when Delimiter is missing
	 * @throws IOException thrown when IO operation fails
	 */ 
	@Override
	public int write(IHL7Stream objIHL7Stream, IDelimiter objIDelimiter) throws DelimiterMissingException, IOException 
	{
		if(strBuffer!=null)
		{
			objIHL7Stream.writeString(strBuffer);
		}

		return HL7Constants.HL7_FUNCTION_SUCCESS;
	}

	/**
	 * This validate function validates the data value object.
	 * This Validate method return true, because we can not understand what type of Data Type it contains (it is in String format).
	 * @return boolean - function status [Success/Failure].
	 */
	public boolean validate()
	{
		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 UN)))
			return false;

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

		return true;
	}
}
