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

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

    private ReadResponse read = null;

    private WriteResponse write = null;

    private GetVariableAccessAttributesResponse getVariableAccessAttributes = null;

    private DefineNamedVariableListResponse defineNamedVariableList = null;

    private GetNamedVariableListAttributesResponse getNamedVariableListAttributes = null;

    private DeleteNamedVariableListResponse deleteNamedVariableList = null;

    public ConfirmedServiceResponse() {
    }

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

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

    public GetNameListResponse getGetNameList() {
        return getNameList;
    }

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

    public ReadResponse getRead() {
        return read;
    }

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

    public WriteResponse getWrite() {
        return write;
    }

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

    public GetVariableAccessAttributesResponse getGetVariableAccessAttributes() {
        return getVariableAccessAttributes;
    }

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

    public DefineNamedVariableListResponse getDefineNamedVariableList() {
        return defineNamedVariableList;
    }

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

    public GetNamedVariableListAttributesResponse getGetNamedVariableListAttributes() {
        return getNamedVariableListAttributes;
    }

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

    public DeleteNamedVariableListResponse 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;
        if (deleteNamedVariableList != null) {
            codeLength += deleteNamedVariableList.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 13
            os.write(0xad);
            codeLength += 1;
            return codeLength;

        }

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

        }

        if (getVariableAccessAttributes != null) {
            codeLength += getVariableAccessAttributes.encode(os, false);
            // 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 GetNameListResponse();
            codeLength += getNameList.decode(is, false);
            return codeLength;
        }

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

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

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

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

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

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 13)) {
            deleteNamedVariableList = new DeleteNamedVariableListResponse();
            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";
    }

}
