package com.concurrentperformance.ringingmaster.engine.notation.impl;


import com.concurrentperformance.ringingmaster.engine.NumberOfBells;
import com.concurrentperformance.ringingmaster.engine.notation.NotationBody;
import com.concurrentperformance.ringingmaster.generated.notation.persist.SerializableNotation;
import com.ringingmaster.extraction.CentralCouncilMethodExtractor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@RunWith(Parameterized.class)
public class CheckAllLeadHeadsTest {

	private final Logger log = LoggerFactory.getLogger(this.getClass());


	@Parameterized.Parameters
	public static Collection<Object[]> checkAllCCLibrary() {
		return new CentralCouncilMethodExtractor()
				.extractNotationsToStream()
//				.filter(serializableNotation -> serializableNotation.getNumberOfBells() == 8)
//				.filter(serializableNotation -> serializableNotation.getName().contains("Waterloo Reverse Bob"))
				.map(serializableNotation -> new Object[]{serializableNotation})
				.collect(Collectors.toList());
	}

	public CheckAllLeadHeadsTest(SerializableNotation serializableNotation) {
		this.serializableNotation = serializableNotation;
	}

	private final SerializableNotation serializableNotation;

	@Test
	public void checkLeadHeadCorrectness() {

		log.info(serializableNotation.toString());

		NotationBody notationBody = NotationBuilder.getInstance()
				.setFromSerializableNotation(serializableNotation)
				.build();

		NumberOfBells ccNumberOfBells = NumberOfBells.valueOf(serializableNotation.getNumberOfBells());
		String ccLeadHead = serializableNotation.getLeadHead();


		// Uncomment section to log out the changes in the lead
//		log.warn(notationBody.toString());
//		log.warn(PlainCourseHelper.buildPlainCourse(notationBody, "TEST", false).getCreatedMethod().getLead(0).toString());

		LeadHeadCalculator.LeadHeadCodeType leadHeadType = LeadHeadCalculator.getLeadHeadType(ccLeadHead, ccNumberOfBells);

		String calculatedLeadHead = null;
		if (leadHeadType == LeadHeadCalculator.LeadHeadCodeType.VALID_LEADHEAD_ROW) {

			//TODO need to exclude non plain bob lead heads from calculating a code.
			// These rows are where it is not a plain bob lead head, and therefore no code is in the cc library.
//			calculatedLeadHead = LeadHeadCalculator.lookupRowFromCode(notationBody.getLeadHeadCode(), notationBody.getNumberOfWorkingBells());
			calculatedLeadHead = notationBody.getLeadHeadCode();
		}
		else if (leadHeadType == LeadHeadCalculator.LeadHeadCodeType.VALID_LEADHEAD_CODE){
			calculatedLeadHead = notationBody.getLeadHeadCode();
		}
		else {
			fail();
		}

		assertEquals("[" + notationBody.getNumberOfWorkingBells().getBellCount() + "] " + notationBody.getNameIncludingNumberOfBells() +
						"[" + ccLeadHead + "](library) vs [" + notationBody.getLeadHeadCode() + "](calculated) NOT OK ",
				ccLeadHead, calculatedLeadHead);

	}
}
@@ -130,24 +130,104 @@
 berOfBells;%0A
+import com.concurrentperformance.ringingmaster.engine.helper.PlainCourseHelper;%0A
 import com.c
@@ -291,16 +291,16 @@
 onBody;%0A
-
 import c
@@ -722,45 +722,8 @@
 als;
-%0Aimport static org.junit.Assert.fail;
 %0A%0A@R
@@ -1022,18 +1022,16 @@
 tream()%0A
-//
 %09%09%09%09.fil
@@ -1104,18 +1104,16 @@
 ) == 8)%0A
-//
 %09%09%09%09.fil
@@ -1945,18 +1945,16 @@
 he lead%0A
-//
 %09%09log.wa
@@ -1982,18 +1982,16 @@
 ing());%0A
-//
 %09%09log.wa
@@ -2255,11 +2255,37 @@
  = n
-ull
+otationBody.getLeadHeadCode()
 ;%0A%09%09
@@ -2362,17 +2362,16 @@
 _ROW) %7B%0A
-%0A
 %09%09%09//TOD
@@ -2542,18 +2542,16 @@
 ibrary.%0A
-//
 %09%09%09calcu
@@ -2680,233 +2680,8 @@
 ));%0A
-%09%09%09calculatedLeadHead = notationBody.getLeadHeadCode();%0A%09%09%7D%0A%09%09else if (leadHeadType == LeadHeadCalculator.LeadHeadCodeType.VALID_LEADHEAD_CODE)%7B%0A%09%09%09calculatedLeadHead = notationBody.getLeadHeadCode();%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09fail();%0A
 %09%09%7D%0A

Tidy up the main lead head test