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

public class AuthenticationValue {

    public byte[] code = null;
    public BerGraphicString charstring = null;

    public BerBitString bitstring = null;

    public Myexternal2 external = null;

    public AuthenticationValue() {
    }

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

    public AuthenticationValue(BerGraphicString charstring, BerBitString bitstring, Myexternal2 external) {
        this.charstring = charstring;
        this.bitstring = bitstring;
        this.external = external;
    }

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

        }

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

        }

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

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 1)) {
            bitstring = new BerBitString();
            codeLength += bitstring.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
            external = new Myexternal2();
            codeLength += external.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 (charstring != null) {
            return "CHOICE{charstring: " + charstring + "}";
        }

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

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

        return "unknown";
    }

}
