package org.ringingmaster.engine.parsernew.assignparsetype;

import org.ringingmaster.engine.parser.ParseType;
import org.ringingmaster.engine.parsernew.cell.Group;
import org.ringingmaster.engine.parsernew.cell.ParsedCell;
import org.ringingmaster.engine.parsernew.cell.Section;

import java.util.Arrays;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * TODO comments???
 *
 * @author stevelake
 */

public class AssertParse {


    public static void assertParse(ParsedCell parsedCell, SectionExpected... expecteds) {
        assertNotNull(parsedCell);

        assertEquals(Arrays.stream(expecteds).mapToInt(e -> e.length).sum(), parsedCell.getElementSize());

        int elementIndex = 0;
        for (SectionExpected expected : expecteds) {
            Optional<Section> sectionAtFirstElementIndex = parsedCell.getSectionAtElementIndex(elementIndex);
            Optional<Group> wordAtFirstElementIndex = parsedCell.getWordAtElementIndex(elementIndex);

            if (expected.parseType == null) {
                assertFalse(sectionAtFirstElementIndex.isPresent());
                assertFalse(wordAtFirstElementIndex.isPresent());

                for (int i=0;i<expected.length;i++) {
                    assertFalse(parsedCell.getSectionAtElementIndex(elementIndex).isPresent());
                    assertFalse(parsedCell.getWordAtElementIndex(elementIndex).isPresent());
                    elementIndex++;
                }
            }
            else {
                assertTrue(sectionAtFirstElementIndex.isPresent());
                assertEquals(expected.parseType, sectionAtFirstElementIndex.get().getParseType());

                assertTrue(wordAtFirstElementIndex.isPresent());
                assertEquals(1, wordAtFirstElementIndex.get().getSections().size());
                assertEquals(sectionAtFirstElementIndex.get(), wordAtFirstElementIndex.get().getSections().get(0));

                for (int i = 0; i < expected.length; i++) {
                    assertEquals(sectionAtFirstElementIndex.get(), parsedCell.getSectionAtElementIndex(elementIndex).get());
                    assertEquals(sectionAtFirstElementIndex.get(), parsedCell.getWordAtElementIndex(elementIndex).get().getSections().get(0));

                    elementIndex++;
                }
            }
        }
    }


    public static class SectionExpected {
        final int length;
        final ParseType parseType;

        SectionExpected(int length, ParseType parseType) {
            this.length = length;
            this.parseType = parseType;
        }
    }

    public static SectionExpected parsed(ParseType parseType) {
        return parsed(1, parseType);
    }

    public static SectionExpected parsed(int length, ParseType parseType) {
        return new SectionExpected(length, parseType);
    }

    public static SectionExpected unparsed(int length) {
        return parsed(length, null);
    }

}
@@ -272,16 +272,73 @@
 Section;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -644,16 +644,99 @@
 arse %7B%0A%0A
+    private final static Logger log = LoggerFactory.getLogger(AssertParse.class);%0A%0A
 %0A    pub
@@ -1797,16 +1797,47 @@
 ertTrue(
+%22Missing Section %22 + expected, 
 sectionA
@@ -2625,32 +2625,83 @@
 %7D%0A            %7D%0A
+            log.info(%22Section %5B%7B%7D%5D OK%22, expected);%0A
         %7D%0A    %7D%0A
@@ -2940,32 +2940,214 @@
 eType;%0A        %7D
+%0A%0A        @Override%0A        public String toString() %7B%0A            return %22%7B%22 +%0A                    length +%0A                    %22, %22 + parseType +%0A                    '%7D';%0A        %7D
 %0A    %7D%0A%0A    publ
@@ -3314,32 +3314,64 @@
 pe parseType) %7B%0A
+        assertTrue(length %3E 0);%0A
         return n
@@ -3517,9 +3517,94 @@
 %0A    %7D%0A%0A
+    public static SectionExpected unparsed() %7B%0A        return parsed(1, null);%0A    %7D%0A
 %7D

Add logging