/**
 * 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.BerBitString;
import org.openmuc.jasn1.ber.types.BerObjectIdentifier;

public class AAREApdu {

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

    public byte[] code = null;
    public BerBitString protocolVersion = null;

    public BerObjectIdentifier applicationContextName = null;

    public AssociateResult result = null;

    public AssociateSourceDiagnostic resultSourceDiagnostic = null;

    public APTitle respondingAPTitle = null;

    public AEQualifier respondingAEQualifier = null;

    public APInvocationIdentifier respondingAPInvocationIdentifier = null;

    public AEInvocationIdentifier respondingAEInvocationIdentifier = null;

    public ACSERequirements responderAcseRequirements = null;

    public MechanismName mechanismName = null;

    public AuthenticationValue respondingAuthenticationValue = null;

    public ApplicationContextNameList applicationContextNameList = null;

    public ImplementationData implementationInformation = null;

    public AssociationInformation userInformation = null;

    public AAREApdu() {
        id = identifier;
    }

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

    public AAREApdu(BerBitString protocolVersion, BerObjectIdentifier applicationContextName, AssociateResult result,
            AssociateSourceDiagnostic resultSourceDiagnostic, APTitle respondingAPTitle,
            AEQualifier respondingAEQualifier, APInvocationIdentifier respondingAPInvocationIdentifier,
            AEInvocationIdentifier respondingAEInvocationIdentifier, ACSERequirements responderAcseRequirements,
            MechanismName mechanismName, AuthenticationValue respondingAuthenticationValue,
            ApplicationContextNameList applicationContextNameList, ImplementationData implementationInformation,
            AssociationInformation userInformation) {
        id = identifier;
        this.protocolVersion = protocolVersion;
        this.applicationContextName = applicationContextName;
        this.result = result;
        this.resultSourceDiagnostic = resultSourceDiagnostic;
        this.respondingAPTitle = respondingAPTitle;
        this.respondingAEQualifier = respondingAEQualifier;
        this.respondingAPInvocationIdentifier = respondingAPInvocationIdentifier;
        this.respondingAEInvocationIdentifier = respondingAEInvocationIdentifier;
        this.responderAcseRequirements = responderAcseRequirements;
        this.mechanismName = mechanismName;
        this.respondingAuthenticationValue = respondingAuthenticationValue;
        this.applicationContextNameList = applicationContextNameList;
        this.implementationInformation = implementationInformation;
        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;
            int sublength;

            if (userInformation != null) {
                codeLength += userInformation.encode(os, false);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 30
                os.write(0xbe);
                codeLength += 1;
            }

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

            if (applicationContextNameList != null) {
                codeLength += applicationContextNameList.encode(os, false);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 11
                os.write(0xab);
                codeLength += 1;
            }

            if (respondingAuthenticationValue != null) {
                sublength = respondingAuthenticationValue.encode(os, true);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 10
                os.write(0xaa);
                codeLength += 1;
            }

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

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

            if (respondingAEInvocationIdentifier != null) {
                sublength = respondingAEInvocationIdentifier.encode(os, true);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 7
                os.write(0xa7);
                codeLength += 1;
            }

            if (respondingAPInvocationIdentifier != null) {
                sublength = respondingAPInvocationIdentifier.encode(os, true);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 6
                os.write(0xa6);
                codeLength += 1;
            }

            if (respondingAEQualifier != null) {
                sublength = respondingAEQualifier.encode(os, true);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 5
                os.write(0xa5);
                codeLength += 1;
            }

            if (respondingAPTitle != null) {
                sublength = respondingAPTitle.encode(os, true);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 4
                os.write(0xa4);
                codeLength += 1;
            }

            sublength = resultSourceDiagnostic.encode(os, true);
            codeLength += sublength;
            codeLength += BerLength.encodeLength(os, sublength);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 3
            os.write(0xa3);
            codeLength += 1;

            sublength = result.encode(os, true);
            codeLength += sublength;
            codeLength += BerLength.encodeLength(os, sublength);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 2
            os.write(0xa2);
            codeLength += 1;

            sublength = applicationContextName.encode(os, true);
            codeLength += sublength;
            codeLength += BerLength.encodeLength(os, sublength);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 1
            os.write(0xa1);
            codeLength += 1;

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

        subCodeLength += berIdentifier.decode(is);
        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 0)) {
            protocolVersion = new BerBitString();
            subCodeLength += protocolVersion.decode(is, false);
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 1)) {
            subCodeLength += length.decode(is);
            applicationContextName = new BerObjectIdentifier();
            subCodeLength += applicationContextName.decode(is, true);
            subCodeLength += berIdentifier.decode(is);
        }
        else {
            throw new IOException("Identifier does not match the mandatory sequence element identifer.");
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
            subCodeLength += length.decode(is);
            result = new AssociateResult();
            subCodeLength += result.decode(is, true);
            subCodeLength += berIdentifier.decode(is);
        }
        else {
            throw new IOException("Identifier does not match the mandatory sequence element identifer.");
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 3)) {
            subCodeLength += length.decode(is);
            resultSourceDiagnostic = new AssociateSourceDiagnostic();
            subCodeLength += resultSourceDiagnostic.decode(is, null);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }
        else {
            throw new IOException("Identifier does not match required sequence element identifer.");
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 4)) {
            subCodeLength += length.decode(is);
            respondingAPTitle = new APTitle();
            subCodeLength += respondingAPTitle.decode(is, null);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 5)) {
            subCodeLength += length.decode(is);
            respondingAEQualifier = new AEQualifier();
            subCodeLength += respondingAEQualifier.decode(is, null);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 6)) {
            subCodeLength += length.decode(is);
            respondingAPInvocationIdentifier = new APInvocationIdentifier();
            subCodeLength += respondingAPInvocationIdentifier.decode(is, true);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 7)) {
            subCodeLength += length.decode(is);
            respondingAEInvocationIdentifier = new AEInvocationIdentifier();
            subCodeLength += respondingAEInvocationIdentifier.decode(is, true);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 8)) {
            responderAcseRequirements = new ACSERequirements();
            subCodeLength += responderAcseRequirements.decode(is, false);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 9)) {
            mechanismName = new MechanismName();
            subCodeLength += mechanismName.decode(is, false);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 10)) {
            subCodeLength += length.decode(is);
            respondingAuthenticationValue = new AuthenticationValue();
            subCodeLength += respondingAuthenticationValue.decode(is, null);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 11)) {
            applicationContextNameList = new ApplicationContextNameList();
            subCodeLength += applicationContextNameList.decode(is, false);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 29)) {
            implementationInformation = new ImplementationData();
            subCodeLength += implementationInformation.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 (protocolVersion != null) {
            sb.append("protocolVersion: ").append(protocolVersion);
            firstSelectedElement = false;
        }

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

        sb.append(", ");
        sb.append("result: ").append(result);

        sb.append(", ");
        sb.append("resultSourceDiagnostic: ").append(resultSourceDiagnostic);

        if (respondingAPTitle != null) {
            sb.append(", ");
            sb.append("respondingAPTitle: ").append(respondingAPTitle);
        }

        if (respondingAEQualifier != null) {
            sb.append(", ");
            sb.append("respondingAEQualifier: ").append(respondingAEQualifier);
        }

        if (respondingAPInvocationIdentifier != null) {
            sb.append(", ");
            sb.append("respondingAPInvocationIdentifier: ").append(respondingAPInvocationIdentifier);
        }

        if (respondingAEInvocationIdentifier != null) {
            sb.append(", ");
            sb.append("respondingAEInvocationIdentifier: ").append(respondingAEInvocationIdentifier);
        }

        if (responderAcseRequirements != null) {
            sb.append(", ");
            sb.append("responderAcseRequirements: ").append(responderAcseRequirements);
        }

        if (mechanismName != null) {
            sb.append(", ");
            sb.append("mechanismName: ").append(mechanismName);
        }

        if (respondingAuthenticationValue != null) {
            sb.append(", ");
            sb.append("respondingAuthenticationValue: ").append(respondingAuthenticationValue);
        }

        if (applicationContextNameList != null) {
            sb.append(", ");
            sb.append("applicationContextNameList: ").append(applicationContextNameList);
        }

        if (implementationInformation != null) {
            sb.append(", ");
            sb.append("implementationInformation: ").append(implementationInformation);
        }

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

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

}
