package net.ranides.assira.collection.query.derived;

import lombok.RequiredArgsConstructor;
import net.ranides.assira.collection.iterators.IteratorUtils;
import net.ranides.assira.collection.query.CQuery;
import net.ranides.test.CQueryAssert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.*;

public class CQTreeBFSTest {

    private final MyNode tree = new MyNode(
        "1", Arrays.asList(
        new MyNode("1/1", Arrays.asList(
            new MyNode("1/1/1", Arrays.asList()),
            new MyNode("1/1/2", Arrays.asList()),
            new MyNode("1/1/3", Arrays.asList()),
            new MyNode("1/1/4", Arrays.asList())
        )),
        new MyNode("1/2", Arrays.asList(
            new MyNode("1/2/1", Arrays.asList()),
            new MyNode("1/2/2", Arrays.asList(
                new MyNode("1/2/2/1", Arrays.asList()),
                new MyNode("1/2/2/2", Arrays.asList()),
                new MyNode("1/2/2/3", Arrays.asList())
            )),
            new MyNode("1/2/3", Arrays.asList())
        )),
        new MyNode("1/3", Arrays.asList(
            new MyNode("1/3/1", Arrays.asList()),
            new MyNode("1/3/2", Arrays.asList()),
            new MyNode("1/3/3", Arrays.asList()),
            new MyNode("1/3/4", Arrays.asList()),
            new MyNode("1/3/5", Arrays.asList())
        ))
    )
    );

    private final List<String> bfs = Arrays.asList(
        "1",
        "1/1",
        "1/2",
        "1/3",
        "1/1/1",
        "1/1/2",
        "1/1/3",
        "1/1/4",
        "1/2/1",
        "1/2/2",
        "1/2/3",
        "1/3/1",
        "1/3/2",
        "1/3/3",
        "1/3/4",
        "1/3/5",
        "1/2/2/1",
        "1/2/2/2",
        "1/2/2/3"
    );

    @Test
    public void testBasic() {
        CQuery<MyNode> query = CQuery.from().tree().bfs(() -> tree, node -> CQuery.from(node.nodes));
        CQueryAssert.assertEqualsNull(bfs, query.map(v -> v.value));
    }

    @Test
    public void testForEach() {
        CQuery<MyNode> query = CQuery.from().tree().bfs(() -> tree, node -> CQuery.from(node.nodes));

        List<String> out = new ArrayList<>();
        query.forEach(v -> out.add(v.value));

        assertEquals(bfs, out);
    }

    @Test
    public void testWhileEach() {
        CQuery<MyNode> query = CQuery.from().tree().bfs(() -> tree, node -> CQuery.from(node.nodes));

        List<String> out1 = new ArrayList<>();
        assertTrue(query.whileEach(v -> {
            out1.add(v.value);
            return true;
        }));

        List<String> out2 = new ArrayList<>();
        assertFalse(query.whileEach(v -> {
            out2.add(v.value);
            return !"1/3/2".equals(v.value);
        }));

        List<String> out3 = new ArrayList<>();
        assertFalse(query.whileEach(v -> {
            out3.add(v.value);
            return !"1/2/2/2".equals(v.value);
        }));

        assertEquals(bfs, out1);
        assertEquals(bfs.subList(0, 13), out2);
        assertEquals(bfs.subList(0, 18), out3);
    }

    @Test
    public void testIterator() {
        CQuery<MyNode> query = CQuery.from().tree().bfs(() -> tree, node -> CQuery.from(node.nodes));

        List<String> out = new ArrayList<>();
        Iterator<MyNode> itr = query.iterator();
        while (itr.hasNext()) {
            out.add(itr.next().value);
        }
        assertEquals(bfs, out);
    }

    @Test
    public void testList() {
        CQuery<MyNode> query = CQuery.from().tree().bfs(() -> tree, node -> CQuery.from(node.nodes));

        List<String> out = query.map(v -> v.value).list();
        assertEquals(bfs, out);
    }

    @RequiredArgsConstructor
    private static class MyNode {
        final String value;
        final List<MyNode> nodes;
    }
}