/*
 * This class file was automatically generated by ASN1bean v1.12.0 (http://www.beanit.com)
 */

package com.beanit.asn1dsrc.dsrc;

import java.io.IOException;
import java.io.EOFException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.io.Serializable;
import com.beanit.asn1bean.ber.*;
import com.beanit.asn1bean.ber.types.*;
import com.beanit.asn1bean.ber.types.string.*;


public class Login implements BerType, Serializable {

	private static final long serialVersionUID = 1L;

	public static class DatexLoginEncodingRulesId implements BerType, Serializable {

		private static final long serialVersionUID = 1L;

		public static final BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.CONSTRUCTED, 16);
		private byte[] code = null;
		private List<BerObjectIdentifier> seqOf = null;

		public DatexLoginEncodingRulesId() {
			seqOf = new ArrayList<BerObjectIdentifier>();
		}

		public DatexLoginEncodingRulesId(byte[] code) {
			this.code = code;
		}

		public List<BerObjectIdentifier> getBerObjectIdentifier() {
			if (seqOf == null) {
				seqOf = new ArrayList<BerObjectIdentifier>();
			}
			return seqOf;
		}

		@Override public int encode(OutputStream reverseOS) throws IOException {
			return encode(reverseOS, true);
		}

		public int encode(OutputStream reverseOS, boolean withTag) throws IOException {

			if (code != null) {
				reverseOS.write(code);
				if (withTag) {
					return tag.encode(reverseOS) + code.length;
				}
				return code.length;
			}

			int codeLength = 0;
			for (int i = (seqOf.size() - 1); i >= 0; i--) {
				codeLength += seqOf.get(i).encode(reverseOS, true);
			}

			codeLength += BerLength.encodeLength(reverseOS, codeLength);

			if (withTag) {
				codeLength += tag.encode(reverseOS);
			}

			return codeLength;
		}

		@Override public int decode(InputStream is) throws IOException {
			return decode(is, true);
		}

		public int decode(InputStream is, boolean withTag) throws IOException {
			int tlByteCount = 0;
			int vByteCount = 0;
			BerTag berTag = new BerTag();
			if (withTag) {
				tlByteCount += tag.decodeAndCheck(is);
			}

			BerLength length = new BerLength();
			tlByteCount += length.decode(is);
			int lengthVal = length.val;

			while (vByteCount < lengthVal || lengthVal < 0) {
				vByteCount += berTag.decode(is);

				if (lengthVal < 0 && berTag.equals(0, 0, 0)) {
					vByteCount += BerLength.readEocByte(is);
					break;
				}

				if (!berTag.equals(BerObjectIdentifier.tag)) {
					throw new IOException("Tag does not match mandatory sequence of/set of component.");
				}
				BerObjectIdentifier element = new BerObjectIdentifier();
				vByteCount += element.decode(is, false);
				seqOf.add(element);
			}
			if (lengthVal >= 0 && vByteCount != lengthVal) {
				throw new IOException("Decoded SequenceOf or SetOf has wrong length. Expected " + lengthVal + " but has " + vByteCount);

			}
			return tlByteCount + vByteCount;
		}

		public void encodeAndSave(int encodingSizeGuess) throws IOException {
			ReverseByteArrayOutputStream reverseOS = new ReverseByteArrayOutputStream(encodingSizeGuess);
			encode(reverseOS, false);
			code = reverseOS.getArray();
		}

		@Override public String toString() {
			StringBuilder sb = new StringBuilder();
			appendAsString(sb, 0);
			return sb.toString();
		}

		public void appendAsString(StringBuilder sb, int indentLevel) {

			sb.append("{\n");
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			if (seqOf == null) {
				sb.append("null");
			}
			else {
				Iterator<BerObjectIdentifier> it = seqOf.iterator();
				if (it.hasNext()) {
					sb.append(it.next());
					while (it.hasNext()) {
						sb.append(",\n");
						for (int i = 0; i < indentLevel + 1; i++) {
							sb.append("\t");
						}
						sb.append(it.next());
					}
				}
			}

			sb.append("\n");
			for (int i = 0; i < indentLevel; i++) {
				sb.append("\t");
			}
			sb.append("}");
		}

	}

	public static final BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.CONSTRUCTED, 16);

	private byte[] code = null;
	private BerUTF8String datexSenderTxt = null;
	private BerUTF8String datexDestinationTxt = null;
	private BerOctetString datexLoginUserNameTxt = null;
	private BerOctetString datexLoginPasswordTxt = null;
	private DatexLoginEncodingRulesId datexLoginEncodingRulesId = null;
	private BerInteger datexLoginHeartbeatDurationMaxQty = null;
	private BerInteger datexLoginResponseTimeOutQty = null;
	private BerEnum datexLoginInitiatorCd = null;
	private BerInteger datexLoginDatagramSizeQty = null;
	
	public Login() {
	}

	public Login(byte[] code) {
		this.code = code;
	}

	public void setDatexSenderTxt(BerUTF8String datexSenderTxt) {
		this.datexSenderTxt = datexSenderTxt;
	}

	public BerUTF8String getDatexSenderTxt() {
		return datexSenderTxt;
	}

	public void setDatexDestinationTxt(BerUTF8String datexDestinationTxt) {
		this.datexDestinationTxt = datexDestinationTxt;
	}

	public BerUTF8String getDatexDestinationTxt() {
		return datexDestinationTxt;
	}

	public void setDatexLoginUserNameTxt(BerOctetString datexLoginUserNameTxt) {
		this.datexLoginUserNameTxt = datexLoginUserNameTxt;
	}

	public BerOctetString getDatexLoginUserNameTxt() {
		return datexLoginUserNameTxt;
	}

	public void setDatexLoginPasswordTxt(BerOctetString datexLoginPasswordTxt) {
		this.datexLoginPasswordTxt = datexLoginPasswordTxt;
	}

	public BerOctetString getDatexLoginPasswordTxt() {
		return datexLoginPasswordTxt;
	}

	public void setDatexLoginEncodingRulesId(DatexLoginEncodingRulesId datexLoginEncodingRulesId) {
		this.datexLoginEncodingRulesId = datexLoginEncodingRulesId;
	}

	public DatexLoginEncodingRulesId getDatexLoginEncodingRulesId() {
		return datexLoginEncodingRulesId;
	}

	public void setDatexLoginHeartbeatDurationMaxQty(BerInteger datexLoginHeartbeatDurationMaxQty) {
		this.datexLoginHeartbeatDurationMaxQty = datexLoginHeartbeatDurationMaxQty;
	}

	public BerInteger getDatexLoginHeartbeatDurationMaxQty() {
		return datexLoginHeartbeatDurationMaxQty;
	}

	public void setDatexLoginResponseTimeOutQty(BerInteger datexLoginResponseTimeOutQty) {
		this.datexLoginResponseTimeOutQty = datexLoginResponseTimeOutQty;
	}

	public BerInteger getDatexLoginResponseTimeOutQty() {
		return datexLoginResponseTimeOutQty;
	}

	public void setDatexLoginInitiatorCd(BerEnum datexLoginInitiatorCd) {
		this.datexLoginInitiatorCd = datexLoginInitiatorCd;
	}

	public BerEnum getDatexLoginInitiatorCd() {
		return datexLoginInitiatorCd;
	}

	public void setDatexLoginDatagramSizeQty(BerInteger datexLoginDatagramSizeQty) {
		this.datexLoginDatagramSizeQty = datexLoginDatagramSizeQty;
	}

	public BerInteger getDatexLoginDatagramSizeQty() {
		return datexLoginDatagramSizeQty;
	}

	@Override public int encode(OutputStream reverseOS) throws IOException {
		return encode(reverseOS, true);
	}

	public int encode(OutputStream reverseOS, boolean withTag) throws IOException {

		if (code != null) {
			reverseOS.write(code);
			if (withTag) {
				return tag.encode(reverseOS) + code.length;
			}
			return code.length;
		}

		int codeLength = 0;
		codeLength += datexLoginDatagramSizeQty.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, PRIMITIVE, 8
		reverseOS.write(0x88);
		codeLength += 1;
		
		codeLength += datexLoginInitiatorCd.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, PRIMITIVE, 7
		reverseOS.write(0x87);
		codeLength += 1;
		
		codeLength += datexLoginResponseTimeOutQty.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, PRIMITIVE, 6
		reverseOS.write(0x86);
		codeLength += 1;
		
		codeLength += datexLoginHeartbeatDurationMaxQty.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, PRIMITIVE, 5
		reverseOS.write(0x85);
		codeLength += 1;
		
		codeLength += datexLoginEncodingRulesId.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, CONSTRUCTED, 4
		reverseOS.write(0xA4);
		codeLength += 1;
		
		codeLength += datexLoginPasswordTxt.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, PRIMITIVE, 3
		reverseOS.write(0x83);
		codeLength += 1;
		
		codeLength += datexLoginUserNameTxt.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, PRIMITIVE, 2
		reverseOS.write(0x82);
		codeLength += 1;
		
		codeLength += datexDestinationTxt.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, PRIMITIVE, 1
		reverseOS.write(0x81);
		codeLength += 1;
		
		codeLength += datexSenderTxt.encode(reverseOS, false);
		// write tag: CONTEXT_CLASS, PRIMITIVE, 0
		reverseOS.write(0x80);
		codeLength += 1;
		
		codeLength += BerLength.encodeLength(reverseOS, codeLength);

		if (withTag) {
			codeLength += tag.encode(reverseOS);
		}

		return codeLength;

	}

	@Override public int decode(InputStream is) throws IOException {
		return decode(is, true);
	}

	public int decode(InputStream is, boolean withTag) throws IOException {
		int tlByteCount = 0;
		int vByteCount = 0;
		BerTag berTag = new BerTag();

		if (withTag) {
			tlByteCount += tag.decodeAndCheck(is);
		}

		BerLength length = new BerLength();
		tlByteCount += length.decode(is);
		int lengthVal = length.val;
		vByteCount += berTag.decode(is);

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 0)) {
			datexSenderTxt = new BerUTF8String();
			vByteCount += datexSenderTxt.decode(is, false);
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
			datexDestinationTxt = new BerUTF8String();
			vByteCount += datexDestinationTxt.decode(is, false);
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) {
			datexLoginUserNameTxt = new BerOctetString();
			vByteCount += datexLoginUserNameTxt.decode(is, false);
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 3)) {
			datexLoginPasswordTxt = new BerOctetString();
			vByteCount += datexLoginPasswordTxt.decode(is, false);
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 4)) {
			datexLoginEncodingRulesId = new DatexLoginEncodingRulesId();
			vByteCount += datexLoginEncodingRulesId.decode(is, false);
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 5)) {
			datexLoginHeartbeatDurationMaxQty = new BerInteger();
			vByteCount += datexLoginHeartbeatDurationMaxQty.decode(is, false);
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 6)) {
			datexLoginResponseTimeOutQty = new BerInteger();
			vByteCount += datexLoginResponseTimeOutQty.decode(is, false);
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 7)) {
			datexLoginInitiatorCd = new BerEnum();
			vByteCount += datexLoginInitiatorCd.decode(is, false);
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 8)) {
			datexLoginDatagramSizeQty = new BerInteger();
			vByteCount += datexLoginDatagramSizeQty.decode(is, false);
			if (lengthVal >= 0 && vByteCount == lengthVal) {
				return tlByteCount + vByteCount;
			}
			vByteCount += berTag.decode(is);
		}
		else {
			throw new IOException("Tag does not match mandatory sequence component.");
		}
		
		if (lengthVal < 0) {
			if (!berTag.equals(0, 0, 0)) {
				throw new IOException("Decoded sequence has wrong end of contents octets");
			}
			vByteCount += BerLength.readEocByte(is);
			return tlByteCount + vByteCount;
		}

		throw new IOException("Unexpected end of sequence, length tag: " + lengthVal + ", bytes decoded: " + vByteCount);

	}

	public void encodeAndSave(int encodingSizeGuess) throws IOException {
		ReverseByteArrayOutputStream reverseOS = new ReverseByteArrayOutputStream(encodingSizeGuess);
		encode(reverseOS, false);
		code = reverseOS.getArray();
	}

	@Override public String toString() {
		StringBuilder sb = new StringBuilder();
		appendAsString(sb, 0);
		return sb.toString();
	}

	public void appendAsString(StringBuilder sb, int indentLevel) {

		sb.append("{");
		sb.append("\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexSenderTxt != null) {
			sb.append("datexSenderTxt: ").append(datexSenderTxt);
		}
		else {
			sb.append("datexSenderTxt: <empty-required-field>");
		}
		
		sb.append(",\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexDestinationTxt != null) {
			sb.append("datexDestinationTxt: ").append(datexDestinationTxt);
		}
		else {
			sb.append("datexDestinationTxt: <empty-required-field>");
		}
		
		sb.append(",\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexLoginUserNameTxt != null) {
			sb.append("datexLoginUserNameTxt: ").append(datexLoginUserNameTxt);
		}
		else {
			sb.append("datexLoginUserNameTxt: <empty-required-field>");
		}
		
		sb.append(",\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexLoginPasswordTxt != null) {
			sb.append("datexLoginPasswordTxt: ").append(datexLoginPasswordTxt);
		}
		else {
			sb.append("datexLoginPasswordTxt: <empty-required-field>");
		}
		
		sb.append(",\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexLoginEncodingRulesId != null) {
			sb.append("datexLoginEncodingRulesId: ");
			datexLoginEncodingRulesId.appendAsString(sb, indentLevel + 1);
		}
		else {
			sb.append("datexLoginEncodingRulesId: <empty-required-field>");
		}
		
		sb.append(",\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexLoginHeartbeatDurationMaxQty != null) {
			sb.append("datexLoginHeartbeatDurationMaxQty: ").append(datexLoginHeartbeatDurationMaxQty);
		}
		else {
			sb.append("datexLoginHeartbeatDurationMaxQty: <empty-required-field>");
		}
		
		sb.append(",\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexLoginResponseTimeOutQty != null) {
			sb.append("datexLoginResponseTimeOutQty: ").append(datexLoginResponseTimeOutQty);
		}
		else {
			sb.append("datexLoginResponseTimeOutQty: <empty-required-field>");
		}
		
		sb.append(",\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexLoginInitiatorCd != null) {
			sb.append("datexLoginInitiatorCd: ").append(datexLoginInitiatorCd);
		}
		else {
			sb.append("datexLoginInitiatorCd: <empty-required-field>");
		}
		
		sb.append(",\n");
		for (int i = 0; i < indentLevel + 1; i++) {
			sb.append("\t");
		}
		if (datexLoginDatagramSizeQty != null) {
			sb.append("datexLoginDatagramSizeQty: ").append(datexLoginDatagramSizeQty);
		}
		else {
			sb.append("datexLoginDatagramSizeQty: <empty-required-field>");
		}
		
		sb.append("\n");
		for (int i = 0; i < indentLevel; i++) {
			sb.append("\t");
		}
		sb.append("}");
	}

}

