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

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

    private ConfirmedResponsePDU confirmedResponsePDU = null;

    private ConfirmedErrorPDU confirmedErrorPDU = null;

    private UnconfirmedPDU unconfirmedPDU = null;

    private RejectPDU rejectPDU = null;

    private InitiateRequestPDU initiateRequestPDU = null;

    private InitiateResponsePDU initiateResponsePDU = null;

    private InitiateErrorPDU initiateErrorPDU = null;

    private ConcludeRequestPDU concludeRequestPDU = null;

    public MMSpdu() {
    }

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

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

    public ConfirmedRequestPDU getConfirmedRequestPDU() {
        return confirmedRequestPDU;
    }

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

    public ConfirmedResponsePDU getConfirmedResponsePDU() {
        return confirmedResponsePDU;
    }

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

    public ConfirmedErrorPDU getConfirmedErrorPDU() {
        return confirmedErrorPDU;
    }

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

    public UnconfirmedPDU getUnconfirmedPDU() {
        return unconfirmedPDU;
    }

    public void setRejectPDU(RejectPDU rejectPDU) {
        this.rejectPDU = rejectPDU;
    }

    public RejectPDU getRejectPDU() {
        return rejectPDU;
    }

    public void setInitiateRequestPDU(InitiateRequestPDU initiateRequestPDU) {
        this.initiateRequestPDU = initiateRequestPDU;
    }

    public InitiateRequestPDU getInitiateRequestPDU() {
        return initiateRequestPDU;
    }

    public void setInitiateResponsePDU(InitiateResponsePDU initiateResponsePDU) {
        this.initiateResponsePDU = initiateResponsePDU;
    }

    public InitiateResponsePDU getInitiateResponsePDU() {
        return initiateResponsePDU;
    }

    public void setInitiateErrorPDU(InitiateErrorPDU initiateErrorPDU) {
        this.initiateErrorPDU = initiateErrorPDU;
    }

    public InitiateErrorPDU getInitiateErrorPDU() {
        return initiateErrorPDU;
    }

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

    public ConcludeRequestPDU getConcludeRequestPDU() {
        return concludeRequestPDU;
    }

    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 (concludeRequestPDU != null) {
            codeLength += concludeRequestPDU.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 11
            os.write(0x8b);
            codeLength += 1;
            return codeLength;

        }

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

        }

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

        }

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

        }

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

        }

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

        }

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

        }

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

        }

        if (confirmedRequestPDU != null) {
            codeLength += confirmedRequestPDU.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 0
            os.write(0xa0);
            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.CONSTRUCTED, 0)) {
            confirmedRequestPDU = new ConfirmedRequestPDU();
            codeLength += confirmedRequestPDU.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 1)) {
            confirmedResponsePDU = new ConfirmedResponsePDU();
            codeLength += confirmedResponsePDU.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
            confirmedErrorPDU = new ConfirmedErrorPDU();
            codeLength += confirmedErrorPDU.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 3)) {
            unconfirmedPDU = new UnconfirmedPDU();
            codeLength += unconfirmedPDU.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 4)) {
            rejectPDU = new RejectPDU();
            codeLength += rejectPDU.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 8)) {
            initiateRequestPDU = new InitiateRequestPDU();
            codeLength += initiateRequestPDU.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 9)) {
            initiateResponsePDU = new InitiateResponsePDU();
            codeLength += initiateResponsePDU.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 10)) {
            initiateErrorPDU = new InitiateErrorPDU();
            codeLength += initiateErrorPDU.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 11)) {
            concludeRequestPDU = new ConcludeRequestPDU();
            codeLength += concludeRequestPDU.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 (rejectPDU != null) {
            return "CHOICE{rejectPDU: " + rejectPDU + "}";
        }

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

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

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

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

        return "unknown";
    }

}
