package ua.nure.jfn.task4;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Part1 {

	private static final String PATH = "part1.txt";
	private static final Pattern LATN_PATTERN = Pattern.compile("\\b\\p{IsLatin}+\\b",
			Pattern.UNICODE_CHARACTER_CLASS);
	private static final Pattern CYRL_PATTERN = Pattern.compile("\\b\\p{IsCyrillic}+\\b",
			Pattern.UNICODE_CHARACTER_CLASS);

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		String fileContent = null;
		try {
			fileContent = new String(Files.readAllBytes(Paths.get(PATH)));
		} catch (IOException ex) {
			ex.printStackTrace();
			scanner.close();
			return;
		}

		String result;
		String input;
		while (scanner.hasNextLine()) {
			input = scanner.nextLine();

			if (input.equalsIgnoreCase("stop")) {
				break;
			}

			if (input.equals("Latn")) {
				result = extractWords(fileContent, LATN_PATTERN);
			} else if (input.equals("Cyrl")) {
				result = extractWords(fileContent, CYRL_PATTERN);
			} else {
				result = "Incorrect input";
			}

			System.out.printf("%s: %s\n", input, result);
		}
		scanner.close();
	}

	private static String extractWords(String text, Pattern pattern) {
		String words = "";
		Matcher matcher = pattern.matcher(text);

		while (matcher.find())
			words += " " + matcher.group();

		return words.trim();
	}
}
