/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2015 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wildfly.client.config;

import java.util.NoSuchElementException;
import java.util.Set;

/**
 * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
 */
class SelectingXMLStreamReader extends AbstractDelegatingXMLStreamReader {
    private final Set<String> namespaces;
    private int state;
    private int level;

    private static final int ST_SEEKING = 0;
    private static final int ST_FOUND_PRE = 1;
    private static final int ST_FOUND = 2;
    private static final int ST_DONE = 3;

    SelectingXMLStreamReader(final boolean closeDelegate, final ConfigurationXMLStreamReader delegate, final Set<String> namespaces) {
        super(closeDelegate, delegate);
        this.namespaces = namespaces;
    }

    public boolean hasNext() throws ConfigXMLParseException {
        switch (state) {
            case ST_SEEKING: {
                while (super.hasNext()) {
                    final int next = super.next();
                    switch (next) {
                        case START_ELEMENT: {
                            if (namespaces.contains(super.getNamespaceURI())) {
                                state = ST_FOUND_PRE;
                                return true;
                            } else {
                                getDelegate().skipContent();
                            }
                            break;
                        }
                        case END_ELEMENT: {
                            state = ST_DONE;
                            return false;
                        }
                    }
                }
                return false;
            }
            case ST_FOUND_PRE: {
                return true;
            }
            case ST_FOUND: {
                return super.hasNext();
            }
            case ST_DONE: {
                return false;
            }
            default: {
                throw new IllegalStateException();
            }
        }
    }

    public int next() throws ConfigXMLParseException {
        if (! hasNext()) throw new NoSuchElementException();
        switch (state) {
            case ST_FOUND_PRE: {
                state = ST_FOUND;
                return super.getEventType();
            }
            case ST_FOUND: {
                int next = super.next();
                switch (next) {
                    case START_ELEMENT: {
                        level ++;
                        break;
                    }
                    case END_ELEMENT: {
                        if (level-- == 0) {
                            state = ST_DONE;
                        }
                        break;
                    }
                }
                return next;
            }
            default: {
                throw new IllegalStateException();
            }
        }
    }
}
