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

package org.openmuc.josistack.internal.acse.asn1;

import java.io.IOException;
import java.io.InputStream;

import org.openmuc.jasn1.ber.BerByteArrayOutputStream;
import org.openmuc.jasn1.ber.BerIdentifier;
import org.openmuc.jasn1.ber.BerLength;
import org.openmuc.jasn1.ber.types.BerAny;
import org.openmuc.jasn1.ber.types.BerBitString;
import org.openmuc.jasn1.ber.types.BerInteger;
import org.openmuc.jasn1.ber.types.BerObjectIdentifier;
import org.openmuc.jasn1.ber.types.BerOctetString;

public class Myexternal2 {

    public static class Encoding {

        public byte[] code = null;
        public BerAny singleASN1Type = null;

        public BerOctetString octetAligned = null;

        public BerBitString arbitrary = null;

        public Encoding() {
        }

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

        public Encoding(BerAny singleASN1Type, BerOctetString octetAligned, BerBitString arbitrary) {
            this.singleASN1Type = singleASN1Type;
            this.octetAligned = octetAligned;
            this.arbitrary = arbitrary;
        }

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

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

            }

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

            }

            if (singleASN1Type != null) {
                sublength = singleASN1Type.encode(os);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 0
                os.write(0xa0);
                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, 0)) {
                codeLength += length.decode(is);
                singleASN1Type = new BerAny();
                codeLength += singleASN1Type.decode(is, length.val);
                return codeLength;
            }

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

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 2)) {
                arbitrary = new BerBitString();
                codeLength += arbitrary.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 (singleASN1Type != null) {
                return "CHOICE{singleASN1Type: " + singleASN1Type + "}";
            }

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

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

            return "unknown";
        }

    }

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

    public byte[] code = null;
    public BerObjectIdentifier directReference = null;

    public BerInteger indirectReference = null;

    public Encoding encoding = null;

    public Myexternal2() {
        id = identifier;
    }

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

    public Myexternal2(BerObjectIdentifier directReference, BerInteger indirectReference, Encoding encoding) {
        id = identifier;
        this.directReference = directReference;
        this.indirectReference = indirectReference;
        this.encoding = encoding;
    }

    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;
            codeLength += encoding.encode(os, true);

            if (indirectReference != null) {
                codeLength += indirectReference.encode(os, true);
            }

            if (directReference != null) {
                codeLength += directReference.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;
        codeLength += totalLength;

        subCodeLength += berIdentifier.decode(is);
        if (berIdentifier.equals(BerObjectIdentifier.identifier)) {
            directReference = new BerObjectIdentifier();
            subCodeLength += directReference.decode(is, false);
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerInteger.identifier)) {
            indirectReference = new BerInteger();
            subCodeLength += indirectReference.decode(is, false);
            subCodeLength += berIdentifier.decode(is);
        }

        encoding = new Encoding();
        subCodeLength += encoding.decode(is, berIdentifier);
        if (subCodeLength == totalLength) {
            return codeLength;
        }
        throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: "
                + subCodeLength);

    }

    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{");
        boolean firstSelectedElement = true;
        if (directReference != null) {
            sb.append("directReference: ").append(directReference);
            firstSelectedElement = false;
        }

        if (indirectReference != null) {
            if (!firstSelectedElement) {
                sb.append(", ");
            }
            sb.append("indirectReference: ").append(indirectReference);
            firstSelectedElement = false;
        }

        if (!firstSelectedElement) {
            sb.append(", ");
        }
        sb.append("encoding: ").append(encoding);

        sb.append("}");
        return sb.toString();
    }

}
