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

	private static final long serialVersionUID = 1L;

	private byte[] code = null;
	public static class Continuous 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 BerInteger datexRegisteredUpdateDelayQty = null;
		private Time datexRegisteredStartTime = null;
		private Time datexRegisteredEndTime = null;
		
		public Continuous() {
		}

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

		public void setDatexRegisteredUpdateDelayQty(BerInteger datexRegisteredUpdateDelayQty) {
			this.datexRegisteredUpdateDelayQty = datexRegisteredUpdateDelayQty;
		}

		public BerInteger getDatexRegisteredUpdateDelayQty() {
			return datexRegisteredUpdateDelayQty;
		}

		public void setDatexRegisteredStartTime(Time datexRegisteredStartTime) {
			this.datexRegisteredStartTime = datexRegisteredStartTime;
		}

		public Time getDatexRegisteredStartTime() {
			return datexRegisteredStartTime;
		}

		public void setDatexRegisteredEndTime(Time datexRegisteredEndTime) {
			this.datexRegisteredEndTime = datexRegisteredEndTime;
		}

		public Time getDatexRegisteredEndTime() {
			return datexRegisteredEndTime;
		}

		@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;
			if (datexRegisteredEndTime != null) {
				codeLength += datexRegisteredEndTime.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, CONSTRUCTED, 2
				reverseOS.write(0xA2);
				codeLength += 1;
			}
			
			if (datexRegisteredStartTime != null) {
				codeLength += datexRegisteredStartTime.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, CONSTRUCTED, 1
				reverseOS.write(0xA1);
				codeLength += 1;
			}
			
			if (datexRegisteredUpdateDelayQty != null) {
				codeLength += datexRegisteredUpdateDelayQty.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;
			if (lengthVal == 0) {
				return tlByteCount;
			}
			vByteCount += berTag.decode(is);

			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 0)) {
				datexRegisteredUpdateDelayQty = new BerInteger();
				vByteCount += datexRegisteredUpdateDelayQty.decode(is, false);
				if (lengthVal >= 0 && vByteCount == lengthVal) {
					return tlByteCount + vByteCount;
				}
				vByteCount += berTag.decode(is);
			}
			
			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 1)) {
				datexRegisteredStartTime = new Time();
				vByteCount += datexRegisteredStartTime.decode(is, false);
				if (lengthVal >= 0 && vByteCount == lengthVal) {
					return tlByteCount + vByteCount;
				}
				vByteCount += berTag.decode(is);
			}
			
			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 2)) {
				datexRegisteredEndTime = new Time();
				vByteCount += datexRegisteredEndTime.decode(is, false);
				if (lengthVal >= 0 && vByteCount == lengthVal) {
					return tlByteCount + vByteCount;
				}
				vByteCount += berTag.decode(is);
			}
			
			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("{");
			boolean firstSelectedElement = true;
			if (datexRegisteredUpdateDelayQty != null) {
				sb.append("\n");
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("datexRegisteredUpdateDelayQty: ").append(datexRegisteredUpdateDelayQty);
				firstSelectedElement = false;
			}
			
			if (datexRegisteredStartTime != null) {
				if (!firstSelectedElement) {
					sb.append(",\n");
				}
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("datexRegisteredStartTime: ");
				datexRegisteredStartTime.appendAsString(sb, indentLevel + 1);
				firstSelectedElement = false;
			}
			
			if (datexRegisteredEndTime != null) {
				if (!firstSelectedElement) {
					sb.append(",\n");
				}
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("datexRegisteredEndTime: ");
				datexRegisteredEndTime.appendAsString(sb, indentLevel + 1);
				firstSelectedElement = false;
			}
			
			sb.append("\n");
			for (int i = 0; i < indentLevel; i++) {
				sb.append("\t");
			}
			sb.append("}");
		}

	}

	public static class Daily 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 BerInteger datexRegisteredUpdateDelayQty = null;
		private BerBitString datexRegisteredDaysOfWeekCd = null;
		private Time datexRegisteredStartDate = null;
		private Time datexRegisteredEndDate = null;
		private Time datexRegisteredStartTime = null;
		private BerInteger datexRegisteredDurationQty = null;
		
		public Daily() {
		}

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

		public void setDatexRegisteredUpdateDelayQty(BerInteger datexRegisteredUpdateDelayQty) {
			this.datexRegisteredUpdateDelayQty = datexRegisteredUpdateDelayQty;
		}

		public BerInteger getDatexRegisteredUpdateDelayQty() {
			return datexRegisteredUpdateDelayQty;
		}

		public void setDatexRegisteredDaysOfWeekCd(BerBitString datexRegisteredDaysOfWeekCd) {
			this.datexRegisteredDaysOfWeekCd = datexRegisteredDaysOfWeekCd;
		}

		public BerBitString getDatexRegisteredDaysOfWeekCd() {
			return datexRegisteredDaysOfWeekCd;
		}

		public void setDatexRegisteredStartDate(Time datexRegisteredStartDate) {
			this.datexRegisteredStartDate = datexRegisteredStartDate;
		}

		public Time getDatexRegisteredStartDate() {
			return datexRegisteredStartDate;
		}

		public void setDatexRegisteredEndDate(Time datexRegisteredEndDate) {
			this.datexRegisteredEndDate = datexRegisteredEndDate;
		}

		public Time getDatexRegisteredEndDate() {
			return datexRegisteredEndDate;
		}

		public void setDatexRegisteredStartTime(Time datexRegisteredStartTime) {
			this.datexRegisteredStartTime = datexRegisteredStartTime;
		}

		public Time getDatexRegisteredStartTime() {
			return datexRegisteredStartTime;
		}

		public void setDatexRegisteredDurationQty(BerInteger datexRegisteredDurationQty) {
			this.datexRegisteredDurationQty = datexRegisteredDurationQty;
		}

		public BerInteger getDatexRegisteredDurationQty() {
			return datexRegisteredDurationQty;
		}

		@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;
			if (datexRegisteredDurationQty != null) {
				codeLength += datexRegisteredDurationQty.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, PRIMITIVE, 5
				reverseOS.write(0x85);
				codeLength += 1;
			}
			
			if (datexRegisteredStartTime != null) {
				codeLength += datexRegisteredStartTime.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, CONSTRUCTED, 4
				reverseOS.write(0xA4);
				codeLength += 1;
			}
			
			if (datexRegisteredEndDate != null) {
				codeLength += datexRegisteredEndDate.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, CONSTRUCTED, 3
				reverseOS.write(0xA3);
				codeLength += 1;
			}
			
			if (datexRegisteredStartDate != null) {
				codeLength += datexRegisteredStartDate.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, CONSTRUCTED, 2
				reverseOS.write(0xA2);
				codeLength += 1;
			}
			
			codeLength += datexRegisteredDaysOfWeekCd.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 1
			reverseOS.write(0x81);
			codeLength += 1;
			
			if (datexRegisteredUpdateDelayQty != null) {
				codeLength += datexRegisteredUpdateDelayQty.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)) {
				datexRegisteredUpdateDelayQty = new BerInteger();
				vByteCount += datexRegisteredUpdateDelayQty.decode(is, false);
				vByteCount += berTag.decode(is);
			}
			
			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
				datexRegisteredDaysOfWeekCd = new BerBitString();
				vByteCount += datexRegisteredDaysOfWeekCd.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 (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 2)) {
				datexRegisteredStartDate = new Time();
				vByteCount += datexRegisteredStartDate.decode(is, false);
				if (lengthVal >= 0 && vByteCount == lengthVal) {
					return tlByteCount + vByteCount;
				}
				vByteCount += berTag.decode(is);
			}
			
			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 3)) {
				datexRegisteredEndDate = new Time();
				vByteCount += datexRegisteredEndDate.decode(is, false);
				if (lengthVal >= 0 && vByteCount == lengthVal) {
					return tlByteCount + vByteCount;
				}
				vByteCount += berTag.decode(is);
			}
			
			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 4)) {
				datexRegisteredStartTime = new Time();
				vByteCount += datexRegisteredStartTime.decode(is, false);
				if (lengthVal >= 0 && vByteCount == lengthVal) {
					return tlByteCount + vByteCount;
				}
				vByteCount += berTag.decode(is);
			}
			
			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 5)) {
				datexRegisteredDurationQty = new BerInteger();
				vByteCount += datexRegisteredDurationQty.decode(is, false);
				if (lengthVal >= 0 && vByteCount == lengthVal) {
					return tlByteCount + vByteCount;
				}
				vByteCount += berTag.decode(is);
			}
			
			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("{");
			boolean firstSelectedElement = true;
			if (datexRegisteredUpdateDelayQty != null) {
				sb.append("\n");
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("datexRegisteredUpdateDelayQty: ").append(datexRegisteredUpdateDelayQty);
				firstSelectedElement = false;
			}
			
			if (!firstSelectedElement) {
				sb.append(",\n");
			}
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			if (datexRegisteredDaysOfWeekCd != null) {
				sb.append("datexRegisteredDaysOfWeekCd: ").append(datexRegisteredDaysOfWeekCd);
			}
			else {
				sb.append("datexRegisteredDaysOfWeekCd: <empty-required-field>");
			}
			
			if (datexRegisteredStartDate != null) {
				sb.append(",\n");
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("datexRegisteredStartDate: ");
				datexRegisteredStartDate.appendAsString(sb, indentLevel + 1);
			}
			
			if (datexRegisteredEndDate != null) {
				sb.append(",\n");
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("datexRegisteredEndDate: ");
				datexRegisteredEndDate.appendAsString(sb, indentLevel + 1);
			}
			
			if (datexRegisteredStartTime != null) {
				sb.append(",\n");
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("datexRegisteredStartTime: ");
				datexRegisteredStartTime.appendAsString(sb, indentLevel + 1);
			}
			
			if (datexRegisteredDurationQty != null) {
				sb.append(",\n");
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("datexRegisteredDurationQty: ").append(datexRegisteredDurationQty);
			}
			
			sb.append("\n");
			for (int i = 0; i < indentLevel; i++) {
				sb.append("\t");
			}
			sb.append("}");
		}

	}

	private Continuous continuous = null;
	private Daily daily = null;
	
	public Registered() {
	}

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

	public void setContinuous(Continuous continuous) {
		this.continuous = continuous;
	}

	public Continuous getContinuous() {
		return continuous;
	}

	public void setDaily(Daily daily) {
		this.daily = daily;
	}

	public Daily getDaily() {
		return daily;
	}

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

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

		int codeLength = 0;
		if (daily != null) {
			codeLength += daily.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 1
			reverseOS.write(0xA1);
			codeLength += 1;
			return codeLength;
		}
		
		if (continuous != null) {
			codeLength += continuous.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)) {
			continuous = new Continuous();
			tlvByteCount += continuous.decode(is, false);
			return tlvByteCount;
		}

		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 1)) {
			daily = new Daily();
			tlvByteCount += daily.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 (continuous != null) {
			sb.append("continuous: ");
			continuous.appendAsString(sb, indentLevel + 1);
			return;
		}

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

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

}

