package com.example.fw;

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.utils.SortedListOf;

public class PrintedPhonesHelper extends BaseHelper {

	public PrintedPhonesHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public SortedListOf<String> getContacts(){
		manager.navigateTo().printPhonesPage();
		return getDataFromTableCells();
	}
	
	private SortedListOf<String> getDataFromTableCells(){
		List<WebElement> cells = driver.findElements(By.xpath("//table[@id='view']/tbody/tr/td[@*]"));
		
		SortedListOf<String> printedContactPhones = new SortedListOf<String>();
		
		Pattern pattern = Pattern.compile("^(.*):\\s*$|^H:(.*)$", Pattern.MULTILINE);
		
		for (WebElement cell : cells) {	
			Matcher matcher = pattern.matcher(cell.getText());
			
			int idx = 1;
			StringBuffer contactRecord = new StringBuffer();
			while(matcher.find()){
				contactRecord.append(" ").append(matcher.group(idx).trim());
				idx++;
			}
			
			printedContactPhones.add(contactRecord.toString().trim());
		}
		
		return printedContactPhones;
	}

}

@@ -791,13 +791,23 @@
 *$%7C%5E
-H:(.*
+%5BHMWP%5D:%5C%5Cs(%5C%5Cd+
 )$%22,
@@ -865,17 +865,16 @@
 cells) %7B
-%09
 %0D%0A%09%09%09Mat
@@ -1020,16 +1020,29 @@
 r.find()
+ && (idx %3C 3)
 )%7B%0D%0A%09%09%09%09

task 10: regular expression was changed