package ua.nure.jfn.task3;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Part3 {

	private static final String PATH = "part3.txt";

	public static void main(String[] args) {
		String lines = Utils.getContent(PATH);
		System.out.println(convert(lines));
	}

	public static String convert(String input) {
		String[] words = input.split("\\s+");
		String result = input;

		int last = 0;
		String[] processed = new String[words.length];
		for (String word : words) {
			if (Utils.contains(word, processed))
				continue;

			Pattern pattern = Pattern.compile("(?<=^|\\s)(\\Q" + word + "\\E)(?=$|\\s)",
					Pattern.UNICODE_CHARACTER_CLASS);

			Matcher matcher = pattern.matcher(input);
			for (int i = 0; matcher.find(); i++) {
				if (i % 2 != 0)
					result = Utils.copyOfRange(result, 0, matcher.start()) + Utils.changeCase(matcher.group())
							+ Utils.copyOfRange(result, matcher.end(), input.length());
			}

			processed[last++] = word;
		}

		return result.toString();
	}
}
