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

    public byte[] code = null;
    private DataAccessError failure = null;

    private Data success = null;

    public AccessResult() {
    }

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

    public void setFailure(DataAccessError failure) {
        this.failure = failure;
    }

    public DataAccessError getFailure() {
        return failure;
    }

    public void setSuccess(Data success) {
        this.success = success;
    }

    public Data getSuccess() {
        return success;
    }

    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 (success != null) {
            codeLength += success.encode(os, true);
            return codeLength;

        }

        if (failure != null) {
            codeLength += failure.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)) {
            failure = new DataAccessError();
            codeLength += failure.decode(is, false);
            return codeLength;
        }

        success = new Data();
        int choiceDecodeLength = success.decode(is, berIdentifier);
        if (choiceDecodeLength != 0) {
            codeLength += choiceDecodeLength;
            return codeLength;
        }
        else {
            success = null;
        }

        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 (failure != null) {
            return "CHOICE{failure: " + failure + "}";
        }

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

        return "unknown";
    }

}
