/**
 * 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 RLREApdu {

    public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.APPLICATION_CLASS,
            BerIdentifier.CONSTRUCTED, 3);
    protected BerIdentifier id;

    public byte[] code = null;
    public ReleaseResponseReason reason = null;

    public AssociationInformation userInformation = null;

    public RLREApdu() {
        id = identifier;
    }

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

    public RLREApdu(ReleaseResponseReason reason, AssociationInformation userInformation) {
        id = identifier;
        this.reason = reason;
        this.userInformation = userInformation;
    }

    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;
            if (userInformation != null) {
                codeLength += userInformation.encode(os, false);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 30
                os.write(0xbe);
                codeLength += 1;
            }

            if (reason != null) {
                codeLength += reason.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 0
                os.write(0x80);
                codeLength += 1;
            }

            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;

        if (totalLength == 0) {
            return codeLength;
        }
        subCodeLength += berIdentifier.decode(is);
        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 0)) {
            reason = new ReleaseResponseReason();
            subCodeLength += reason.decode(is, false);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 30)) {
            userInformation = new AssociationInformation();
            subCodeLength += userInformation.decode(is, false);
            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 (reason != null) {
            sb.append("reason: ").append(reason);
            firstSelectedElement = false;
        }

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

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

}
