/**
 * This class file was automatically generated by jASN1 v1.6.0 (http://www.openmuc.org)
 */

package org.openmuc.josistack.internal.acse.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 AssociateSourceDiagnostic {

    public byte[] code = null;
    public BerInteger acseServiceUser = null;

    public BerInteger acseServiceProvider = null;

    public AssociateSourceDiagnostic() {
    }

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

    public AssociateSourceDiagnostic(BerInteger acseServiceUser, BerInteger acseServiceProvider) {
        this.acseServiceUser = acseServiceUser;
        this.acseServiceProvider = acseServiceProvider;
    }

    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 (acseServiceProvider != null) {
            sublength = acseServiceProvider.encode(os, true);
            codeLength += sublength;
            codeLength += BerLength.encodeLength(os, sublength);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 2
            os.write(0xa2);
            codeLength += 1;
            return codeLength;

        }

        if (acseServiceUser != null) {
            sublength = acseServiceUser.encode(os, true);
            codeLength += sublength;
            codeLength += BerLength.encodeLength(os, sublength);
            // 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)) {
            codeLength += length.decode(is);
            acseServiceUser = new BerInteger();
            codeLength += acseServiceUser.decode(is, true);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
            codeLength += length.decode(is);
            acseServiceProvider = new BerInteger();
            codeLength += acseServiceProvider.decode(is, true);
            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 (acseServiceUser != null) {
            return "CHOICE{acseServiceUser: " + acseServiceUser + "}";
        }

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

        return "unknown";
    }

}
