/*
 *  ******************************************************************************
 *  *
 *  *
 *  * 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.eclipse.deeplearning4j.dl4jcore.parallelism;

import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.io.TempDir;
import org.nd4j.common.io.ClassPathResource;
import org.deeplearning4j.BaseDL4JTest;
import org.deeplearning4j.datasets.iterator.callbacks.DataSetDeserializer;
import org.deeplearning4j.datasets.iterator.parallel.FileSplitParallelDataSetIterator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.nd4j.common.tests.tags.NativeTag;
import org.nd4j.common.tests.tags.TagNames;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.common.primitives.Pair;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.DisplayName;
import java.nio.file.Path;
import static java.time.Duration.ofMillis;
import static org.junit.jupiter.api.Assertions.assertTimeout;

@Slf4j
/*
    @Test
    public void testSimpleLoop1() throws Exception {
        ParallelExistingMiniBatchDataSetIterator iterator = new ParallelExistingMiniBatchDataSetIterator(rootFolder,"mnist-train-%d.bin", 4);
        ExistingMiniBatchDataSetIterator test = new ExistingMiniBatchDataSetIterator(rootFolder,"mnist-train-%d.bin");
    
    
        List<Pair<Long, Long>> pairs = new ArrayList<>();
    
        int cnt = 0;
        long time1 = System.nanoTime();
        while (iterator.hasNext()) {
            DataSet ds = iterator.next();
            long time2 = System.nanoTime();
            assertNotNull(ds);
            assertEquals(64, ds.numExamples());
            pairs.add(new Pair<Long, Long>(time2 - time1, 0L));
            cnt++;
            time1 = System.nanoTime();
        }
        assertEquals(26, cnt);
    
        cnt = 0;
        time1 = System.nanoTime();
        while (test.hasNext()) {
            DataSet ds = test.next();
            long time2 = System.nanoTime();
            assertNotNull(ds);
            assertEquals(64, ds.numExamples());
            pairs.get(cnt).setSecond(time2 - time1);
            cnt++;
            time1 = System.nanoTime();
        }
    
        assertEquals(26, cnt);
    
        for (Pair<Long, Long> times: pairs) {
            log.info("Parallel: {} ns; Simple: {} ns", times.getFirst(), times.getSecond());
        }
    }
    
    @Test
    public void testReset1() throws Exception {
        ParallelExistingMiniBatchDataSetIterator iterator = new ParallelExistingMiniBatchDataSetIterator(rootFolder,"mnist-train-%d.bin", 8);
    
        int cnt = 0;
        long time1 = System.nanoTime();
        while (iterator.hasNext()) {
            DataSet ds = iterator.next();
            long time2 = System.nanoTime();
            assertNotNull(ds);
            assertEquals(64, ds.numExamples());
            cnt++;
    
            if (cnt == 10)
                iterator.reset();
    
            time1 = System.nanoTime();
        }
        assertEquals(36, cnt);
    }
    
    @Test
    public void testWithAdsi1() throws Exception {
        ParallelExistingMiniBatchDataSetIterator iterator = new ParallelExistingMiniBatchDataSetIterator(rootFolder,"mnist-train-%d.bin", 8);
        AsyncDataSetIterator adsi = new AsyncDataSetIterator(iterator, 8, true);
    
        int cnt = 0;
        long time1 = System.nanoTime();
        while (adsi.hasNext()) {
            DataSet ds = adsi.next();
            long time2 = System.nanoTime();
            assertNotNull(ds);
            assertEquals(64, ds.numExamples());
            cnt++;
    
            if (cnt == 10)
                adsi.reset();
    
            time1 = System.nanoTime();
        }
        assertEquals(36, cnt);
    }
    */
@DisplayName("Parallel Existing Mini Batch Data Set Iterator Test")
@NativeTag
@Tag(TagNames.DL4J_OLD_API)
class ParallelExistingMiniBatchDataSetIteratorTest extends BaseDL4JTest {

    @TempDir
    public Path tempDir;

    private static File rootFolder;

    @BeforeEach
    void setUp() throws Exception {
        if (rootFolder == null) {
            rootFolder = tempDir.toFile();
            for (int i = 0; i < 26; i++) {
                new ClassPathResource("/datasets/mnist/mnist-train-" + i + ".bin").getTempFileFromArchive(rootFolder);
            }
        }
    }

    @Test
    @DisplayName("Test New Simple Loop 1")
    void testNewSimpleLoop1() {
        assertTimeout(ofMillis(30000), () -> {
            FileSplitParallelDataSetIterator fspdsi = new FileSplitParallelDataSetIterator(rootFolder, "mnist-train-%d.bin", new DataSetDeserializer());
            List<Pair<Long, Long>> pairs = new ArrayList<>();
            long time1 = System.nanoTime();
            int cnt = 0;
            while (fspdsi.hasNext()) {
                DataSet ds = fspdsi.next();
                long time2 = System.nanoTime();
                pairs.add(new Pair<Long, Long>(time2 - time1, 0L));
                assertNotNull(ds);
                // imitating processing here
                Thread.sleep(10);
                cnt++;
                time1 = System.nanoTime();
            }
            assertEquals(26, cnt);
            for (Pair<Long, Long> times : pairs) {
                log.info("Parallel: {} ns; Simple: {} ns", times.getFirst(), times.getSecond());
            }
        });
    }
    /*
    @Test
    public void testSimpleLoop1() throws Exception {
        ParallelExistingMiniBatchDataSetIterator iterator = new ParallelExistingMiniBatchDataSetIterator(rootFolder,"mnist-train-%d.bin", 4);
        ExistingMiniBatchDataSetIterator test = new ExistingMiniBatchDataSetIterator(rootFolder,"mnist-train-%d.bin");
    
    
        List<Pair<Long, Long>> pairs = new ArrayList<>();
    
        int cnt = 0;
        long time1 = System.nanoTime();
        while (iterator.hasNext()) {
            DataSet ds = iterator.next();
            long time2 = System.nanoTime();
            assertNotNull(ds);
            assertEquals(64, ds.numExamples());
            pairs.add(new Pair<Long, Long>(time2 - time1, 0L));
            cnt++;
            time1 = System.nanoTime();
        }
        assertEquals(26, cnt);
    
        cnt = 0;
        time1 = System.nanoTime();
        while (test.hasNext()) {
            DataSet ds = test.next();
            long time2 = System.nanoTime();
            assertNotNull(ds);
            assertEquals(64, ds.numExamples());
            pairs.get(cnt).setSecond(time2 - time1);
            cnt++;
            time1 = System.nanoTime();
        }
    
        assertEquals(26, cnt);
    
        for (Pair<Long, Long> times: pairs) {
            log.info("Parallel: {} ns; Simple: {} ns", times.getFirst(), times.getSecond());
        }
    }
    
    @Test
    public void testReset1() throws Exception {
        ParallelExistingMiniBatchDataSetIterator iterator = new ParallelExistingMiniBatchDataSetIterator(rootFolder,"mnist-train-%d.bin", 8);
    
        int cnt = 0;
        long time1 = System.nanoTime();
        while (iterator.hasNext()) {
            DataSet ds = iterator.next();
            long time2 = System.nanoTime();
            assertNotNull(ds);
            assertEquals(64, ds.numExamples());
            cnt++;
    
            if (cnt == 10)
                iterator.reset();
    
            time1 = System.nanoTime();
        }
        assertEquals(36, cnt);
    }
    
    @Test
    public void testWithAdsi1() throws Exception {
        ParallelExistingMiniBatchDataSetIterator iterator = new ParallelExistingMiniBatchDataSetIterator(rootFolder,"mnist-train-%d.bin", 8);
        AsyncDataSetIterator adsi = new AsyncDataSetIterator(iterator, 8, true);
    
        int cnt = 0;
        long time1 = System.nanoTime();
        while (adsi.hasNext()) {
            DataSet ds = adsi.next();
            long time2 = System.nanoTime();
            assertNotNull(ds);
            assertEquals(64, ds.numExamples());
            cnt++;
    
            if (cnt == 10)
                adsi.reset();
    
            time1 = System.nanoTime();
        }
        assertEquals(36, cnt);
    }
    */
}
