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

package org.openmuc.josistack.internal.presentation.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 CPAPPDU {

    public static class NormalModeParameters {

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

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

        public RespondingPresentationSelector respondingPresentationSelector = null;

        public PresentationContextDefinitionResultList presentationContextDefinitionResultList = null;

        public PresentationRequirements presentationRequirements = null;

        public UserSessionRequirements userSessionRequirements = null;

        public UserData userData = null;

        public NormalModeParameters() {
            id = identifier;
        }

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

        public NormalModeParameters(ProtocolVersion protocolVersion,
                RespondingPresentationSelector respondingPresentationSelector,
                PresentationContextDefinitionResultList presentationContextDefinitionResultList,
                PresentationRequirements presentationRequirements, UserSessionRequirements userSessionRequirements,
                UserData userData) {
            id = identifier;
            this.protocolVersion = protocolVersion;
            this.respondingPresentationSelector = respondingPresentationSelector;
            this.presentationContextDefinitionResultList = presentationContextDefinitionResultList;
            this.presentationRequirements = presentationRequirements;
            this.userSessionRequirements = userSessionRequirements;
            this.userData = userData;
        }

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

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

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

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

                if (respondingPresentationSelector != null) {
                    codeLength += respondingPresentationSelector.encode(os, false);
                    // write tag: CONTEXT_CLASS, PRIMITIVE, 3
                    os.write(0x83);
                    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;

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

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 3)) {
                respondingPresentationSelector = new RespondingPresentationSelector();
                subCodeLength += respondingPresentationSelector.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
                subCodeLength += berIdentifier.decode(is);
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 5)) {
                presentationContextDefinitionResultList = new PresentationContextDefinitionResultList();
                subCodeLength += presentationContextDefinitionResultList.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
                subCodeLength += berIdentifier.decode(is);
            }

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

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

            userData = new UserData();
            int choiceDecodeLength = userData.decode(is, berIdentifier);
            subCodeLength += choiceDecodeLength;
            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 (respondingPresentationSelector != null) {
                if (!firstSelectedElement) {
                    sb.append(", ");
                }
                sb.append("respondingPresentationSelector: ").append(respondingPresentationSelector);
                firstSelectedElement = false;
            }

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

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

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

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

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

    }

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

    public byte[] code = null;
    public ModeSelector modeSelector = null;

    public NormalModeParameters normalModeParameters = null;

    public CPAPPDU() {
        id = identifier;
    }

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

    public CPAPPDU(ModeSelector modeSelector, NormalModeParameters normalModeParameters) {
        id = identifier;
        this.modeSelector = modeSelector;
        this.normalModeParameters = normalModeParameters;
    }

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

            codeLength += modeSelector.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 0
            os.write(0xa0);
            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;
        while (subCodeLength < totalLength) {
            subCodeLength += berIdentifier.decode(is);
            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 0)) {
                modeSelector = new ModeSelector();
                subCodeLength += modeSelector.decode(is, false);
            }
            else if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
                normalModeParameters = new NormalModeParameters();
                subCodeLength += normalModeParameters.decode(is, false);
            }
        }
        if (subCodeLength != totalLength) {
            throw new IOException("Length of set does not match length tag, length tag: " + totalLength
                    + ", actual set length: " + 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{");
        sb.append("modeSelector: ").append(modeSelector);

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

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

}
