package bschecker.bluesheets;

import java.util.ArrayList;

import bschecker.util.ErrorList;
import bschecker.util.LogHelper;
import bschecker.util.Tools;
import bschecker.util.UtilityMethods;

/**
 * Defines abstract class for types of grammatical errors
 * @author tedpyne
 * @author JeremiahDeGreeff
 */
public abstract class Bluesheet {
	
	/**
	 * Finds errors of a specific type in the submitted text.
	 * @param line the paragraph in which to find errors
	 * @param parses a String array of the parses of each sentence of the line
	 * @return an ErrorList which for each error references start and end tokens, the bluesheet number [1 - 14], and, optionally, a note
	 */
	protected abstract ErrorList findErrors(String line, String[] parses);
	
	protected ErrorList findErrors(String line) {
		String[] sentences = Tools.getSentenceDetector().sentDetect(line), parses = new String[sentences.length];
		for(int i = 0; i < sentences.length; i++)
			parses[i] = UtilityMethods.parse(sentences[i]);
		return findErrors(line, parses);
	}
	
	/**
	 * Finds all errors within the given text.
	 * All types included in {@code ERROR_LIST} which have a {@code CheckedWhenAnalyzed} value of {@code true} will be checked.
	 * Assumes that text ends with a new line character.
	 * @param text the text to search
	 * @return a ErrorList which contains all the errors in the passage, referenced by character indices
	 */
	public static ErrorList findAllErrors(String text) {
		long start = System.currentTimeMillis();
		ErrorList errors = new ErrorList(text, false);
		int lineNum = 1, charOffset = 0;
		String line;
		while (charOffset < text.length()) {
			long lineStart = System.currentTimeMillis();
			line = text.substring(charOffset, charOffset + text.substring(charOffset).indexOf('\n'));
			
			System.out.println();
			LogHelper.getLogger(17).info("Analyzing line " + lineNum + " (characters " + charOffset + "-" + (charOffset + line.length()) + "):");
			ArrayList<Integer> removedChars = new ArrayList<Integer>();
			line = UtilityMethods.removeExtraPunctuation(line, charOffset, removedChars);
			LogHelper.getLogger(17).info("Ignoring characters: " + removedChars);
			
			long parseStart = System.currentTimeMillis();
			LogHelper.getLogger(18).info("Parsing line " + lineNum + "...");
			String[] sentences = Tools.getSentenceDetector().sentDetect(line), parses = new String[sentences.length];
			for(int i = 0; i < sentences.length; i++)
				parses[i] = UtilityMethods.parse(sentences[i]);
			LogHelper.getLogger(18).info("Complete (" + ((System.currentTimeMillis() - parseStart) / 1000d) + "s)");
			
			ErrorList lineErrors = new ErrorList(line, true);
			for(Bluesheets b : Bluesheets.values())
				if(Bluesheets.isSetToAnalyze(b.getNumber())){
					long bluesheetStart = System.currentTimeMillis();
					LogHelper.getLogger(17).info("Looking for: " + b.getName() + "...");
					ErrorList temp = b.getObject().findErrors(line, parses);
					temp.setBluesheetNumber(b.getNumber());
					lineErrors.addAll(temp);
					LogHelper.getLogger(17).info(temp.size() + (temp.size() == 1 ? " Error" : " Errors") + " Found (" + ((System.currentTimeMillis() - bluesheetStart) / 1000d) + "s)");
				}
			LogHelper.getLogger(17).info(lineErrors.size() + (lineErrors.size() == 1 ? " Error" : " Errors") + " Found in line " + lineNum + " (" + ((System.currentTimeMillis() - lineStart) / 1000d) + "s)");
			
			
			errors.addAll(lineErrors.tokensToChars(charOffset, removedChars));

			lineNum++;
			charOffset += line.length() + removedChars.size() + 1;
		}
		System.out.println();
		LogHelper.getLogger(17).info("Passage analyzed in " + ((System.currentTimeMillis() - start) / 1000d) + "s\n\n" + errors);
		
		return errors;
	}
	
}

@@ -747,298 +747,8 @@
 ;%0A%09%0A
-%09protected ErrorList findErrors(String line) %7B%0A%09%09String%5B%5D sentences = Tools.getSentenceDetector().sentDetect(line), parses = new String%5Bsentences.length%5D;%0A%09%09for(int i = 0; i %3C sentences.length; i++)%0A%09%09%09parses%5Bi%5D = UtilityMethods.parse(sentences%5Bi%5D);%0A%09%09return findErrors(line, parses);%0A%09%7D%0A%09%0A
 %09/**
@@ -2455,16 +2455,17 @@
 mber()))
+ 
 %7B%0A%09%09%09%09%09l
@@ -3177,16 +3177,19 @@
 hars));%0A
+%09%09%09
 %0A%09%09%09line

remove old method and fix formatting