package modules;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import objects.Filter;

import objects.Line;

public class CircularShifter extends Filter {

	private List<Line> lines;

	private static String[] IGNORED_WORD_LIST = {"after","as","and","of","is","the","a"};

	private static final Set<String> IGNORED_WORD_SET = new HashSet<String>(Arrays.asList(IGNORED_WORD_LIST));

	public CircularShifter() {
	}

	public CircularShifter(List<Line> lines){
		this.lines = lines;
	}

	public List<Line> generateShiftedResult() {
		ArrayList<Line> result = new ArrayList<Line>();
		for (Line line:lines){
						if (!IGNORED_WORD_SET.contains(line.getWords()[0].toLowerCase())) {
								result.add(line);
						}
			for (int i = 0; i < line.getWords().length; i++){
				if (IGNORED_WORD_SET.contains(line.getWords()[i].toLowerCase())){
					result.add(generateShiftedNewLineFromIndex(line,i));
				}
			}
		}
		return result;
	}

	private Line generateShiftedNewLineFromIndex(Line line,int index){
		int lineLength = line.getWords().length;
		String[] words = new String[lineLength];
		int count = 0;
		for (int i=index + 1;i<lineLength;i++){
			words[count++] = line.getWords()[i];
		}
		for (int i=0;i<=index;i++){
			words[count++] = line.getWords()[i];
		}
		return new Line(words);
	}

	@Override
	public void run() {
		this.lines = super.read();
		super.write(generateShiftedResult());
	}
}

@@ -674,19 +674,16 @@
 lines)%7B%0A
-%09%09%09
 %09%09%09if (!
@@ -745,20 +745,16 @@
 e())) %7B%0A
-%09%09%09%09
 %09%09%09%09resu
@@ -767,19 +767,16 @@
 (line);%0A
-%09%09%09
 %09%09%09%7D%0A%09%09%09
@@ -900,27 +900,35 @@
 )%7B%0A%09%09%09%09%09
-result.add(
+Line shiftedLine = 
 generate
@@ -958,18 +958,135 @@
 (line,i)
-)
 ;
+%0A%09%09%09%09%09if (!IGNORED_WORD_SET.contains(shiftedLine.getWords()%5B0%5D.toLowerCase())) %7B%0A%09%09%09%09%09%09result.add(shiftedLine);%0A%09%09%09%09%09%7D
 %0A%09%09%09%09%7D%0A%09

Fix bug on line with multiple ignore words