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

public class RejectPDU {

    public static class RejectReason {

        public byte[] code = null;
        private BerInteger confirmedRequestPDU = null;

        private BerInteger confirmedResponsePDU = null;

        private BerInteger confirmedErrorPDU = null;

        private BerInteger unconfirmedPDU = null;

        private BerInteger pduError = null;

        private BerInteger cancelRequestPDU = null;

        private BerInteger cancelResponsePDU = null;

        private BerInteger cancelErrorPDU = null;

        private BerInteger concludeRequestPDU = null;

        private BerInteger concludeResponsePDU = null;

        private BerInteger concludeErrorPDU = null;

        public RejectReason() {
        }

        public RejectReason(byte[] code) {
            this.code = code;
        }

        public void setConfirmedRequestPDU(BerInteger confirmedRequestPDU) {
            this.confirmedRequestPDU = confirmedRequestPDU;
        }

        public BerInteger getConfirmedRequestPDU() {
            return confirmedRequestPDU;
        }

        public void setConfirmedResponsePDU(BerInteger confirmedResponsePDU) {
            this.confirmedResponsePDU = confirmedResponsePDU;
        }

        public BerInteger getConfirmedResponsePDU() {
            return confirmedResponsePDU;
        }

        public void setConfirmedErrorPDU(BerInteger confirmedErrorPDU) {
            this.confirmedErrorPDU = confirmedErrorPDU;
        }

        public BerInteger getConfirmedErrorPDU() {
            return confirmedErrorPDU;
        }

        public void setUnconfirmedPDU(BerInteger unconfirmedPDU) {
            this.unconfirmedPDU = unconfirmedPDU;
        }

        public BerInteger getUnconfirmedPDU() {
            return unconfirmedPDU;
        }

        public void setPduError(BerInteger pduError) {
            this.pduError = pduError;
        }

        public BerInteger getPduError() {
            return pduError;
        }

        public void setCancelRequestPDU(BerInteger cancelRequestPDU) {
            this.cancelRequestPDU = cancelRequestPDU;
        }

        public BerInteger getCancelRequestPDU() {
            return cancelRequestPDU;
        }

        public void setCancelResponsePDU(BerInteger cancelResponsePDU) {
            this.cancelResponsePDU = cancelResponsePDU;
        }

        public BerInteger getCancelResponsePDU() {
            return cancelResponsePDU;
        }

        public void setCancelErrorPDU(BerInteger cancelErrorPDU) {
            this.cancelErrorPDU = cancelErrorPDU;
        }

        public BerInteger getCancelErrorPDU() {
            return cancelErrorPDU;
        }

        public void setConcludeRequestPDU(BerInteger concludeRequestPDU) {
            this.concludeRequestPDU = concludeRequestPDU;
        }

        public BerInteger getConcludeRequestPDU() {
            return concludeRequestPDU;
        }

        public void setConcludeResponsePDU(BerInteger concludeResponsePDU) {
            this.concludeResponsePDU = concludeResponsePDU;
        }

        public BerInteger getConcludeResponsePDU() {
            return concludeResponsePDU;
        }

        public void setConcludeErrorPDU(BerInteger concludeErrorPDU) {
            this.concludeErrorPDU = concludeErrorPDU;
        }

        public BerInteger getConcludeErrorPDU() {
            return concludeErrorPDU;
        }

        public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {
            if (code != null) {
                for (int i = code.length - 1; i >= 0; i--) {
                    os.write(code[i]);
                }
                return code.length;

            }
            int codeLength = 0;
            if (concludeErrorPDU != null) {
                codeLength += concludeErrorPDU.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 11
                os.write(0x8b);
                codeLength += 1;
                return codeLength;

            }

            if (concludeResponsePDU != null) {
                codeLength += concludeResponsePDU.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 10
                os.write(0x8a);
                codeLength += 1;
                return codeLength;

            }

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

            }

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

            }

            if (cancelResponsePDU != null) {
                codeLength += cancelResponsePDU.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 7
                os.write(0x87);
                codeLength += 1;
                return codeLength;

            }

            if (cancelRequestPDU != null) {
                codeLength += cancelRequestPDU.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 6
                os.write(0x86);
                codeLength += 1;
                return codeLength;

            }

            if (pduError != null) {
                codeLength += pduError.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 5
                os.write(0x85);
                codeLength += 1;
                return codeLength;

            }

            if (unconfirmedPDU != null) {
                codeLength += unconfirmedPDU.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 4
                os.write(0x84);
                codeLength += 1;
                return codeLength;

            }

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

            }

            if (confirmedResponsePDU != null) {
                codeLength += confirmedResponsePDU.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 2
                os.write(0x82);
                codeLength += 1;
                return codeLength;

            }

            if (confirmedRequestPDU != null) {
                codeLength += confirmedRequestPDU.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 1
                os.write(0x81);
                codeLength += 1;
                return codeLength;

            }

            throw new IOException("Error encoding BerChoice: No item in choice was selected.");
        }

        public int decode(InputStream is, BerIdentifier berIdentifier) throws IOException {
            int codeLength = 0;
            BerIdentifier passedIdentifier = berIdentifier;

            if (berIdentifier == null) {
                berIdentifier = new BerIdentifier();
                codeLength += berIdentifier.decode(is);
            }

            BerLength length = new BerLength();
            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 1)) {
                confirmedRequestPDU = new BerInteger();
                codeLength += confirmedRequestPDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 2)) {
                confirmedResponsePDU = new BerInteger();
                codeLength += confirmedResponsePDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 3)) {
                confirmedErrorPDU = new BerInteger();
                codeLength += confirmedErrorPDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 4)) {
                unconfirmedPDU = new BerInteger();
                codeLength += unconfirmedPDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 5)) {
                pduError = new BerInteger();
                codeLength += pduError.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 6)) {
                cancelRequestPDU = new BerInteger();
                codeLength += cancelRequestPDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 7)) {
                cancelResponsePDU = new BerInteger();
                codeLength += cancelResponsePDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 8)) {
                cancelErrorPDU = new BerInteger();
                codeLength += cancelErrorPDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 9)) {
                concludeRequestPDU = new BerInteger();
                codeLength += concludeRequestPDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 10)) {
                concludeResponsePDU = new BerInteger();
                codeLength += concludeResponsePDU.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 11)) {
                concludeErrorPDU = new BerInteger();
                codeLength += concludeErrorPDU.decode(is, false);
                return codeLength;
            }

            if (passedIdentifier != null) {
                return 0;
            }
            throw new IOException("Error decoding BerChoice: Identifier matched to no item.");
        }

        public void encodeAndSave(int encodingSizeGuess) throws IOException {
            BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
            encode(os, false);
            code = os.getArray();
        }

        @Override
        public String toString() {
            if (confirmedRequestPDU != null) {
                return "CHOICE{confirmedRequestPDU: " + confirmedRequestPDU + "}";
            }

            if (confirmedResponsePDU != null) {
                return "CHOICE{confirmedResponsePDU: " + confirmedResponsePDU + "}";
            }

            if (confirmedErrorPDU != null) {
                return "CHOICE{confirmedErrorPDU: " + confirmedErrorPDU + "}";
            }

            if (unconfirmedPDU != null) {
                return "CHOICE{unconfirmedPDU: " + unconfirmedPDU + "}";
            }

            if (pduError != null) {
                return "CHOICE{pduError: " + pduError + "}";
            }

            if (cancelRequestPDU != null) {
                return "CHOICE{cancelRequestPDU: " + cancelRequestPDU + "}";
            }

            if (cancelResponsePDU != null) {
                return "CHOICE{cancelResponsePDU: " + cancelResponsePDU + "}";
            }

            if (cancelErrorPDU != null) {
                return "CHOICE{cancelErrorPDU: " + cancelErrorPDU + "}";
            }

            if (concludeRequestPDU != null) {
                return "CHOICE{concludeRequestPDU: " + concludeRequestPDU + "}";
            }

            if (concludeResponsePDU != null) {
                return "CHOICE{concludeResponsePDU: " + concludeResponsePDU + "}";
            }

            if (concludeErrorPDU != null) {
                return "CHOICE{concludeErrorPDU: " + concludeErrorPDU + "}";
            }

            return "unknown";
        }

    }

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

    public byte[] code = null;
    private Unsigned32 originalInvokeID = null;

    private RejectReason rejectReason = null;

    public RejectPDU() {
        id = identifier;
    }

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

    public void setOriginalInvokeID(Unsigned32 originalInvokeID) {
        this.originalInvokeID = originalInvokeID;
    }

    public Unsigned32 getOriginalInvokeID() {
        return originalInvokeID;
    }

    public void setRejectReason(RejectReason rejectReason) {
        this.rejectReason = rejectReason;
    }

    public RejectReason getRejectReason() {
        return rejectReason;
    }

    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 += rejectReason.encode(os, true);

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

        rejectReason = new RejectReason();
        subCodeLength += rejectReason.decode(is, berIdentifier);
        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 (originalInvokeID != null) {
            sb.append("originalInvokeID: ").append(originalInvokeID);
            firstSelectedElement = false;
        }

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

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

}
