/*
 *  ******************************************************************************
 *  *
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Apache License, Version 2.0 which is available at
 *  * https://www.apache.org/licenses/LICENSE-2.0.
 *  *
 *  *  See the NOTICE file distributed with this work for additional
 *  *  information regarding copyright ownership.
 *  * 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.
 *  *
 *  * SPDX-License-Identifier: Apache-2.0
 *  *****************************************************************************
 */

package org.deeplearning4j.text.documentiterator;

import lombok.NonNull;

import java.util.Iterator;

public class SimpleLabelAwareIterator implements LabelAwareIterator {
    protected transient Iterable<LabelledDocument> underlyingIterable;
    protected transient Iterator<LabelledDocument> currentIterator;
    protected LabelsSource labels = new LabelsSource();

    /**
     * Builds LabelAwareIterator instance using Iterable object
     * @param iterable
     */
    public SimpleLabelAwareIterator(@NonNull Iterable<LabelledDocument> iterable) {
        this.underlyingIterable = iterable;
        this.currentIterator = underlyingIterable.iterator();
    }

    /**
     * Builds LabelAwareIterator instance using Iterator object
     * PLEASE NOTE: If instance is built using Iterator object, reset() method becomes unavailable
     *
     * @param iterator
     */
    public SimpleLabelAwareIterator(@NonNull Iterator<LabelledDocument> iterator) {
        this.currentIterator = iterator;
    }

    /**
     * This method checks, if there's more LabelledDocuments in underlying iterator
     * @return
     */
    @Override
    public boolean hasNextDocument() {
        return currentIterator.hasNext();
    }

    /**
     * This method returns next LabelledDocument from underlying iterator
     * @return
     */
    @Override
    public LabelledDocument nextDocument() {
        LabelledDocument document = currentIterator.next();
        for (String label : document.getLabels()) {
            labels.storeLabel(label);
        }

        return document;
    }

    @Override
    public boolean hasNext() {
        return hasNextDocument();
    }

    @Override
    public LabelledDocument next() {
        return nextDocument();
    }

    @Override
    public void remove() {
        // no-op
    }

    @Override
    public void shutdown() {
        // no-op
    }

    /**
     * This methods resets LabelAwareIterator by creating new Iterator from Iterable internally
     */
    @Override
    public void reset() {
        if (underlyingIterable != null)
            this.currentIterator = this.underlyingIterable.iterator();
        else
            throw new UnsupportedOperationException(
                            "You can't use reset() method for Iterator<> based instance, please provide Iterable<> instead, or avoid reset()");
    }

    /**
     * This method returns LabelsSource instance, containing all labels derived from this iterator
     * @return
     */
    @Override
    public LabelsSource getLabelsSource() {
        return labels;
    }
}
