/*
 * 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 PDUs implements BerType, Serializable {

	private static final long serialVersionUID = 1L;

	private byte[] code = null;
	private Initiate datexInitiateNull = null;
	private Login login = null;
	private FrED fred = null;
	private Terminate terminate = null;
	private Logout logout = null;
	private Subscription subscription = null;
	private Publication publication = null;
	private TransferDone transferDone = null;
	private Accept accept = null;
	private Reject reject = null;
	
	public PDUs() {
	}

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

	public void setDatexInitiateNull(Initiate datexInitiateNull) {
		this.datexInitiateNull = datexInitiateNull;
	}

	public Initiate getDatexInitiateNull() {
		return datexInitiateNull;
	}

	public void setLogin(Login login) {
		this.login = login;
	}

	public Login getLogin() {
		return login;
	}

	public void setFred(FrED fred) {
		this.fred = fred;
	}

	public FrED getFred() {
		return fred;
	}

	public void setTerminate(Terminate terminate) {
		this.terminate = terminate;
	}

	public Terminate getTerminate() {
		return terminate;
	}

	public void setLogout(Logout logout) {
		this.logout = logout;
	}

	public Logout getLogout() {
		return logout;
	}

	public void setSubscription(Subscription subscription) {
		this.subscription = subscription;
	}

	public Subscription getSubscription() {
		return subscription;
	}

	public void setPublication(Publication publication) {
		this.publication = publication;
	}

	public Publication getPublication() {
		return publication;
	}

	public void setTransferDone(TransferDone transferDone) {
		this.transferDone = transferDone;
	}

	public TransferDone getTransferDone() {
		return transferDone;
	}

	public void setAccept(Accept accept) {
		this.accept = accept;
	}

	public Accept getAccept() {
		return accept;
	}

	public void setReject(Reject reject) {
		this.reject = reject;
	}

	public Reject getReject() {
		return reject;
	}

	@Override public int encode(OutputStream reverseOS) throws IOException {

		if (code != null) {
			reverseOS.write(code);
			return code.length;
		}

		int codeLength = 0;
		if (reject != null) {
			codeLength += reject.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 9
			reverseOS.write(0xA9);
			codeLength += 1;
			return codeLength;
		}
		
		if (accept != null) {
			codeLength += accept.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 8
			reverseOS.write(0xA8);
			codeLength += 1;
			return codeLength;
		}
		
		if (transferDone != null) {
			codeLength += transferDone.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 7
			reverseOS.write(0xA7);
			codeLength += 1;
			return codeLength;
		}
		
		if (publication != null) {
			codeLength += publication.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 6
			reverseOS.write(0xA6);
			codeLength += 1;
			return codeLength;
		}
		
		if (subscription != null) {
			codeLength += subscription.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 5
			reverseOS.write(0xA5);
			codeLength += 1;
			return codeLength;
		}
		
		if (logout != null) {
			codeLength += logout.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 4
			reverseOS.write(0x84);
			codeLength += 1;
			return codeLength;
		}
		
		if (terminate != null) {
			codeLength += terminate.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 3
			reverseOS.write(0x83);
			codeLength += 1;
			return codeLength;
		}
		
		if (fred != null) {
			codeLength += fred.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 2
			reverseOS.write(0x82);
			codeLength += 1;
			return codeLength;
		}
		
		if (login != null) {
			codeLength += login.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 1
			reverseOS.write(0xA1);
			codeLength += 1;
			return codeLength;
		}
		
		if (datexInitiateNull != null) {
			codeLength += datexInitiateNull.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 0
			reverseOS.write(0xA0);
			codeLength += 1;
			return codeLength;
		}
		
		throw new IOException("Error encoding CHOICE: No element of CHOICE was selected.");
	}

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

	public int decode(InputStream is, BerTag berTag) throws IOException {

		int tlvByteCount = 0;
		boolean tagWasPassed = (berTag != null);

		if (berTag == null) {
			berTag = new BerTag();
			tlvByteCount += berTag.decode(is);
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 0)) {
			datexInitiateNull = new Initiate();
			tlvByteCount += datexInitiateNull.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 1)) {
			login = new Login();
			tlvByteCount += login.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) {
			fred = new FrED();
			tlvByteCount += fred.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 3)) {
			terminate = new Terminate();
			tlvByteCount += terminate.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 4)) {
			logout = new Logout();
			tlvByteCount += logout.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 5)) {
			subscription = new Subscription();
			tlvByteCount += subscription.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 6)) {
			publication = new Publication();
			tlvByteCount += publication.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 7)) {
			transferDone = new TransferDone();
			tlvByteCount += transferDone.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 8)) {
			accept = new Accept();
			tlvByteCount += accept.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 9)) {
			reject = new Reject();
			tlvByteCount += reject.decode(is, false);
			return tlvByteCount;
		}

		if (tagWasPassed) {
			return 0;
		}

		throw new IOException("Error decoding CHOICE: Tag " + berTag + " matched to no item.");
	}

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

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

	public void appendAsString(StringBuilder sb, int indentLevel) {

		if (datexInitiateNull != null) {
			sb.append("datexInitiateNull: ");
			datexInitiateNull.appendAsString(sb, indentLevel + 1);
			return;
		}

		if (login != null) {
			sb.append("login: ");
			login.appendAsString(sb, indentLevel + 1);
			return;
		}

		if (fred != null) {
			sb.append("fred: ").append(fred);
			return;
		}

		if (terminate != null) {
			sb.append("terminate: ").append(terminate);
			return;
		}

		if (logout != null) {
			sb.append("logout: ").append(logout);
			return;
		}

		if (subscription != null) {
			sb.append("subscription: ");
			subscription.appendAsString(sb, indentLevel + 1);
			return;
		}

		if (publication != null) {
			sb.append("publication: ");
			publication.appendAsString(sb, indentLevel + 1);
			return;
		}

		if (transferDone != null) {
			sb.append("transferDone: ");
			transferDone.appendAsString(sb, indentLevel + 1);
			return;
		}

		if (accept != null) {
			sb.append("accept: ");
			accept.appendAsString(sb, indentLevel + 1);
			return;
		}

		if (reject != null) {
			sb.append("reject: ");
			reject.appendAsString(sb, indentLevel + 1);
			return;
		}

		sb.append("<none>");
	}

}

