/**
 * 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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;
import org.openmuc.jasn1.ber.types.string.BerVisibleString;

public class DeleteNamedVariableListRequest {

    public static class ListOfVariableListName {

        public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
                BerIdentifier.CONSTRUCTED, 16);
        protected BerIdentifier id;

        public byte[] code = null;
        private List<ObjectName> seqOf = null;

        public ListOfVariableListName() {
            id = identifier;
            seqOf = new ArrayList<>();
        }

        public ListOfVariableListName(byte[] code) {
            id = identifier;
            this.code = code;
        }

        public List<ObjectName> getObjectName() {
            if (seqOf == null) {
                seqOf = new ArrayList<>();
            }
            return seqOf;
        }

        public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {
            int codeLength;

            if (code != null) {
                codeLength = code.length;
                for (int i = code.length - 1; i >= 0; i--) {
                    os.write(code[i]);
                }
            }
            else {
                codeLength = 0;
                for (int i = (seqOf.size() - 1); i >= 0; i--) {
                    codeLength += seqOf.get(i).encode(os, true);
                }

                codeLength += BerLength.encodeLength(os, codeLength);

            }

            if (explicit) {
                codeLength += id.encode(os);
            }

            return codeLength;
        }

        public int decode(InputStream is, boolean explicit) throws IOException {
            int codeLength = 0;
            int subCodeLength = 0;
            BerIdentifier berIdentifier = new BerIdentifier();
            if (explicit) {
                codeLength += id.decodeAndCheck(is);
            }

            BerLength length = new BerLength();
            codeLength += length.decode(is);
            int totalLength = length.val;

            while (subCodeLength < totalLength) {
                ObjectName element = new ObjectName();
                subCodeLength += element.decode(is, null);
                seqOf.add(element);
            }
            if (subCodeLength != totalLength) {
                throw new IOException("Decoded SequenceOf or SetOf has wrong length. Expected " + totalLength
                        + " but has " + subCodeLength);

            }
            codeLength += subCodeLength;

            return codeLength;
        }

        public void encodeAndSave(int encodingSizeGuess) throws IOException {
            BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
            encode(os, false);
            code = os.getArray();
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("SEQUENCE OF{");

            if (seqOf == null) {
                sb.append("null");
            }
            else {
                Iterator<ObjectName> it = seqOf.iterator();
                if (it.hasNext()) {
                    sb.append(it.next());
                    while (it.hasNext()) {
                        sb.append(", ").append(it.next());
                    }
                }
            }

            sb.append("}");

            return sb.toString();
        }

    }

    public static class DomainName {

        public byte[] code = null;
        private BasicIdentifier basic = null;

        public DomainName() {
        }

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

        public void setBasic(BasicIdentifier basic) {
            this.basic = basic;
        }

        public BasicIdentifier getBasic() {
            return basic;
        }

        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 (basic != null) {
                codeLength += basic.encode(os, true);
                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(BerVisibleString.identifier)) {
                basic = new BasicIdentifier();
                codeLength += basic.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 (basic != null) {
                return "CHOICE{basic: " + basic + "}";
            }

            return "unknown";
        }

    }

    public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
            BerIdentifier.CONSTRUCTED, 16);
    protected BerIdentifier id;

    public byte[] code = null;
    private BerInteger scopeOfDelete = null;

    private ListOfVariableListName listOfVariableListName = null;

    private DomainName domainName = null;

    public DeleteNamedVariableListRequest() {
        id = identifier;
    }

    public DeleteNamedVariableListRequest(byte[] code) {
        id = identifier;
        this.code = code;
    }

    public void setScopeOfDelete(BerInteger scopeOfDelete) {
        this.scopeOfDelete = scopeOfDelete;
    }

    public BerInteger getScopeOfDelete() {
        return scopeOfDelete;
    }

    public void setListOfVariableListName(ListOfVariableListName listOfVariableListName) {
        this.listOfVariableListName = listOfVariableListName;
    }

    public ListOfVariableListName getListOfVariableListName() {
        return listOfVariableListName;
    }

    public void setDomainName(DomainName domainName) {
        this.domainName = domainName;
    }

    public DomainName getDomainName() {
        return domainName;
    }

    public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {

        int codeLength;

        if (code != null) {
            codeLength = code.length;
            for (int i = code.length - 1; i >= 0; i--) {
                os.write(code[i]);
            }
        }
        else {
            codeLength = 0;
            int sublength;

            if (domainName != null) {
                sublength = domainName.encode(os, true);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 2
                os.write(0xa2);
                codeLength += 1;
            }

            if (listOfVariableListName != null) {
                codeLength += listOfVariableListName.encode(os, false);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 1
                os.write(0xa1);
                codeLength += 1;
            }

            if (scopeOfDelete != null) {
                codeLength += scopeOfDelete.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 0
                os.write(0x80);
                codeLength += 1;
            }

            codeLength += BerLength.encodeLength(os, codeLength);
        }

        if (explicit) {
            codeLength += id.encode(os);
        }

        return codeLength;

    }

    public int decode(InputStream is, boolean explicit) throws IOException {
        int codeLength = 0;
        int subCodeLength = 0;
        BerIdentifier berIdentifier = new BerIdentifier();

        if (explicit) {
            codeLength += id.decodeAndCheck(is);
        }

        BerLength length = new BerLength();
        codeLength += length.decode(is);

        int totalLength = length.val;
        codeLength += totalLength;

        if (totalLength == 0) {
            return codeLength;
        }
        subCodeLength += berIdentifier.decode(is);
        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 0)) {
            scopeOfDelete = new BerInteger();
            subCodeLength += scopeOfDelete.decode(is, false);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 1)) {
            listOfVariableListName = new ListOfVariableListName();
            subCodeLength += listOfVariableListName.decode(is, false);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
            subCodeLength += berIdentifier.decode(is);
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
            subCodeLength += length.decode(is);
            domainName = new DomainName();
            subCodeLength += domainName.decode(is, null);
            if (subCodeLength == totalLength) {
                return codeLength;
            }
        }
        throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: "
                + subCodeLength);

    }

    public void encodeAndSave(int encodingSizeGuess) throws IOException {
        BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
        encode(os, false);
        code = os.getArray();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("SEQUENCE{");
        boolean firstSelectedElement = true;
        if (scopeOfDelete != null) {
            sb.append("scopeOfDelete: ").append(scopeOfDelete);
            firstSelectedElement = false;
        }

        if (listOfVariableListName != null) {
            if (!firstSelectedElement) {
                sb.append(", ");
            }
            sb.append("listOfVariableListName: ").append(listOfVariableListName);
            firstSelectedElement = false;
        }

        if (domainName != null) {
            if (!firstSelectedElement) {
                sb.append(", ");
            }
            sb.append("domainName: ").append(domainName);
            firstSelectedElement = false;
        }

        sb.append("}");
        return sb.toString();
    }

}
