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

	private static final long serialVersionUID = 1L;

	public static class TimeSecondFractions implements BerType, Serializable {

		private static final long serialVersionUID = 1L;

		private byte[] code = null;
		private BerInteger deciSeconds = null;
		private BerInteger centiSeconds = null;
		private BerInteger milliseconds = null;
		
		public TimeSecondFractions() {
		}

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

		public void setDeciSeconds(BerInteger deciSeconds) {
			this.deciSeconds = deciSeconds;
		}

		public BerInteger getDeciSeconds() {
			return deciSeconds;
		}

		public void setCentiSeconds(BerInteger centiSeconds) {
			this.centiSeconds = centiSeconds;
		}

		public BerInteger getCentiSeconds() {
			return centiSeconds;
		}

		public void setMilliseconds(BerInteger milliseconds) {
			this.milliseconds = milliseconds;
		}

		public BerInteger getMilliseconds() {
			return milliseconds;
		}

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

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

			int codeLength = 0;
			if (milliseconds != null) {
				codeLength += milliseconds.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, PRIMITIVE, 2
				reverseOS.write(0x82);
				codeLength += 1;
				return codeLength;
			}
			
			if (centiSeconds != null) {
				codeLength += centiSeconds.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, PRIMITIVE, 1
				reverseOS.write(0x81);
				codeLength += 1;
				return codeLength;
			}
			
			if (deciSeconds != null) {
				codeLength += deciSeconds.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, PRIMITIVE, 0
				reverseOS.write(0x80);
				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.PRIMITIVE, 0)) {
				deciSeconds = new BerInteger();
				tlvByteCount += deciSeconds.decode(is, false);
				return tlvByteCount;
			}

			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
				centiSeconds = new BerInteger();
				tlvByteCount += centiSeconds.decode(is, false);
				return tlvByteCount;
			}

			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) {
				milliseconds = new BerInteger();
				tlvByteCount += milliseconds.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 (deciSeconds != null) {
				sb.append("deciSeconds: ").append(deciSeconds);
				return;
			}

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

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

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

	}

	public static class TimeTimezone 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 timezoneHourQty = null;
		private BerInteger timeMinuteQty = null;
		
		public TimeTimezone() {
		}

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

		public void setTimezoneHourQty(BerInteger timezoneHourQty) {
			this.timezoneHourQty = timezoneHourQty;
		}

		public BerInteger getTimezoneHourQty() {
			return timezoneHourQty;
		}

		public void setTimeMinuteQty(BerInteger timeMinuteQty) {
			this.timeMinuteQty = timeMinuteQty;
		}

		public BerInteger getTimeMinuteQty() {
			return timeMinuteQty;
		}

		@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 (timeMinuteQty != null) {
				codeLength += timeMinuteQty.encode(reverseOS, false);
				// write tag: CONTEXT_CLASS, PRIMITIVE, 1
				reverseOS.write(0x81);
				codeLength += 1;
			}
			
			if (timezoneHourQty != null) {
				codeLength += timezoneHourQty.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)) {
				timezoneHourQty = new BerInteger();
				vByteCount += timezoneHourQty.decode(is, false);
				if (lengthVal >= 0 && vByteCount == lengthVal) {
					return tlByteCount + vByteCount;
				}
				vByteCount += berTag.decode(is);
			}
			
			if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
				timeMinuteQty = new BerInteger();
				vByteCount += timeMinuteQty.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 (timezoneHourQty != null) {
				sb.append("\n");
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("timezoneHourQty: ").append(timezoneHourQty);
				firstSelectedElement = false;
			}
			
			if (timeMinuteQty != null) {
				if (!firstSelectedElement) {
					sb.append(",\n");
				}
				for (int i = 0; i < indentLevel + 1; i++) {
					sb.append("\t");
				}
				sb.append("timeMinuteQty: ").append(timeMinuteQty);
				firstSelectedElement = false;
			}
			
			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 BerInteger timeYearQty = null;
	private BerInteger timeMonthQty = null;
	private BerInteger timeDayQty = null;
	private BerInteger timeHourQty = null;
	private BerInteger timeMinuteQty = null;
	private BerInteger timeSecondQty = null;
	private TimeSecondFractions timeSecondFractions = null;
	private TimeTimezone timeTimezone = null;
	
	public Time() {
	}

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

	public void setTimeYearQty(BerInteger timeYearQty) {
		this.timeYearQty = timeYearQty;
	}

	public BerInteger getTimeYearQty() {
		return timeYearQty;
	}

	public void setTimeMonthQty(BerInteger timeMonthQty) {
		this.timeMonthQty = timeMonthQty;
	}

	public BerInteger getTimeMonthQty() {
		return timeMonthQty;
	}

	public void setTimeDayQty(BerInteger timeDayQty) {
		this.timeDayQty = timeDayQty;
	}

	public BerInteger getTimeDayQty() {
		return timeDayQty;
	}

	public void setTimeHourQty(BerInteger timeHourQty) {
		this.timeHourQty = timeHourQty;
	}

	public BerInteger getTimeHourQty() {
		return timeHourQty;
	}

	public void setTimeMinuteQty(BerInteger timeMinuteQty) {
		this.timeMinuteQty = timeMinuteQty;
	}

	public BerInteger getTimeMinuteQty() {
		return timeMinuteQty;
	}

	public void setTimeSecondQty(BerInteger timeSecondQty) {
		this.timeSecondQty = timeSecondQty;
	}

	public BerInteger getTimeSecondQty() {
		return timeSecondQty;
	}

	public void setTimeSecondFractions(TimeSecondFractions timeSecondFractions) {
		this.timeSecondFractions = timeSecondFractions;
	}

	public TimeSecondFractions getTimeSecondFractions() {
		return timeSecondFractions;
	}

	public void setTimeTimezone(TimeTimezone timeTimezone) {
		this.timeTimezone = timeTimezone;
	}

	public TimeTimezone getTimeTimezone() {
		return timeTimezone;
	}

	@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;
		int sublength;

		if (timeTimezone != null) {
			codeLength += timeTimezone.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 7
			reverseOS.write(0xA7);
			codeLength += 1;
		}
		
		if (timeSecondFractions != null) {
			sublength = timeSecondFractions.encode(reverseOS);
			codeLength += sublength;
			codeLength += BerLength.encodeLength(reverseOS, sublength);
			// write tag: CONTEXT_CLASS, CONSTRUCTED, 6
			reverseOS.write(0xA6);
			codeLength += 1;
		}
		
		if (timeSecondQty != null) {
			codeLength += timeSecondQty.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 5
			reverseOS.write(0x85);
			codeLength += 1;
		}
		
		if (timeMinuteQty != null) {
			codeLength += timeMinuteQty.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 4
			reverseOS.write(0x84);
			codeLength += 1;
		}
		
		if (timeHourQty != null) {
			codeLength += timeHourQty.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 3
			reverseOS.write(0x83);
			codeLength += 1;
		}
		
		if (timeDayQty != null) {
			codeLength += timeDayQty.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 2
			reverseOS.write(0x82);
			codeLength += 1;
		}
		
		if (timeMonthQty != null) {
			codeLength += timeMonthQty.encode(reverseOS, false);
			// write tag: CONTEXT_CLASS, PRIMITIVE, 1
			reverseOS.write(0x81);
			codeLength += 1;
		}
		
		if (timeYearQty != null) {
			codeLength += timeYearQty.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)) {
			timeYearQty = new BerInteger();
			vByteCount += timeYearQty.decode(is, false);
			if (lengthVal >= 0 && vByteCount == lengthVal) {
				return tlByteCount + vByteCount;
			}
			vByteCount += berTag.decode(is);
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 1)) {
			timeMonthQty = new BerInteger();
			vByteCount += timeMonthQty.decode(is, false);
			if (lengthVal >= 0 && vByteCount == lengthVal) {
				return tlByteCount + vByteCount;
			}
			vByteCount += berTag.decode(is);
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 2)) {
			timeDayQty = new BerInteger();
			vByteCount += timeDayQty.decode(is, false);
			if (lengthVal >= 0 && vByteCount == lengthVal) {
				return tlByteCount + vByteCount;
			}
			vByteCount += berTag.decode(is);
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 3)) {
			timeHourQty = new BerInteger();
			vByteCount += timeHourQty.decode(is, false);
			if (lengthVal >= 0 && vByteCount == lengthVal) {
				return tlByteCount + vByteCount;
			}
			vByteCount += berTag.decode(is);
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 4)) {
			timeMinuteQty = new BerInteger();
			vByteCount += timeMinuteQty.decode(is, false);
			if (lengthVal >= 0 && vByteCount == lengthVal) {
				return tlByteCount + vByteCount;
			}
			vByteCount += berTag.decode(is);
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.PRIMITIVE, 5)) {
			timeSecondQty = new BerInteger();
			vByteCount += timeSecondQty.decode(is, false);
			if (lengthVal >= 0 && vByteCount == lengthVal) {
				return tlByteCount + vByteCount;
			}
			vByteCount += berTag.decode(is);
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 6)) {
			vByteCount += length.decode(is);
			timeSecondFractions = new TimeSecondFractions();
			vByteCount += timeSecondFractions.decode(is, null);
			vByteCount += length.readEocIfIndefinite(is);
			if (lengthVal >= 0 && vByteCount == lengthVal) {
				return tlByteCount + vByteCount;
			}
			vByteCount += berTag.decode(is);
		}
		
		if (berTag.equals(BerTag.CONTEXT_CLASS, BerTag.CONSTRUCTED, 7)) {
			timeTimezone = new TimeTimezone();
			vByteCount += timeTimezone.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 (timeYearQty != null) {
			sb.append("\n");
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			sb.append("timeYearQty: ").append(timeYearQty);
			firstSelectedElement = false;
		}
		
		if (timeMonthQty != null) {
			if (!firstSelectedElement) {
				sb.append(",\n");
			}
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			sb.append("timeMonthQty: ").append(timeMonthQty);
			firstSelectedElement = false;
		}
		
		if (timeDayQty != null) {
			if (!firstSelectedElement) {
				sb.append(",\n");
			}
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			sb.append("timeDayQty: ").append(timeDayQty);
			firstSelectedElement = false;
		}
		
		if (timeHourQty != null) {
			if (!firstSelectedElement) {
				sb.append(",\n");
			}
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			sb.append("timeHourQty: ").append(timeHourQty);
			firstSelectedElement = false;
		}
		
		if (timeMinuteQty != null) {
			if (!firstSelectedElement) {
				sb.append(",\n");
			}
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			sb.append("timeMinuteQty: ").append(timeMinuteQty);
			firstSelectedElement = false;
		}
		
		if (timeSecondQty != null) {
			if (!firstSelectedElement) {
				sb.append(",\n");
			}
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			sb.append("timeSecondQty: ").append(timeSecondQty);
			firstSelectedElement = false;
		}
		
		if (timeSecondFractions != null) {
			if (!firstSelectedElement) {
				sb.append(",\n");
			}
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			sb.append("timeSecondFractions: ");
			timeSecondFractions.appendAsString(sb, indentLevel + 1);
			firstSelectedElement = false;
		}
		
		if (timeTimezone != null) {
			if (!firstSelectedElement) {
				sb.append(",\n");
			}
			for (int i = 0; i < indentLevel + 1; i++) {
				sb.append("\t");
			}
			sb.append("timeTimezone: ");
			timeTimezone.appendAsString(sb, indentLevel + 1);
			firstSelectedElement = false;
		}
		
		sb.append("\n");
		for (int i = 0; i < indentLevel; i++) {
			sb.append("\t");
		}
		sb.append("}");
	}

}

