package ua.nure.jfn.task3;

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

public class Part1 {

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

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

	public static String convert1(String input) {
		Matcher matcher = Pattern.compile(" ([\\S]*;[\\S]*@[\\S]*)").matcher(input);

		String result = "LastName;Email";
		while (matcher.find())
			result += "\n" + matcher.group(1);

		return result;
	}

	public static String convert2(String input) {
		Matcher matcher = Pattern.compile(";([\\S]*) *([\\S]*) ([\\S]*);").matcher(input);

		String result = "LastName;MiddleName;FirstName";
		while (matcher.find())
			result += String.format(
					"\n%s;%s;%s",
					matcher.group(3),
					matcher.group(2),
					matcher.group(1));

		return result;
	}

	public static String convert3(String input) {
		Matcher matcher = Pattern.compile("(.*);(.*) ([^\\s]*);").matcher(input);

		int[] len = { 0, 0, 0 };
		while (matcher.find())
			for (int i = 0; i < 3; i++)
				len[i] = Utils.max(len[i], matcher.group(i + 1).length());

		matcher.reset();

		String result = "";
		while (matcher.find())
			result += String.format(
					"\n%-" + len[2] + "s %-" + len[1] + "s %-" + len[0] + "s",
					matcher.group(3),
					matcher.group(2),
					matcher.group(1));

		return result.trim();
	}

}
