/**
 * 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;

public class ACSEApdu {

    public byte[] code = null;
    public AARQApdu aarq = null;

    public AAREApdu aare = null;

    public RLRQApdu rlrq = null;

    public RLREApdu rlre = null;

    public ACSEApdu() {
    }

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

    public ACSEApdu(AARQApdu aarq, AAREApdu aare, RLRQApdu rlrq, RLREApdu rlre) {
        this.aarq = aarq;
        this.aare = aare;
        this.rlrq = rlrq;
        this.rlre = rlre;
    }

    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 (rlre != null) {
            codeLength += rlre.encode(os, true);
            return codeLength;

        }

        if (rlrq != null) {
            codeLength += rlrq.encode(os, true);
            return codeLength;

        }

        if (aare != null) {
            codeLength += aare.encode(os, true);
            return codeLength;

        }

        if (aarq != null) {
            codeLength += aarq.encode(os, true);
            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(AARQApdu.identifier)) {
            aarq = new AARQApdu();
            codeLength += aarq.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(AAREApdu.identifier)) {
            aare = new AAREApdu();
            codeLength += aare.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(RLRQApdu.identifier)) {
            rlrq = new RLRQApdu();
            codeLength += rlrq.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(RLREApdu.identifier)) {
            rlre = new RLREApdu();
            codeLength += rlre.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 (aarq != null) {
            return "CHOICE{aarq: " + aarq + "}";
        }

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

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

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

        return "unknown";
    }

}
