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

    public static class InitResponseDetail {

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

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

        private ParameterSupportOptions negotiatedParameterCBB = null;

        private ServiceSupportOptions servicesSupportedCalled = null;

        public InitResponseDetail() {
            id = identifier;
        }

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

        public void setNegotiatedVersionNumber(Integer16 negotiatedVersionNumber) {
            this.negotiatedVersionNumber = negotiatedVersionNumber;
        }

        public Integer16 getNegotiatedVersionNumber() {
            return negotiatedVersionNumber;
        }

        public void setNegotiatedParameterCBB(ParameterSupportOptions negotiatedParameterCBB) {
            this.negotiatedParameterCBB = negotiatedParameterCBB;
        }

        public ParameterSupportOptions getNegotiatedParameterCBB() {
            return negotiatedParameterCBB;
        }

        public void setServicesSupportedCalled(ServiceSupportOptions servicesSupportedCalled) {
            this.servicesSupportedCalled = servicesSupportedCalled;
        }

        public ServiceSupportOptions getServicesSupportedCalled() {
            return servicesSupportedCalled;
        }

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

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

                codeLength += negotiatedVersionNumber.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)) {
                negotiatedVersionNumber = new Integer16();
                subCodeLength += negotiatedVersionNumber.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)) {
                negotiatedParameterCBB = new ParameterSupportOptions();
                subCodeLength += negotiatedParameterCBB.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)) {
                servicesSupportedCalled = new ServiceSupportOptions();
                subCodeLength += servicesSupportedCalled.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("negotiatedVersionNumber: ").append(negotiatedVersionNumber);

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

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

            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 localDetailCalled = null;

    private Integer16 negotiatedMaxServOutstandingCalling = null;

    private Integer16 negotiatedMaxServOutstandingCalled = null;

    private Integer8 negotiatedDataStructureNestingLevel = null;

    private InitResponseDetail initResponseDetail = null;

    public InitiateResponsePDU() {
        id = identifier;
    }

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

    public void setLocalDetailCalled(Integer32 localDetailCalled) {
        this.localDetailCalled = localDetailCalled;
    }

    public Integer32 getLocalDetailCalled() {
        return localDetailCalled;
    }

    public void setNegotiatedMaxServOutstandingCalling(Integer16 negotiatedMaxServOutstandingCalling) {
        this.negotiatedMaxServOutstandingCalling = negotiatedMaxServOutstandingCalling;
    }

    public Integer16 getNegotiatedMaxServOutstandingCalling() {
        return negotiatedMaxServOutstandingCalling;
    }

    public void setNegotiatedMaxServOutstandingCalled(Integer16 negotiatedMaxServOutstandingCalled) {
        this.negotiatedMaxServOutstandingCalled = negotiatedMaxServOutstandingCalled;
    }

    public Integer16 getNegotiatedMaxServOutstandingCalled() {
        return negotiatedMaxServOutstandingCalled;
    }

    public void setNegotiatedDataStructureNestingLevel(Integer8 negotiatedDataStructureNestingLevel) {
        this.negotiatedDataStructureNestingLevel = negotiatedDataStructureNestingLevel;
    }

    public Integer8 getNegotiatedDataStructureNestingLevel() {
        return negotiatedDataStructureNestingLevel;
    }

    public void setInitResponseDetail(InitResponseDetail initResponseDetail) {
        this.initResponseDetail = initResponseDetail;
    }

    public InitResponseDetail getInitResponseDetail() {
        return initResponseDetail;
    }

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

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

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

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

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

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 1)) {
            negotiatedMaxServOutstandingCalling = new Integer16();
            subCodeLength += negotiatedMaxServOutstandingCalling.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)) {
            negotiatedMaxServOutstandingCalled = new Integer16();
            subCodeLength += negotiatedMaxServOutstandingCalled.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)) {
            negotiatedDataStructureNestingLevel = new Integer8();
            subCodeLength += negotiatedDataStructureNestingLevel.decode(is, false);
            subCodeLength += berIdentifier.decode(is);
        }

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

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

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

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

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

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

}
