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

import java.io.IOException;
import java.util.Locale;

import cdac.medinfo.sdk.hl7282.commoninterface.IDelimiter;
import cdac.medinfo.sdk.hl7282.commoninterface.IHL7Stream;
import cdac.medinfo.sdk.hl7282.exceptions.FieldMissingException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidFieldException;
import cdac.medinfo.sdk.hl7282.utils.HL7Config;
import cdac.medinfo.sdk.hl7282.utils.HL7Constants;
import cdac.medinfo.sdk.hl7282.utils.ResourceBundleWrapper;

class Delimiter implements IDelimiter  
{
	
	
	char cFieldSeparator;
	char cComponentSeparator;
	char cSubComponentSeparator;
	char cRepetitionSeparator;
	char cEscapeCharacter;
	char cTruncationCharacter;
	
	/**
	 * @return char - ComponentSeparator
	 */
	public char getComponentSeparator() 
	{
		return cComponentSeparator;
	}
	/**
	 * @return char - EscapeCharacter
	 */	
	public char getEscapeCharacter() 
	{
		return cEscapeCharacter;
	}
	/**
	 * @return char - FieldSeparator
	 */
	public char getFieldSeparator() 
	{
		return cFieldSeparator;
	}
	/**
	 * @return char - RepetitionSeparator
	 */
	public char getRepetitionSeparator() 
	{
		return cRepetitionSeparator;
	}
	/**
	 * @return char - SubComponentSeparator
	 */
	public char getSubComponentSeparator() 
	{
		return cSubComponentSeparator;
	}
	/**
	 * @return char - truncation Character
	 */
	public char getTruncationCharacter() {
		
		return cTruncationCharacter;
	}
	/**
	 * Writes the Encoding characters field
	 * This field contains the four characters in the following order: 
	 * the component separator, repetition separator, escape character, and subcomponent separator
	 * @param objIHL7Stream Object of HL7Stream.
	 * @return int - Success / Fail
	 * @throws IOException 
	 */
	public int writeDelimiters(IHL7Stream objIHL7Stream) throws IOException
	{
		objIHL7Stream.writeChar(cComponentSeparator);
		objIHL7Stream.writeChar(cRepetitionSeparator);
		objIHL7Stream.writeChar(cEscapeCharacter);
		objIHL7Stream.writeChar(cSubComponentSeparator);
		objIHL7Stream.writeChar(cTruncationCharacter);
		return HL7Constants.HL7_FUNCTION_SUCCESS;
	}
	/**
	 * Read delimiters from string
	 * @param strEncodingChars String of encoding characters.
	 * @return int - Fail if delimiters are not valid.
	 * 			   - Number of charaters read.
	 */
	public int readDelimiters(String strEncodingChars) //throws IOException
	{
		int iReadBytes = 0;
		int i = 0;
		//Read component separator
		cComponentSeparator = strEncodingChars.charAt(i);
		i++;
		if(cComponentSeparator == cFieldSeparator)
		{
			cComponentSeparator = '\0';
			String strMessage = ResourceBundleWrapper.getValue("exceptions", "MissingEncodingCharacter", new Locale(HL7Config.createInstance().getLocale()));
			throw new FieldMissingException(strMessage);
		}
		iReadBytes++;
		//Read repetition separator
		cRepetitionSeparator = strEncodingChars.charAt(i);
		i++;
		if(cRepetitionSeparator == cFieldSeparator)
		{
			cRepetitionSeparator = '\0';
			String strMessage = ResourceBundleWrapper.getValue("exceptions", "MissingrepetitionSeparator", new Locale(HL7Config.createInstance().getLocale()));
			throw new InvalidFieldException(strMessage);
		}
		iReadBytes++;
		//Read escape character
		
		cEscapeCharacter = strEncodingChars.charAt(i++);
		if(cEscapeCharacter == cFieldSeparator)
		{
			cEscapeCharacter = '\0';
			
			return HL7Constants.HL7_FUNCTION_SUCCESS;
		}
		iReadBytes++;
		//Read sub component separator
		
		cSubComponentSeparator = strEncodingChars.charAt(i++);
		if(cSubComponentSeparator == cFieldSeparator)
		{
			//cEscapeCharacter = '\0';
			cSubComponentSeparator= '\0';

			String strMessage = ResourceBundleWrapper.getValue("exceptions", "MissingSubcomponentSeparator", new Locale(HL7Config.createInstance().getLocale()));
			throw new InvalidFieldException(strMessage);
		}
		iReadBytes++;

		//Read Truncation Character
		cTruncationCharacter = strEncodingChars.charAt(i++);
		if(cTruncationCharacter == cFieldSeparator )
		{
			//Since any Truncation Character is not fixed. Truncation Character used is '#".
			cTruncationCharacter='#';
		}
		iReadBytes++;
		return iReadBytes;
	}
	
}

