/**
 * This class file was automatically generated by jASN1 v1.6.0 (http://www.openmuc.org)
 */

package org.openmuc.openiec61850.internal.mms.asn1;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.openmuc.jasn1.ber.BerByteArrayOutputStream;
import org.openmuc.jasn1.ber.BerIdentifier;
import org.openmuc.jasn1.ber.BerLength;
import org.openmuc.jasn1.ber.types.BerBitString;
import org.openmuc.jasn1.ber.types.BerBoolean;
import org.openmuc.jasn1.ber.types.BerInteger;
import org.openmuc.jasn1.ber.types.BerOctetString;
import org.openmuc.jasn1.ber.types.string.BerVisibleString;

public class Data {

    public byte[] code = null;

    public static class Array {

        public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
                BerIdentifier.CONSTRUCTED, 16);
        protected BerIdentifier id;

        public byte[] code = null;
        private List<Data> seqOf = null;

        public Array() {
            id = identifier;
            seqOf = new ArrayList<>();
        }

        public Array(byte[] code) {
            id = identifier;
            this.code = code;
        }

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

        public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {
            int codeLength;

            if (code != null) {
                codeLength = code.length;
                for (int i = code.length - 1; i >= 0; i--) {
                    os.write(code[i]);
                }
            }
            else {
                codeLength = 0;
                for (int i = (seqOf.size() - 1); i >= 0; i--) {
                    codeLength += seqOf.get(i).encode(os, true);
                }

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

            }

            if (explicit) {
                codeLength += id.encode(os);
            }

            return codeLength;
        }

        public int decode(InputStream is, boolean explicit) throws IOException {
            int codeLength = 0;
            int subCodeLength = 0;
            BerIdentifier berIdentifier = new BerIdentifier();
            if (explicit) {
                codeLength += id.decodeAndCheck(is);
            }

            BerLength length = new BerLength();
            codeLength += length.decode(is);
            int totalLength = length.val;

            while (subCodeLength < totalLength) {
                Data element = new Data();
                subCodeLength += element.decode(is, null);
                seqOf.add(element);
            }
            if (subCodeLength != totalLength) {
                throw new IOException("Decoded SequenceOf or SetOf has wrong length. Expected " + totalLength
                        + " but has " + subCodeLength);

            }
            codeLength += subCodeLength;

            return codeLength;
        }

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

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("SEQUENCE OF{");

            if (seqOf == null) {
                sb.append("null");
            }
            else {
                Iterator<Data> it = seqOf.iterator();
                if (it.hasNext()) {
                    sb.append(it.next());
                    while (it.hasNext()) {
                        sb.append(", ").append(it.next());
                    }
                }
            }

            sb.append("}");

            return sb.toString();
        }

    }

    public static class Structure {

        public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
                BerIdentifier.CONSTRUCTED, 16);
        protected BerIdentifier id;

        public byte[] code = null;
        private List<Data> seqOf = null;

        public Structure() {
            id = identifier;
            seqOf = new ArrayList<>();
        }

        public Structure(byte[] code) {
            id = identifier;
            this.code = code;
        }

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

        public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {
            int codeLength;

            if (code != null) {
                codeLength = code.length;
                for (int i = code.length - 1; i >= 0; i--) {
                    os.write(code[i]);
                }
            }
            else {
                codeLength = 0;
                for (int i = (seqOf.size() - 1); i >= 0; i--) {
                    codeLength += seqOf.get(i).encode(os, true);
                }

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

            }

            if (explicit) {
                codeLength += id.encode(os);
            }

            return codeLength;
        }

        public int decode(InputStream is, boolean explicit) throws IOException {
            int codeLength = 0;
            int subCodeLength = 0;
            BerIdentifier berIdentifier = new BerIdentifier();
            if (explicit) {
                codeLength += id.decodeAndCheck(is);
            }

            BerLength length = new BerLength();
            codeLength += length.decode(is);
            int totalLength = length.val;

            while (subCodeLength < totalLength) {
                Data element = new Data();
                subCodeLength += element.decode(is, null);
                seqOf.add(element);
            }
            if (subCodeLength != totalLength) {
                throw new IOException("Decoded SequenceOf or SetOf has wrong length. Expected " + totalLength
                        + " but has " + subCodeLength);

            }
            codeLength += subCodeLength;

            return codeLength;
        }

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

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("SEQUENCE OF{");

            if (seqOf == null) {
                sb.append("null");
            }
            else {
                Iterator<Data> it = seqOf.iterator();
                if (it.hasNext()) {
                    sb.append(it.next());
                    while (it.hasNext()) {
                        sb.append(", ").append(it.next());
                    }
                }
            }

            sb.append("}");

            return sb.toString();
        }

    }

    private Array array = null;

    private Structure structure = null;

    private BerBoolean bool = null;

    private BerBitString bitString = null;

    private BerInteger integer = null;

    private BerInteger unsigned = null;

    private FloatingPoint floatingPoint = null;

    private BerOctetString octetString = null;

    private BerVisibleString visibleString = null;

    private TimeOfDay binaryTime = null;

    private MMSString mMSString = null;

    private UtcTime utcTime = null;

    public Data() {
    }

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

    public void setArray(Array array) {
        this.array = array;
    }

    public Array getArray() {
        return array;
    }

    public void setStructure(Structure structure) {
        this.structure = structure;
    }

    public Structure getStructure() {
        return structure;
    }

    public void setBool(BerBoolean bool) {
        this.bool = bool;
    }

    public BerBoolean getBool() {
        return bool;
    }

    public void setBitString(BerBitString bitString) {
        this.bitString = bitString;
    }

    public BerBitString getBitString() {
        return bitString;
    }

    public void setInteger(BerInteger integer) {
        this.integer = integer;
    }

    public BerInteger getInteger() {
        return integer;
    }

    public void setUnsigned(BerInteger unsigned) {
        this.unsigned = unsigned;
    }

    public BerInteger getUnsigned() {
        return unsigned;
    }

    public void setFloatingPoint(FloatingPoint floatingPoint) {
        this.floatingPoint = floatingPoint;
    }

    public FloatingPoint getFloatingPoint() {
        return floatingPoint;
    }

    public void setOctetString(BerOctetString octetString) {
        this.octetString = octetString;
    }

    public BerOctetString getOctetString() {
        return octetString;
    }

    public void setVisibleString(BerVisibleString visibleString) {
        this.visibleString = visibleString;
    }

    public BerVisibleString getVisibleString() {
        return visibleString;
    }

    public void setBinaryTime(TimeOfDay binaryTime) {
        this.binaryTime = binaryTime;
    }

    public TimeOfDay getBinaryTime() {
        return binaryTime;
    }

    public void setMMSString(MMSString mMSString) {
        this.mMSString = mMSString;
    }

    public MMSString getMMSString() {
        return mMSString;
    }

    public void setUtcTime(UtcTime utcTime) {
        this.utcTime = utcTime;
    }

    public UtcTime getUtcTime() {
        return utcTime;
    }

    public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {
        if (code != null) {
            for (int i = code.length - 1; i >= 0; i--) {
                os.write(code[i]);
            }
            return code.length;

        }
        int codeLength = 0;
        if (utcTime != null) {
            codeLength += utcTime.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 17
            os.write(0x91);
            codeLength += 1;
            return codeLength;

        }

        if (mMSString != null) {
            codeLength += mMSString.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 16
            os.write(0x90);
            codeLength += 1;
            return codeLength;

        }

        if (binaryTime != null) {
            codeLength += binaryTime.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 12
            os.write(0x8c);
            codeLength += 1;
            return codeLength;

        }

        if (visibleString != null) {
            codeLength += visibleString.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 10
            os.write(0x8a);
            codeLength += 1;
            return codeLength;

        }

        if (octetString != null) {
            codeLength += octetString.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 9
            os.write(0x89);
            codeLength += 1;
            return codeLength;

        }

        if (floatingPoint != null) {
            codeLength += floatingPoint.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 7
            os.write(0x87);
            codeLength += 1;
            return codeLength;

        }

        if (unsigned != null) {
            codeLength += unsigned.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 6
            os.write(0x86);
            codeLength += 1;
            return codeLength;

        }

        if (integer != null) {
            codeLength += integer.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 5
            os.write(0x85);
            codeLength += 1;
            return codeLength;

        }

        if (bitString != null) {
            codeLength += bitString.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 4
            os.write(0x84);
            codeLength += 1;
            return codeLength;

        }

        if (bool != null) {
            codeLength += bool.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 3
            os.write(0x83);
            codeLength += 1;
            return codeLength;

        }

        if (structure != null) {
            codeLength += structure.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 2
            os.write(0xa2);
            codeLength += 1;
            return codeLength;

        }

        if (array != null) {
            codeLength += array.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 1
            os.write(0xa1);
            codeLength += 1;
            return codeLength;

        }

        throw new IOException("Error encoding BerChoice: No item in choice was selected.");
    }

    public int decode(InputStream is, BerIdentifier berIdentifier) throws IOException {
        int codeLength = 0;
        BerIdentifier passedIdentifier = berIdentifier;

        if (berIdentifier == null) {
            berIdentifier = new BerIdentifier();
            codeLength += berIdentifier.decode(is);
        }

        BerLength length = new BerLength();
        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 1)) {
            array = new Array();
            codeLength += array.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
            structure = new Structure();
            codeLength += structure.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 3)) {
            bool = new BerBoolean();
            codeLength += bool.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 4)) {
            bitString = new BerBitString();
            codeLength += bitString.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 5)) {
            integer = new BerInteger();
            codeLength += integer.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 6)) {
            unsigned = new BerInteger();
            codeLength += unsigned.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 7)) {
            floatingPoint = new FloatingPoint();
            codeLength += floatingPoint.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 9)) {
            octetString = new BerOctetString();
            codeLength += octetString.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 10)) {
            visibleString = new BerVisibleString();
            codeLength += visibleString.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 12)) {
            binaryTime = new TimeOfDay();
            codeLength += binaryTime.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 16)) {
            mMSString = new MMSString();
            codeLength += mMSString.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 17)) {
            utcTime = new UtcTime();
            codeLength += utcTime.decode(is, false);
            return codeLength;
        }

        if (passedIdentifier != null) {
            return 0;
        }
        throw new IOException("Error decoding BerChoice: Identifier matched to no item.");
    }

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

    @Override
    public String toString() {
        if (array != null) {
            return "CHOICE{array: " + array + "}";
        }

        if (structure != null) {
            return "CHOICE{structure: " + structure + "}";
        }

        if (bool != null) {
            return "CHOICE{bool: " + bool + "}";
        }

        if (bitString != null) {
            return "CHOICE{bitString: " + bitString + "}";
        }

        if (integer != null) {
            return "CHOICE{integer: " + integer + "}";
        }

        if (unsigned != null) {
            return "CHOICE{unsigned: " + unsigned + "}";
        }

        if (floatingPoint != null) {
            return "CHOICE{floatingPoint: " + floatingPoint + "}";
        }

        if (octetString != null) {
            return "CHOICE{octetString: " + octetString + "}";
        }

        if (visibleString != null) {
            return "CHOICE{visibleString: " + visibleString + "}";
        }

        if (binaryTime != null) {
            return "CHOICE{binaryTime: " + binaryTime + "}";
        }

        if (mMSString != null) {
            return "CHOICE{mMSString: " + mMSString + "}";
        }

        if (utcTime != null) {
            return "CHOICE{utcTime: " + utcTime + "}";
        }

        return "unknown";
    }

}
