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

    public byte[] code = null;
    private GetNameListRequest getNameList = null;

    private ReadRequest read = null;

    private WriteRequest write = null;

    private GetVariableAccessAttributesRequest getVariableAccessAttributes = null;

    private DefineNamedVariableListRequest defineNamedVariableList = null;

    private GetNamedVariableListAttributesRequest getNamedVariableListAttributes = null;

    private DeleteNamedVariableListRequest deleteNamedVariableList = null;

    public ConfirmedServiceRequest() {
    }

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

    public void setGetNameList(GetNameListRequest getNameList) {
        this.getNameList = getNameList;
    }

    public GetNameListRequest getGetNameList() {
        return getNameList;
    }

    public void setRead(ReadRequest read) {
        this.read = read;
    }

    public ReadRequest getRead() {
        return read;
    }

    public void setWrite(WriteRequest write) {
        this.write = write;
    }

    public WriteRequest getWrite() {
        return write;
    }

    public void setGetVariableAccessAttributes(GetVariableAccessAttributesRequest getVariableAccessAttributes) {
        this.getVariableAccessAttributes = getVariableAccessAttributes;
    }

    public GetVariableAccessAttributesRequest getGetVariableAccessAttributes() {
        return getVariableAccessAttributes;
    }

    public void setDefineNamedVariableList(DefineNamedVariableListRequest defineNamedVariableList) {
        this.defineNamedVariableList = defineNamedVariableList;
    }

    public DefineNamedVariableListRequest getDefineNamedVariableList() {
        return defineNamedVariableList;
    }

    public void setGetNamedVariableListAttributes(
            GetNamedVariableListAttributesRequest getNamedVariableListAttributes) {
        this.getNamedVariableListAttributes = getNamedVariableListAttributes;
    }

    public GetNamedVariableListAttributesRequest getGetNamedVariableListAttributes() {
        return getNamedVariableListAttributes;
    }

    public void setDeleteNamedVariableList(DeleteNamedVariableListRequest deleteNamedVariableList) {
        this.deleteNamedVariableList = deleteNamedVariableList;
    }

    public DeleteNamedVariableListRequest getDeleteNamedVariableList() {
        return deleteNamedVariableList;
    }

    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;
        int sublength;

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

        }

        if (getNamedVariableListAttributes != null) {
            sublength = getNamedVariableListAttributes.encode(os, true);
            codeLength += sublength;
            codeLength += BerLength.encodeLength(os, sublength);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 12
            os.write(0xac);
            codeLength += 1;
            return codeLength;

        }

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

        }

        if (getVariableAccessAttributes != null) {
            sublength = getVariableAccessAttributes.encode(os, true);
            codeLength += sublength;
            codeLength += BerLength.encodeLength(os, sublength);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 6
            os.write(0xa6);
            codeLength += 1;
            return codeLength;

        }

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

        }

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

        }

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

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

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 5)) {
            write = new WriteRequest();
            codeLength += write.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 6)) {
            codeLength += length.decode(is);
            getVariableAccessAttributes = new GetVariableAccessAttributesRequest();
            codeLength += getVariableAccessAttributes.decode(is, null);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 11)) {
            defineNamedVariableList = new DefineNamedVariableListRequest();
            codeLength += defineNamedVariableList.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 12)) {
            codeLength += length.decode(is);
            getNamedVariableListAttributes = new GetNamedVariableListAttributesRequest();
            codeLength += getNamedVariableListAttributes.decode(is, null);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 13)) {
            deleteNamedVariableList = new DeleteNamedVariableListRequest();
            codeLength += deleteNamedVariableList.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 (getNameList != null) {
            return "CHOICE{getNameList: " + getNameList + "}";
        }

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

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

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

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

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

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

        return "unknown";
    }

}
