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

package org.openmuc.openiec61850.internal.mms.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 InitiateRequestPDU {

    public static class InitRequestDetail {

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

        public byte[] code = null;
        private Integer16 proposedVersionNumber = null;

        private ParameterSupportOptions proposedParameterCBB = null;

        private ServiceSupportOptions servicesSupportedCalling = null;

        public InitRequestDetail() {
            id = identifier;
        }

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

        public void setProposedVersionNumber(Integer16 proposedVersionNumber) {
            this.proposedVersionNumber = proposedVersionNumber;
        }

        public Integer16 getProposedVersionNumber() {
            return proposedVersionNumber;
        }

        public void setProposedParameterCBB(ParameterSupportOptions proposedParameterCBB) {
            this.proposedParameterCBB = proposedParameterCBB;
        }

        public ParameterSupportOptions getProposedParameterCBB() {
            return proposedParameterCBB;
        }

        public void setServicesSupportedCalling(ServiceSupportOptions servicesSupportedCalling) {
            this.servicesSupportedCalling = servicesSupportedCalling;
        }

        public ServiceSupportOptions getServicesSupportedCalling() {
            return servicesSupportedCalling;
        }

        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 += servicesSupportedCalling.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 2
                os.write(0x82);
                codeLength += 1;

                codeLength += proposedParameterCBB.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 1
                os.write(0x81);
                codeLength += 1;

                codeLength += proposedVersionNumber.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)) {
                proposedVersionNumber = new Integer16();
                subCodeLength += proposedVersionNumber.decode(is, false);
                subCodeLength += berIdentifier.decode(is);
            }
            else {
                throw new IOException("Identifier does not match the mandatory sequence element identifer.");
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 1)) {
                proposedParameterCBB = new ParameterSupportOptions();
                subCodeLength += proposedParameterCBB.decode(is, false);
                subCodeLength += berIdentifier.decode(is);
            }
            else {
                throw new IOException("Identifier does not match the mandatory sequence element identifer.");
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 2)) {
                servicesSupportedCalling = new ServiceSupportOptions();
                subCodeLength += servicesSupportedCalling.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{");
            sb.append("proposedVersionNumber: ").append(proposedVersionNumber);

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

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

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

    }

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

    public byte[] code = null;
    private Integer32 localDetailCalling = null;

    private Integer16 proposedMaxServOutstandingCalling = null;

    private Integer16 proposedMaxServOutstandingCalled = null;

    private Integer8 proposedDataStructureNestingLevel = null;

    private InitRequestDetail initRequestDetail = null;

    public InitiateRequestPDU() {
        id = identifier;
    }

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

    public void setLocalDetailCalling(Integer32 localDetailCalling) {
        this.localDetailCalling = localDetailCalling;
    }

    public Integer32 getLocalDetailCalling() {
        return localDetailCalling;
    }

    public void setProposedMaxServOutstandingCalling(Integer16 proposedMaxServOutstandingCalling) {
        this.proposedMaxServOutstandingCalling = proposedMaxServOutstandingCalling;
    }

    public Integer16 getProposedMaxServOutstandingCalling() {
        return proposedMaxServOutstandingCalling;
    }

    public void setProposedMaxServOutstandingCalled(Integer16 proposedMaxServOutstandingCalled) {
        this.proposedMaxServOutstandingCalled = proposedMaxServOutstandingCalled;
    }

    public Integer16 getProposedMaxServOutstandingCalled() {
        return proposedMaxServOutstandingCalled;
    }

    public void setProposedDataStructureNestingLevel(Integer8 proposedDataStructureNestingLevel) {
        this.proposedDataStructureNestingLevel = proposedDataStructureNestingLevel;
    }

    public Integer8 getProposedDataStructureNestingLevel() {
        return proposedDataStructureNestingLevel;
    }

    public void setInitRequestDetail(InitRequestDetail initRequestDetail) {
        this.initRequestDetail = initRequestDetail;
    }

    public InitRequestDetail getInitRequestDetail() {
        return initRequestDetail;
    }

    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 += initRequestDetail.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 4
            os.write(0xa4);
            codeLength += 1;

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

            codeLength += proposedMaxServOutstandingCalled.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 2
            os.write(0x82);
            codeLength += 1;

            codeLength += proposedMaxServOutstandingCalling.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 1
            os.write(0x81);
            codeLength += 1;

            if (localDetailCalling != null) {
                codeLength += localDetailCalling.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)) {
            localDetailCalling = new Integer32();
            subCodeLength += localDetailCalling.decode(is, false);
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 1)) {
            proposedMaxServOutstandingCalling = new Integer16();
            subCodeLength += proposedMaxServOutstandingCalling.decode(is, false);
            subCodeLength += berIdentifier.decode(is);
        }
        else {
            throw new IOException("Identifier does not match the mandatory sequence element identifer.");
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 2)) {
            proposedMaxServOutstandingCalled = new Integer16();
            subCodeLength += proposedMaxServOutstandingCalled.decode(is, false);
            subCodeLength += berIdentifier.decode(is);
        }
        else {
            throw new IOException("Identifier does not match the mandatory sequence element identifer.");
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 3)) {
            proposedDataStructureNestingLevel = new Integer8();
            subCodeLength += proposedDataStructureNestingLevel.decode(is, false);
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 4)) {
            initRequestDetail = new InitRequestDetail();
            subCodeLength += initRequestDetail.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 (localDetailCalling != null) {
            sb.append("localDetailCalling: ").append(localDetailCalling);
            firstSelectedElement = false;
        }

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

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

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

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

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

}
